Source language: Translate to:

File read & File write

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

Postby redjr » Wed Jan 13, 2010 12:16 pm

Wrangler,

After I added in my additional variable that I wanted to add to each line, I modified using your code.

Worked like a champ! Got the perfect results I wanted.

Thanks again,

redjr...
User avatar
redjr
 
Posts: 80
Joined: Tue Jan 06, 2009 2:49 pm
Location: NY

Postby Wrangler » Wed Jan 13, 2010 12:19 pm

Glad it worked for you. :) Take a closer look in the help file for using arrays. They are pretty powerful. You'll find many uses for arrays and loops in coding to come.
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1507
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Postby redjr » Wed Jan 13, 2010 2:50 pm

Wrangler,

I need a bit more help and clarity... I seem to be really dense on this multi-variable per line file stuff and getting it to parse properly.

Now that I have my .txt file built and is working as desired(see below), I want to populate two different ListBoxes with the data from the file.

Code: Select all
BILL   PHASE2
FRED   PHASE2
MARY   PHASE2
SUE   PHASE2
JANE   ANGELS
KEVIN   ANGELS
ELLEN   ANGELS
BOB   ANGELS
BILL   AMBER
MARY   STARLIGHT
FRED   BEACON
ELLEN   BEACON


The first ListBoxA should contain the user name (FRED,MARY, etc.). After the users name is selected in ListBoxA, I want to dynamically populate ListBoxB with just the associated list of project(s) for that user. For instance; ListBoxB would show PHASE2 and AMBER for Bill, and just PHASE2 for Sue, and so on.

Sorry if this seems so elementary. I'm think I'm getting confused on parsing different lines in a file, as opposed to different data on the same line at the same time.

Do you know of any basic tutorial samples that would help me grasp this a bit better? And that your willing to share?

You've been a big help today!

redjr...
User avatar
redjr
 
Posts: 80
Joined: Tue Jan 06, 2009 2:49 pm
Location: NY

Postby Wrangler » Wed Jan 13, 2010 3:04 pm

It would go something like this:

../ first get the length of the file
FileLen "[pubdir]test.dat" "[flen]"

../ clear listboxes
ListBoxDeleteItem "listbox1" "all"
ListBoxDeleteItem "listbox2" "all"

../ loop and read lines
Loop "1" "[flen]" "[xx]"
FileRead "[pubdir]test.dat" "[xx]" "[linedata]"
../ string parse the line
StrParse "[linedata]" "[#32][#32]" "[linepart]" "[linenumparsed]"
ListBoxAddItem "listbox1" "0" "[linepart1]"
ListBoxAddItem "listbox2" "0" "[linepart2]"
endloop

