BaCon - Bash-based Basic-to-C converter/compiler

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
miriam
Posts: 373
Joined: Wed 06 Dec 2006, 23:46
Location: Queensland, Australia
Contact:

Linux From Scratch

#521 Post by miriam »

Actually, I need to remind myself to visit the Linux From Scratch project's pages more often, as it has so much useful info there. Basically it, and its sister projects, build versions of Linux totally from start (hence the name) and document the process in its entirety.

http://www.linuxfromscratch.org

Of course those Linux forms don't have the cool features of Puppy -- their Linux From Scratch LiveCD project is much more bloated (fills the CD) with less content and less features. However it does have the wonderful advantage of being almost completely documented. There is so much yummy information there...
[color=blue]A life! Cool! Where can I download one of those from?[/color]

User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

Help needed

#522 Post by e_mattis »

Hey guys!

I'm having problems. I'm trying to take a file with some text strings and reading it in as an array. I need to be able to compare the array elements and create anew array of totally unique elements. the list i am using to learn how is as follows:

array[1] = "SP"
array[2] = "HP"
array[3] = "SP"
array[4] = "B-tip"
array[5] = "B-tip"

I need to create an array containing only the following:

array[1] = "SP"
array[2] = "HP"
array[3] = "B-tip"

I've been through the documentation and have some limited (last time I did things like this was over 30 years ago - before qbasic even :shock: ) programming experience. I think it used to be done in a double-loop (ie. For x = 1 to 5, for y = 1 to 5, etc) Of course that could just be for bubble loops...can't remember ...grrrr :x

I just can't seem to find a handle on it, so I turn to those more knowledgeable for assistance. Any help is appreciated!

Thanks!

E

.

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

#523 Post by sunburnt »

I just coded a solution for this.

It looks like you`re wanting to remove duplicate lines.
You don`t say what the delimiter is, I assume it`s NL$.

Read the file into a list and test it for dups. like this:

Code: Select all

OPEN File$ FOR READING AS file_
list$ = ""
WHILE NOT(ENDFILE(file_))
	READ line$ FROM file_
	IF ENDFILE(file_) THEN BREAK
	IF INSTR(list$, line$) > 0 THEN CONTINUE
	list$ = list$ & line$ & NL$
WHEN
CLOSE FILE file_

SPLIT list$ BY NL$ TO array$ SIZE Sz
This code ignores any duplicate lines and makes an array.

I know Bash`s "uniq" will do the same, but I assume BaCon code is wanted.
Enjoy dude..!
.
Last edited by sunburnt on Fri 17 May 2013, 21:01, edited 3 times in total.

User avatar
miriam
Posts: 373
Joined: Wed 06 Dec 2006, 23:46
Location: Queensland, Australia
Contact:

#524 Post by miriam »

If you have the text strings as lines in a file called list.txt, like

Code: Select all

dog
cat
tree
world
dog
tree
Then you can list the unique strings like this:

Code: Select all

sort list.txt | uniq >no_duplicates.txt
Then you can read the no_duplicates.txt file in using BaCon to do whatever else you want with it.

I don't know enough about BaCon to be able to give a fully BaCon solution, but it seems that if using the shell makes your task simpler it's worth using it.
[color=blue]A life! Cool! Where can I download one of those from?[/color]

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

#525 Post by vovchik »

Dear all,

If we take miriam's list as a basis:

Code: Select all

dog
cat
tree
world
dog
tree
we can do this little bit of processing:

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

' ------------------
FUNCTION UNIQ$(STRING org$)
' ------------------
	' assumes OPTION BASE 0
	LOCAL i TYPE NUMBER
	LOCAL new$ TYPE STRING
	SPLIT org$ BY NL$ TO tmp$ SIZE elements
	SORT tmp$
	FOR i = 1 TO elements - 1
		IF tmp$[i] = tmp$[i - 1] THEN
			tmp$[i - 1] = " "
		END IF
	NEXT i
	SORT tmp$
	JOIN tmp$ BY NL$ TO new$ SIZE elements
	RETURN CHOP$(new$)
END FUNCTION
		
myoldlist$ = CAT("list.txt")
PRINT "Old list: ", NL$, myoldlist$
mynewlist$ = UNIQ$(myoldlist$)
PRINT "New list: ", NL$, mynewlist$
OPEN "newlist.txt" FOR WRITING AS newfile
WRITELN mynewlist$ TO newfile
CLOSE FILE newfile
The function CAT is there just there for convenience - to read the 'list.txt' file into a string variable. There are many ways of doing this, but this is one of them. Note that it doesn't take care of numerics well. You would have to check for a numeric type (probably using REGEX) and then prepend the requisite number of space pads before sorting - not hard.

With kind regards,
vovchik

User avatar
e_mattis
Posts: 114
Joined: Fri 21 Dec 2012, 02:24
Location: Williamston, SC
Contact:

#526 Post by e_mattis »

Thanks guys!

Very kewl how it can be accomplished in a variety of ways! I appreciate the help very much! You guys ROCK! :D

E

.

Post Reply