Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Mon 01 Sep 2014, 18:26
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
BaCon Bits
Post new topic   Reply to topic View previous topic :: View next topic
Page 8 of 12 [168 Posts]   Goto page: Previous 1, 2, 3, ..., 6, 7, 8, 9, 10, 11, 12 Next
Author Message
GatorDog


Joined: 12 Sep 2006
Posts: 136

PostPosted: Mon 31 Oct 2011, 06:03    Post subject: Bacon port of "resizepfile"
Subject description: Using Bacon's Associative arrays
 

"resizepfile.sh" is a Puppy script to resize the Puppy save file.
This program is a port of the resizepfile script to a Bacon program.
In particular it makes use of Bacon's Associative array.
Quote:
An associative array is an array of which the index is determined by a string, instead of a number. Associative arrays use
round brackets '(…)' instead of the square brackets '[…]' used by normal arrays.

An associative array can use any kind of string for the index, and it can have an unlimited amount of elements.

Code:
fullname$("bob") = "Robert Fuller"
fullname$("jane") = "Janet Jenkins"
name$ = "bob"
PRINT fullname$(name$)

Output: Robert Fuller
EDIT: Corrected missing "$" on variable name.

The resizepfile shell script uses an external file (/etc/rc.d/PUPSTATE) to retrieve variables and their values.
The variables are in shell syntax. ie. variable = 'text'. This syntax is decoded and assigned
values in Bacon using an associative array.

This is the contents of (my) /etc/rc.d/PUPSTATE:
Code:
PUPMODE=12
PDEV1='sr1'
DEV1FS='iso9660'
PUPSFS='sda1,vfat,/l5281108.173/lupu_528.sfs'
PUPSAVE='sda1,vfat,/lupusave-528_IM-.3fs'
PMEDIA='cd'
#ATADRIVES is all internal ide/pata/sata drives, excluding optical, excluding usb...
ATADRIVES='sda '
#ATAOPTICALDRIVES is list of non-usb optical drives...
ATAOPTICALDRIVES='sr0 sr1 '
#these directories are unionfs/aufs layers in /initrd...
SAVE_LAYER='/pup_rw'
PUP_LAYER='/pup_ro2'
#The partition that has the lupusave file is mounted here...
PUP_HOME='/mnt/dev_save'
#(in /initrd) ...note, /mnt/home is a link to it.
#this file has extra kernel drivers and firmware...
ZDRV=''
#complete set of modules in the initrd (moved to main f.s.)...
ZDRVINIT='no'
#Partition no. override on boot drive to which session is (or will be) saved...
PSAVEMARK=''

The variable name (ex. PUPMODE) will be used for the associative arrays index (ex. Pupstate$("PUPMODE")
I used a SUB-routine to translate the shell variables. Here is commented code.
Code:
' ------------------
SUB READ_PUPSTATE_VARIABLES
' ------------------
   ' Declare the associative array
   DECLARE Pupstate$ ASSOC STRING

   ' Open the file for reading
   OPEN Pupstate_file$ FOR READING AS Filehandle_

   ' Check for end of file
   WHILE NOT(ENDFILE(Filehandle_)) DO

      ' read in line by line
      READLN Pass$ FROM Filehandle_

      ' CHOP$ - remove leading/trailing space, tab, CR, etc.
      Pass$ = CHOP$(Pass$)

      ' If line begins with "#" it's a comment.
      IF LEFT$(Pass$, 1) = "#" THEN
         'Dump bash comment lines
         CONTINUE
      END IF

      ' If line contains "=", there's a variable assignment going on.
      IF INSTR(Pass$, "=") THEN

         ' Break line up using "=" as seperator
         ' Arg$[1] will be variable name. Arg$[2] is the value.
         SPLIT Pass$ BY "=" TO Arg$ SIZE na

         ' Replace the single quotes with null
         Pass$ = CHOP$(REPLACE$(Arg$[2], Single_quote$, ""))

         ' Set up variable = value
         Pupstate$(Arg$[1]) = Pass$
      END IF
   WEND
   ' Close file
   CLOSE FILE Filehandle_
END SUB

This is a simple use of associative arrays. There are some extended examples in the Bacon reference pages.
The rest of the program implements the actions of the shell script along with the addition of a GUI.

GatorDog
resizepfile.png
 Description   
 Filesize   36.56 KB
 Viewed   842 Time(s)

resizepfile.png

Bacon-resizepfile-src.tar.gz
Description  Bacon source code for resizepfile.bac
gz

 Download 
Filename  Bacon-resizepfile-src.tar.gz 
Filesize  3.64 KB 
Downloaded  237 Time(s) 

Last edited by GatorDog on Mon 31 Oct 2011, 14:59; edited 1 time in total
Back to top
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1747

PostPosted: Mon 31 Oct 2011, 10:43    Post subject:  

Hey GatorDog

thanks for posting that http://www.murga-linux.com/puppy/viewtopic.php?t=69647&start=114 (I had already ported the old xmessage resizepfile.sh
to Xdialog and added larger save options) so going over your BaCon version
will be easier for me to understand and follow this new associative array in action
thanks this will be fun to learn in Bacon Very Happy