From your post, it looks like there are a couple of spaces seperating the 2 entries per line. Therefor the "[#32][#32]" as the delimiter in strparse. If there was a comma seperating them, make the delimiter a comma, or a tab would be [#9].
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1507
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Postby redjr » Wed Jan 13, 2010 3:40 pm

Wrangler,

That gets me much closer than were I was going. Thanks! Here's the 'but' though.

For ListBoxA I want to create a unique set of names. They should only be listed once. The same goes for ListBoxB. It's the unique combination of both ListBoxA and B I'm trying to get to. The items in ListBoxA should be unique. ListBoxB should be unique too, even though a same name might occur with another user. Once I have these two lists, I can direct the user accordingly programmatically.

Here's my code as implemented;

Code: Select all
..Get the length of the ascii file and store in [flen] variable:
FileLen "[GProg_Path]\MASTER_List.txt" "[flen]"

..Clear Listboxes

ListBoxDeleteItem "ComboBox6" "ALL"
ListBoxDeleteItem "ComboBox21" "ALL"

.. loop and read lines in file
Loop "1" "[flen]" "[xx]"
FileRead "[GProg_Path]\MASTER_List.txt" "[xx]" "[Linedata]"
..Parse the line
StrParse "[Linedata]" "[#09]" "[LinePart]" "[LineNumParsed]"

ListBoxAddItem "ComboBox6" "0" "[LinePart1]"
ListBoxAddItem "ComboBox21" "0" "[LinePart2]"

endloop


Here's what I'm getting in ListBoxA;

BILL
BILL
BOB
ELLEN
ELLEN
.
.
.
Looks like I need to add some compare logic somewhere after the name has been added to the ListBoxA/B.

Thanks again for all your insight and assistance.

redjr..
User avatar
redjr
 
Posts: 80
Joined: Tue Jan 06, 2009 2:49 pm
Location: NY

Postby Wrangler » Wed Jan 13, 2010 3:45 pm

StrParse "[Linedata]" "[#09]" "[LinePart]" "[LineNumParsed]"

Take the '0' out of #09 if the line data in the file is seperated with a tab. See if that works.
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1507
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Postby redjr » Wed Jan 13, 2010 4:30 pm

Wrangler,

That wasn't it, but I'll try recreating my MASTER_List.txt file. Seems to me more logic is required to create a unique list of user names first. Then based on the selected user in ListBoxA a subset of projects is displayed. I can't see any of that going on in the code. Aren't we just parsing each line of the file into its respective variables (Part1 and Part2) and not trying to create a unique list? The second list should only be generated after the selection is made in the first list. I can't see where it can be concurrent.

redjr...

Edit... I was using the #9 as a seperator when creating the original file you helped me with this morning.
User avatar
redjr
 
Posts: 80
Joined: Tue Jan 06, 2009 2:49 pm
Location: NY

Postby Wrangler » Wed Jan 13, 2010 4:49 pm

If you've written to the master file a few times testing it, you may need to delete the data and start over before each test so you don't get duplicates. Just a thought. I've done it before. :)

Basically what it's doing is looping thru each line in the file, parsing the data on that line into parts (2 in this case), then adding the first part to listbox1 and the second part to listbox2 until it reaches the end of the file.

It will add BILL to listbox1, and PHASE2 to listbox2, then FRED to listbox1 and then again PHASE2 to listbox2.

BILL PHASE2
FRED PHASE2

Is this not what you want to do?
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1507
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Postby redjr » Wed Jan 13, 2010 6:50 pm

Yes, but when the loop comes across another set, say BILL and AMBER, a second entry of BILL is written to ListBoxA. What I want to see is a single entry for BILL in ListBoxA and only two entries in ListBoxB of PHASE2 and AMBER.

There are no duplicates in my MASTER_List.txt. I even created it manually just for that reason. At some point I will have to deal with duplicates getting into the file - either programmatically, or manually.

redjr...
User avatar
redjr
 
Posts: 80
Joined: Tue Jan 06, 2009 2:49 pm
Location: NY

Postby Gaev » Wed Jan 13, 2010 7:08 pm

redjr:

If you don't want duplicates ... check for the presence of an entry in the ListBox using ListBoxFindItem ... if it returns a zero (i.e. not found), then add it to the List (ListBoxAddItem), otherwise ignore/discard it.
User avatar
Gaev
 
Posts: 3738
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby redjr » Thu Jan 14, 2010 6:25 am

Wrangler,

That sounds like it will work. I'll try it this morning.

redjr...

Edit. Sorry Wrangler. Should of addressed to Gaev.
Last edited by redjr on Thu Jan 14, 2010 10:29 am, edited 1 time in total.
User avatar
redjr
 
Posts: 80
Joined: Tue Jan 06, 2009 2:49 pm
Location: NY

Postby redjr » Thu Jan 14, 2010 10:28 am

Gaev wrote:redjr:

If you don't want duplicates ... check for the presence of an entry in the ListBox using ListBoxFindItem ... if it returns a zero (i.e. not found), then add it to the List (ListBoxAddItem), otherwise ignore/discard it.

Gaev,

Just added that bit of code to test for dups for both ComboBoxes. Works like a charm. I'm getting an empty/blank first entry in my ComboBoxes so I need to go investigate that. Maybe an extra initial LF is getting inserted somewhere. Usage of this complete code snippet will be very useful later on.

redjr..
User avatar
redjr
 
Posts: 80
Joined: Tue Jan 06, 2009 2:49 pm
Location: NY

Postby redjr » Thu Jan 14, 2010 1:20 pm

Wrangler and Gaev,

I spoke to soon.... I discovered that my list being created for ListBoxB(ComboBox21) is not a subset of the overall list - just for that user. It dawned on me that I cannot create ComboBox21 until I have selected the user in ListBoxA(ComboBox6). That means the rest of my code and the population of ComboBox21 cannot be made until additional logic has been added to test for LinePart1=Selection1 (Selected User).

I'm still working on it, but keep coming up with only the last entry in my file showing up in ComboBox21.

Here my code so far;

Code: Select all
..Get the length of the ascii file and store in [flen] variable:
FileLen "[GProgPath]\MASTER_List.txt" "[flen]"

..Clear ComboBoxes

ListBoxDeleteItem "ComboBox6" "ALL"
ListBoxDeleteItem "ComboBox21" "ALL"

.. loop and read lines in file
Loop "1" "[flen]" "[xx]"
FileRead "[GProgPath]\MASTER_List.txt" "[xx]" "[Linedata]"
..Parse the line
StrParse "[Linedata]" "[#9]" "[LinePart]" "[LineNumParsed]"

..Check for Duplicate Items
ListBoxFindItem "ComboBox6" "[LinePart1]" "[Found]"
If "[Found]" ">" "0"
  ListBoxDeleteItem "ComboBox6" "[Found]"
  SetVar "[Found]" ""
EndIf

..Add Unique Items to Combo Boxes 6 and 21
ListBoxAddItem "ComboBox6" "0" "[LinePart1]"
..Test for LinePart1 = Selected User
If "[LinePart1]" "=" "[Selection1]"
    ListBoxFindItem "ComboBox21" "[LinePart2]" "[Found2]"
    If "[Found2]" ">" "0"
       ListBoxDeleteItem "ComboBox21" "[Found2]"
    SetVar "[Found2]" ""
    EndIf
    ListBoxAddItem "ComboBox21" "0" "[LinePart2]"
EndIf
EndLoop


It's the last bit of code above I'm stuck on.

Ideas?

redjr...
User avatar
redjr
 
Posts: 80
Joined: Tue Jan 06, 2009 2:49 pm
Location: NY

Postby Wrangler » Thu Jan 14, 2010 1:43 pm

First, let's clean up some unnecessary code in ..Check for Duplicate Items:

..Get the length of the ascii file and store in [flen] variable:
FileLen "[GProgPath]\MASTER_List.txt" "[flen]"

..Clear ComboBoxes
ListBoxDeleteItem "ComboBox6" "ALL"
ListBoxDeleteItem "ComboBox21" "ALL"

.. loop and read lines in file
Loop "1" "[flen]" "[xx]"
FileRead "[GProgPath]\MASTER_List.txt" "[xx]" "[Linedata]"
..Parse the line
StrParse "[Linedata]" "[#9]" "[LinePart]" "[LineNumParsed]"

..Check for Duplicate Items
ListBoxFindItem "ComboBox6" "[LinePart1]" "[Found]"

.. check if data exists in listbox
If "[Found]" "=" "0"
..Add Unique Items to Combo Boxes 6 and 21
ListBoxAddItem "ComboBox6" "0" "[LinePart1]"
..Test for LinePart1 = Selected User
If "[LinePart1]" "=" "[Selection1]"
ListBoxFindItem "ComboBox21" "[LinePart2]" "[Found2]"
If "[Found2]" ">" "0"
ListBoxDeleteItem "ComboBox21" "[Found2]"
SetVar "[Found2]" ""
EndIf
ListBoxAddItem "ComboBox21" "0" "[LinePart2]"
EndIf
EndIf

Endloop

Note that:

ListBoxDeleteItem "ComboBox6" "[Found]"
SetVar "[Found]" ""

isn't necessary. The revised code above will only add an item to the listbox if it's not found, so it's unnecessary to delete the item or set any vars. The variable [found] will either contain the line data, or "0". You may have to work some on "..Test for LinePart1 = Selected User" part.

I'm on the road right now, so difficult to always access internet. Play with the code above and post any problems. Always good to use the debugger, slowing down the code execution and watching code and variables. Just hit pause when you suspect you're in the right place. I've solved a lot of problems this way.

Hang in. You're almost there!
Wrangler
--------------
"You never know about a woman. Whether she'll laugh, cry or go for a gun." - Louis L'Amour

Windows 7 Ultimate SP1 64bit
16GB Ram
Asus GTX 950 OC Strix
Software made with NeoBook
http://highdesertsoftware.com
User avatar
Wrangler
 
Posts: 1507
Joined: Thu Mar 31, 2005 11:40 pm
Location: USA

Postby redjr » Thu Jan 14, 2010 4:13 pm

Wrangler,

Thanks for your help and patience - and helping me cleanup my code.

Okay, before you replied I had an epiphany and realized the code I was trying to construct could not possibly work correctly - at least not where I was executing it from. For starters, I had it on the 'Startup' tab of the Book properties! So, it was only getting executed once. To make matters worse, while I did mention yesterday that I needed ComboBoxB to change dynamically with the selection of ComboBoxA, that can't happen unless the second part of the code is placed on the 'Selection Changed tab of the ComboBoxA. Duh!!

So along with your suggestions for my code changes, I ended up revamping several pages to get the code located correctly and this bit of user logon variables displayed properly in the picklist comboboxes. All seems to be well for the moment with this part of my app. It's by no means close to being done, but you brought me a long way in understanding principles of StrParse and correct usage. Thanks!

To make matters a bit more exciting, this project I'm working on is version 2 of an app I created last year, to adapt it to a multi-project environment. Along with those code changes, I have adopted H.P. Wickern's TabControlX utility. This does funny things with pages so you have to create them according to how you want the tabs to appear. I mocked this up last year, and for the most part they are operational. I do have much code that needs to be altered, but the basic 'Tab' metaphor appears to be working and the user like the new design.

Anyway, thanks again for your assistance. I've got both ComboBoxes working just the way I wanted them to. Pretty slick.

redjr...
User avatar
redjr
 
Posts: 80
Joined: Tue Jan 06, 2009 2:49 pm
Location: NY

PreviousNext

Return to NeoBook Action Commands

Who is online

Users browsing this forum: No registered users and 1 guest