The time now is Sun 08 Dec 2019, 21:09
All times are UTC - 4 |
Author |
Message |
sunburnt

Joined: 08 Jun 2005 Posts: 5087 Location: Arizona, U.S.A.
|
Posted: Wed 09 Nov 2011, 02:02 Post subject:
|
|
seaside; I thought of that, but why bother with BaCon if always using Bash.
Long ago in Quick Basic I noticed the HD cranking with each "read line".
Visual Basic had a second "input" command that would take in the whole file.
I`d like to see BaCon do file I/O and fill arrays quickly in one command.
I thought of just making a BaCon function library to simplify the repetitive code.
But that doesn`t improve BaCon`s actual operation any. It`d still work the HD.
At the moment my thought is porting my Bash app. "sysinfo" to BaCon.
I could do as you suggested, but then why not just have it call sysinfo?
I wrote a wrapper script to redirect sysinfo`s output to a file for BaCon to read.
But BaCon as a wrapper for Bash is sad. What`s the point of a compiled exec.?
For BaCon to be really useful it has to do most code stuff by itself.
|
Back to top
|
|
 |
GatorDog

Joined: 12 Sep 2006 Posts: 136
|
Posted: Wed 09 Nov 2011, 03:57 Post subject:
Bacon Associative Arrays |
|
Sunburnt,
Quote: | An easy way to get the number of lines in a file to declare the array index? |
One way to accomplish the array assignment is to use Associative arrays.
This snipit will give you the line count and also goes ahead and assigns the lines to Array$(....) .
Code: | DECLARE Array$ ASSOC STRING
Array_index = 0
OPEN My_file$ FOR READING AS Filehandle_
WHILE NOT(ENDFILE(Filehandle_)) DO
INCR Array_index
READLN Txt$ FROM Filehandle_
Array$(STR$(Array_index)) = Txt$
WEND
CLOSE FILE Filehandle_ |
GatorDog
|
Back to top
|
|
 |
vovchik

Joined: 23 Oct 2006 Posts: 1512 Location: Ukraine
|
Posted: Wed 09 Nov 2011, 04:26 Post subject:
getting text file into array |
|
Dear sunburnt,
I already posted this in the BaCon forum, but here it is again. Just another way:
Code: |
' --------------------
FUNCTION CAT(STRING FILENAME$)
' --------------------
LOCAL fileline$, txt$ TYPE STRING
IF FILEEXISTS(FILENAME$) THEN
OPEN FILENAME$ FOR READING AS catfile
WHILE NOT(ENDFILE(catfile)) DO
READLN fileline$ FROM catfile
txt$ = CONCAT$(txt$, fileline$, NL$)
WEND
CLOSE FILE catfile
END IF
RETURN CHOP$(txt$)
END FUNCTION
x$ = CAT("myfile.txt")
SPLIT x$ BY NL$ TO myarray$ SIZE mysize
|
The var "mysize" will be the size of the arrray (i.e. no. of lines). You can use "OPTION COLLAPSE" at the top of your program to ignore empty lines. As for passing arrays to SUBs and FUNCTIONs, Peter explains that business in the first few pages of the BaCon manual. It is entirely possible.
With kind regards,
vovchik
PS. BaCon should not be construed as a bash replacement, but a normal compiled language, like C, but with nicer, easier syntax. If you have complex tasks, it runs circles around bash.
PPS. If it is terseness that you're after, you can always do the following:
Code: |
x$ = EXEC$("cat myfile.txt")
SPLIT x$ BY NL$ TO myarray$ SIZE mysize
|
|
Back to top
|
|
 |
big_bass
Joined: 13 Aug 2007 Posts: 1742
|
Posted: Wed 09 Nov 2011, 10:26 Post subject:
|
|
thanks vovchik,GatorDog
for the great code snippets
and thanks for sparking the question sunburnt
I was trying to do arrays in BaCon too
as I was following the presize.bac code GatorDog posted
since I am used to doing this in bash I want to do it in Bacon also
arrays are the easy way to pull out data
*really the whole problem with linux in general is
important files have scrambled un formatted
data that needs much filtering to get the data out into "useable"
data if much pre thought went into those important files
we wouldnt need so many different tools to filter out the data
and everything would be easier and faster
I commented about this in "speeding up bash scripts"
Quote: |
If it is terseness that you're after, you can always do the following:
|
I like terse and simple and commented
I like to recycle simple code snippets sometimes I forget how to
do somethings that appear easy
it takes too much time to read large programs
and take out some small usable pieces because they get too complex
as time goes while new features get added quickly
so here is simple I took vovchik's snippet and added just a little bit to it
to keep it simple and recyclable
Joe
Code: |
'--- cat the file into an array using x$ ---'
x$ = EXEC$("cat /etc/rc.d/PUPSTATE")
SPLIT x$ BY NL$ TO myarray$ SIZE mysize
'--- this prints all arrays ---'
FOR x = 0 TO mysize - 1
PRINT myarray$[x]
NEXT
'--- this prints only the first array ---'
PRINT myarray$[0]
'--- this prints only the scecond array ---'
PRINT myarray$[1]
'--- this prints only the third array ---'
PRINT myarray$[2]
|
is there a way to covert this bash snippet to BaCon
to check for undefined arrays
Code: |
#str=something
str=""
if [ $str ]
then
echo "Not empty"
else
echo "Empty"
fi
|
now if we only had some regular expressions in Bacon
explained with simple code snippets
I would be so happy it would make me dance
|
Back to top
|
|
 |
