BaCon Bits

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#121 Post by sunburnt »

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.

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

Bacon Associative Arrays

#122 Post by GatorDog »

Sunburnt,
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: Select all

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

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

getting text file into array

#123 Post by vovchik »

Dear sunburnt,

I already posted this in the BaCon forum, but here it is again. Just another way:

Code: Select all

' --------------------
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: Select all

x$ = EXEC$("cat myfile.txt")
SPLIT x$ BY NL$ TO myarray$ SIZE mysize

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#124 Post by big_bass »

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"
If it is terseness that you're after, you can always do the following:
I like terse and simple and commented :D
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: Select all

'--- 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: Select all

#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

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

Bacon regular expressions

#125 Post by GatorDog »

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: Select all

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
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? :wink:

rod
Attachments
sparky.gif
(498 Bytes) Downloaded 1502 times

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#126 Post by sunburnt »

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.!

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#127 Post by GatorDog »

Hey big_bass,
big_bass wrote:is there a way to covert this bash snippet to BaCon to check for undefined arrays

Code: Select all

#str=something

str=""

if [ $str ]
then
   echo "Not empty"
else
   echo "Empty"
fi 
Is this what you're asking for :?:

Code: Select all

str$ = ""

IF LEN( str$ ) THEN
	PRINT "Not empty"
ELSE
	PRINT "Empty"
END IF
GatorDog

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#128 Post by vovchik »

Dear big_bass and GatorDog,

This function works for string arrays:

Code: Select all

' 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

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#129 Post by GatorDog »

Hey vovchik,

Code: Select all

' 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

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#130 Post by sunburnt »

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.

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#131 Post by vovchik »

Dear GatorDog and sunburnt,

This is one example of the "bound" function:

Code: Select all

' 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: Select all

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

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#132 Post by GatorDog »

Ok, got it. I didn't pick up on "STRING" being variable type re: SIZEOF(STRING)

Thanks for clarification and code :)

GatorDog

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#133 Post by big_bass »

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

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#134 Post by sunburnt »

So the BaCon docs are not quite right...
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...

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#135 Post by big_bass »

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: Select all

'--- 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 :D
Last edited by big_bass on Fri 11 Nov 2011, 19:54, edited 5 times in total.

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#136 Post by GatorDog »

Hey big_bass,

Where's that dance? :P

r

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#137 Post by sunburnt »

GatorDog; Do they even allow dancing on this forum? :lol:

big_bass; Pjot showed code to input a file in one read.
More codey of course, but BaCon does it all by itself, no Bash.
Would go good in a function library I think. BaCon needs standard libraries.

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#138 Post by GatorDog »

Do they even allow dancing on this forum?
Sorry, my baD. (mea culpa) :cry:

GatorDog

Volhout
Posts: 547
Joined: Sun 28 Dec 2008, 08:41

COM port

#139 Post by Volhout »

I am trying to communicate with an oscilloscope via serial port (38.4kbaud).
I blindly tried the OPEN "COM1:38400" command, but that does not work.
I am using WARY 5.1.4.1 and have the dev. pack installed.

Anyone tried this ?

What do I have to do to read and write text strings (SCPI commands) through the COM port using BaCon ??

Regards,

Volhout

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#140 Post by big_bass »

Hey Volhout
take a look here I dont want to double post
it will get you closer and there is a link for more stuff using the serial port
*'llI try to dig up some very old code I used to connect my ti-85 calculator using the serial port
I didnt write the serial code but tested *(I did write code for the parallel port though)* it to be working using Qbasic

http://basic-converter.proboards.com/in ... thread=191
http://www.faqs.org/docs/Linux-mini/IO- ... html#ss6.3
http://www.easysw.com/~mike/serial/serial.html
Joe

Post Reply