*I know this is a BaCon thread but only to ease into understanding what is happening
the equivalent bash4 example is given below using your example converted to bash4

GatorDog
Quote:
# associative array Bacon example
fullname("bob") = "Robert Fuller"
fullname("jane") = "Janet Jenkins"
name = "bob"
echo $fullname($name)




big_bass
Code:
# bash4 associative array equivalence to the Bacon example above

declare -A fullname
#       -A option declares associative array.

fullname[bob]="Robert Fuller"
fullname[jane]="Janet Jenkins"


echo "bob's fullname is ${fullname[bob]}."
echo "jane's fullname is ${fullname[jane]}."

echo "${!fullname[*]}"   # The array indices used


Joe

_________________
debian wheezy ,linux mint, slackware I use them all and they all have good points
Mint would be best for general users though
Back to top
View user's profile Send private message 
GatorDog


Joined: 12 Sep 2006
Posts: 136

PostPosted: Mon 31 Oct 2011, 11:49    Post subject:  

Hi Joe,

To further the comparison a little, LOOKUP is used to get the array indices in Bacon.
(LOOKUP works similar to the SPLIT command)
LOOKUP <assoc> TO <array> SIZE <variable>
Code:
LOOKUP Pupstate$ TO Index_name$ SIZE Count

and to access -
Code:
FOR x = 0 TO Count - 1
    PRINT Index_name$[x]
NEXT


GatorDog
Back to top
View user's profile Send private message 
GatorDog


Joined: 12 Sep 2006
Posts: 136

PostPosted: Thu 03 Nov 2011, 09:36    Post subject: Links to example programs in this thread  

Edited first post to include these links to example programs in this thread.

Video-Play
HKP - Hug Keyword Parser
Start of "Hello Bacon (World)"
More Hello Bacon
Even More Hello Bacon
Still more Hello Bacon
Hello Bacon - a complete app
Bacon File I/O
Bacon port of resizepfile "Resize pup personal save file"

GatorDog
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5016
Location: Arizona, U.S.A.

PostPosted: Tue 08 Nov 2011, 17:00    Post subject:  

Hi guys; Thought I`d ask before just doing this in Bash.

My years of Quick Basic are slowly coming back to me as I use BaCon.
I recalled that Basic works with arrays not lists, Bash is powerful with both.
Basic`s problem with arrays is getting data into them... It`s one line at a time.
Bash will read and write files and arrays all in one statement.
I`m going to suggest this for the next BaCon, and also passing arrays to/from Bash.

# The Q:
An easy way to get the number of lines in a file to declare the array index?
All I can think of to do is:
Code:
   OPEN File$ FOR INPUT AS file
   i = -1
   WHILE NOT(ENDFILE(file)) DO
      INCR i
      READLN txt$ FROM file
   WEND
   CLOSE file

   GLOBAL Array$[i] TYPE STRING

   OPEN File$ FOR INPUT AS file
   i = 0
   WHILE NOT(ENDFILE(file)) DO
      READLN txt$ FROM file
      IF ENDFILE(file) THEN BREAK
      Array$[i] = txt$
      INCR i
   WEND
   CLOSE file

Not a very efficient or elegant way to do anything at all.!
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Tue 08 Nov 2011, 21:47    Post subject:  

sunburnt,

Perhaps something like this-

Code:
i$=EXEC$("wc -l filename")


Whether that's faster or not remains to be seen.... Smile

regards,
s
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5016
Location: Arizona, U.S.A.

PostPosted: 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
View user's profile Send private message 
GatorDog


Joined: 12 Sep 2006
Posts: 136

PostPosted: 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
View user's profile Send private message 
vovchik


Joined: 23 Oct 2006
Posts: 1285
Location: Ukraine

PostPosted: 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
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1747

PostPosted: 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 Very Happy
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

_________________
debian wheezy ,linux mint, slackware I use them all and they all have good points
Mint would be best for general users though
Back to top
View user's profile Send private message 
GatorDog


Joined: 12 Sep 2006
Posts: 136

PostPosted: 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? Wink

rod
sparky.gif
 Description   
 Filesize   498 Bytes
 Viewed   1325 Time(s)

sparky.gif

Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5016
Location: Arizona, U.S.A.

PostPosted: 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
View user's profile Send private message 
GatorDog


Joined: 12 Sep 2006
Posts: 136

PostPosted: 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 Question
Code:
str$ = ""

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


GatorDog
Back to top
View user's profile Send private message 
vovchik


Joined: 23 Oct 2006
Posts: 1285
Location: Ukraine

PostPosted: 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
View user's profile Send private message 
GatorDog


Joined: 12 Sep 2006
Posts: 136

PostPosted: 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
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 8 of 12 [168 Posts]   Goto page: Previous 1, 2, 3, ..., 6, 7, 8, 9, 10, 11, 12 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

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
[ Time: 0.1216s ][ Queries: 12 (0.0066s) ][ GZIP on ]