GatorDog

Joined: 12 Sep 2006 Posts: 136
|
Posted: Wed 09 Nov 2011, 13:34 Post subject:
Bacon regular expressions |
|
Quote: | now if we only had some regular expressions in Bacon explained with simple code snippets | Bacon REGEX
This checks that the text does not contain digits.
Then it looks for an underline chr or a capital X. Code: | Txt$ = "big_bass"
IF REGEX(Txt$, "[^[:digit:]]") THEN
PRINT "Yep, ", Txt$, " is an ahpha dog!"
END IF
IF REGEX(Txt$, "_|X") THEN
PRINT "Yes, there IS an underline chr or a capital X."
END IF |
Quote: | I would be so happy it would make me dance |
It may not rise to the level of a dance, but can I at least get a toe-tap?
rod
Description |
|
Filesize |
498 Bytes |
Viewed |
1496 Time(s) |

|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 5087 Location: Arizona, U.S.A.
|
Posted: Wed 09 Nov 2011, 14:45 Post subject:
|
|
Hey Gatordog; I hadn`t noticed the REGEX command, rather useful.!
big_bass; Yep, there`s sooo much parsing in Bash making it very "codey".
You`d think the authors of Linux`s execs. would`ve made raw output modes.
# That`s why I wrote the Bash function library sysinfo, to get raw data.
I`ll be adding more "needed" functions to it, ie: file size, etc. Any suggestions?
Too bad it becomes a dependency, now if scripts like it were std. in Puppy...
vovchik; Excellent.! I didn`t think of using a variable first. Much simpler.!
|
Back to top
|
|
 |
GatorDog

Joined: 12 Sep 2006 Posts: 136
|
Posted: Wed 09 Nov 2011, 17:51 Post subject:
|
|
Hey big_bass,
big_bass wrote: | is there a way to covert this bash snippet to BaCon to check for undefined arrays |
Code: | #str=something
str=""
if [ $str ]
then
echo "Not empty"
else
echo "Empty"
fi |
Is this what you're asking for Code: | str$ = ""
IF LEN( str$ ) THEN
PRINT "Not empty"
ELSE
PRINT "Empty"
END IF |
GatorDog
|
Back to top
|
|
 |
vovchik

Joined: 23 Oct 2006 Posts: 1512 Location: Ukraine
|
Posted: Wed 09 Nov 2011, 18:21 Post subject:
|
|
Dear big_bass and GatorDog,
This function works for string arrays:
Code: |
' Define runtime check for size of array
DEF FN bound(x) = SIZEOF(x)/SIZEOF(STRING)
|
It returns the number of elements. As for the bash equivalent to return empty or not for a particular array element, LEN returns "FALSE" if there is nothing there. If there is something in the string, LEN will be "TRUE" in the Boolean sense. GatorDog's example is perfect in that regard.
With kind regards,
vovchik
|
Back to top
|
|
 |
GatorDog

Joined: 12 Sep 2006 Posts: 136
|
Posted: Thu 10 Nov 2011, 06:31 Post subject:
|
|
Hey vovchik, Code: | ' Define runtime check for size of array
DEF FN bound(x) = SIZEOF(x)/SIZEOF(STRING) |
Could you clarify what is passed in "x" and where "STRING" comes into play?
tnx
GatorDog
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 5087 Location: Arizona, U.S.A.
|
Posted: Thu 10 Nov 2011, 21:01 Post subject:
|
|
vovchik; I think I know what`s going on in your code snip, but tell us anyway...
I agree writing a multimedia codec in BaCon would be much better than Bash.
But so much of what`s done ( or needed ) is system related stuff.
There`s not many full blown applications being written that are new.
Most of what I see is people rewriting and reinventing the wheel.
My sysinfo function library is a good example, nothing new there.
I just wanted "no parsing raw data output" for many things all in one file.
|
Back to top
|
|
 |
vovchik

Joined: 23 Oct 2006 Posts: 1512 Location: Ukraine
|
Posted: Fri 11 Nov 2011, 03:41 Post subject:
|
|
Dear GatorDog and sunburnt,
This is one example of the "bound" function:
Code: |
' Define runtime check for size of array
DEF FN BOUND_STR(x) = SIZEOF(x)/SIZEOF(STRING)
' Create array of adjectives
DECLARE adje$[] = { "autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark", "summer", "icy", \
"delicate", "quiet", "white", "cool", "spring", "winter", "patient", "twilight", "dawn", "crimson", \
"wispy", "weathered", "blue", "billowing", "broken", "cold", "damp", "falling", "frosty", "green" }
' Create array of nouns
DECLARE noun$[] = { "waterfall", "river", "breeze", "moon", "rain", "wind", "sea", "morning", "snow", "lake", \
"sunset", "pine", "shadow", "leaf", "dawn", "glitter" }
' make randomized ajdective and noun line from array elements
myline1$ = CONCAT$(adje$[RANDOM(BOUND_STR(adje$))], " ", noun$[RANDOM(BOUND_STR(noun$))])
' show result
PRINT myline1$
' show array dimensions
PRINT BOUND_STR(adje$)
PRINT BOUND_STR(noun$)
|
If we don't know how many elements are in the arrays adje$[] or noun$[], BOUND_STR(adje$) and BOUND_STR(noun$) will tell us.
You can also do the same with numbers:
Code: |
DEF FN BOUND_NUM(x) = SIZEOF(x)/SIZEOF(NUMBER)
' Create array of numbers
DECLARE nums[] = { 1, 2, 3.5, 4, -5, 6, 7, 8, 9, 10, 11, 0 }
PRINT BOUND_NUM(nums)
|
With kind regards,
vovchik
|
Back to top
|
|
 |
GatorDog

Joined: 12 Sep 2006 Posts: 136
|
Posted: Fri 11 Nov 2011, 09:45 Post subject:
|
|
Ok, got it. I didn't pick up on "STRING" being variable type re: SIZEOF(STRING)
Thanks for clarification and code
GatorDog
|
Back to top
|
|
 |
big_bass
Joined: 13 Aug 2007 Posts: 1742
|
Posted: Fri 11 Nov 2011, 13:05 Post subject:
|
|
Thanks GatorDog and vovchik
for code examples
I wanted to
fix a small part of the bacon2bb code
that adds the grey color to the comments
and using regular expressions
simplifies this little part of the code to reduce it down to
just this idea the fix will follow
Joe
code
http://www.puppy2.org/slaxer/regularexp.html
I use this to convert the bb code to html
http://www.bbcode-to-html.com/
*I will have an option to generate html
had to work out the comment part of the code first
@vovchick this is your work with color
you may want to put all these on a web page some day so
here is a start *notice no problem with word wrapping in html
http://www.puppy2.org/slaxer/vovchik-array.html
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 5087 Location: Arizona, U.S.A.
|
Posted: Fri 11 Nov 2011, 14:07 Post subject:
|
|
So the BaCon docs are not quite right...
Quote: | Arrays must be declared with fixed dimensions, meaning that it is not possible to determine the dimensions of an array using variables or functions, so during program runtime. The reason for this is that the C compiler needs to know the array dimensions during compile time. Therefore the dimensions of an array must be defined with fixed numbers or with CONST definitions. |
### Your code example should be added to BaCon`s doc. section on arrays.
### This also answers my Q about filling an array in one statement.
###> Very Good vovchik.! ... Many thanks for the patient guidance...
|
Back to top
|
|
 |
big_bass
Joined: 13 Aug 2007 Posts: 1742
|
Posted: Fri 11 Nov 2011, 15:22 Post subject:
|
|
Quote: | Arrays must be declared with fixed dimensions, meaning that it is not possible to determine the dimensions of an array using variables or functions, so during program runtime. The reason for this is that the C compiler needs to know the array dimensions during compile time. Therefore the dimensions of an array must be defined with fixed numbers or with CONST definitions. |
this above quote is correct in BaCon coding they were declared above
in the example vovchik posted http://www.puppy2.org/slaxer/vovchik-array.html
*this example below cheats the bacon compiler though
because it is a result from a file read from the "shell" into an array
so the compiler gives it a string value reserved in memory
then continues along without any errors
so if the file wasnt there you would get only a missing file error
in the run time
without declaring myarray$[x] it is seen as a string
the mysize it not known to Bacon until the file is read
notice the mysize -1
traditionally mysize would be some fixed number
like this FOR x = 0 TO 10 you would need to
know how many times to loop
Code: |
'--- cat the file into an array using x$ ---'
x$ = EXEC$("cat /etc/rc.d/PUPSTATE")
SPLIT x$ BY NL$ TO myarray$ SIZE mysize
'--- this prints all arrays ---'
FOR x = 0 TO mysize - 1
PRINT myarray$[x]
NEXT |
anyway the good news is
we have more code snippets to do some cool things
Last edited by big_bass on Fri 11 Nov 2011, 15:54; edited 5 times in total
|
Back to top
|
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|