AutoHotkey Community

It is currently May 27th, 2012, 8:26 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 26 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: February 7th, 2012, 9:11 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
What's the best way to add rows to a list view when you have to use an embedded parsing loop? For example say I just read the contents of a csv. I want each line to have its own row in the list view, but each value that is separated by a comma to be in its own cell in that row. I've done a pretty lengthy work-around for this in the past, but I just struggle with this type of thinking and need a better way to do it.

Code:
Gui, Add, ListView, w1000 r50 vdisplayLocation
Gui, Add, Button, w60, Close
Gui, Show

FileRead, csvFile, C:\SomePath\SomeFile.csv
Loop, Parse, csvFile, `n ; parse each line
{
   Loop, Parse, A_LoopField, `, ; now parse each value
   {
      ; What goes here? ; how can I smoothly get it into the LV?
   }
}
return

GuiClose:
ButtonClose:
ExitApp

_________________
-Jeremiah


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 9:22 pm 
First tip: Never parse on a , or stringsplit will fail at some point for more complex CSV
Second tip: http://www.autohotkey.com/wiki/index.php?title=FAQ#CSV (see 4th link with example. Although CSV Lib has a function to show a CSV in a listview directly)
Third tip: dunno what you want to do, but if your just browsing/searching your data give expermental app csvqf a whirl http://www.autohotkey.com/forum/topic73344.html


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 9:23 pm 
To add on the first tip: use the CSV option of Loop, parse (see doc)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 9:25 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
Anonymous wrote:
To add on the first tip: use the CSV option of Loop, parse (see doc)


I saw that in the help file, but can't quite figure out the difference. Either way you're parsing commas. Are you not? And, how can that assist me with getting it into a listview?

EDIT:
More-so, say it wasn't a csv, say it was a hyphenated file? Then how can I accomplish this?

_________________
-Jeremiah


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 9:29 pm 
Code:
;FileRead, csvFile, C:\SomePath\SomeFile.csv
csvFile=
(
mango,apple
cog,cat,monkey
wind,water,fire,earth
cog,cat,monkey
mango,apple
)

Gui, Add, ListView, w1000 r50 vdisplayLocation, A|B|C|D
Gui, Add, Button, w60, Close

Loop, Parse, csvFile, `n ; parse each line
{
   StringSplit, f, A_LoopField,  `,
   LV_Add("",f1,f2,f3,f4)   ; and more if neccesarry
   Loop % f0            ; clear
      f%A_Index% := ""
}
Loop, 4
LV_ModifyCol(A_Index, "Auto")
Gui, Show
return

GuiClose:
ButtonClose:
ExitApp


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 9:32 pm 
Re CSV because it is a "complex" format,

simple:

1,2,3
4,5,6

"complex" and this is where it goes wrong if you parse / split on a ,

1,"2,5",3
4,5,6

you see the 2,5 cell? that will break your , or split the first would suddenly have 4 columns instead of 3.

doesn't apply to other chars of course, unless it is a CSV like format which allows for "escaping" as the "2,5" cell above.

your example "fixed"
Code:
csvFile=
(join`r`n
1,2,3
4,5,6
)

Gui, Add, ListView, w1000 r50 vdisplayLocation,col1|col2|col3
;FileRead, csvFile, C:\SomePath\SomeFile.csv
Loop, Parse, csvFile, `n, `r ; parse each line
{
   Loop, Parse, A_LoopField, CSV ; now parse each value
      Col%A_Index%:=A_LoopField
    LV_Add("", Col1,Col2,Col3)
}
Gui, Add, Button, w60, Close
Gui, Show
return


GuiClose:
ButtonClose:
ExitApp


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 9:33 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
L1 wrote:
Code:
;FileRead, csvFile, C:\SomePath\SomeFile.csv
csvFile=
(
mango,apple
cog,cat,monkey
wind,water,fire,earth
cog,cat,monkey
mango,apple
)

Gui, Add, ListView, w1000 r50 vdisplayLocation, A|B|C|D
Gui, Add, Button, w60, Close

Loop, Parse, csvFile, `n ; parse each line
{
   StringSplit, f, A_LoopField,  `,
   LV_Add("",f1,f2,f3,f4)   ; and more if neccesarry
   Loop % f0            ; clear
      f%A_Index% := ""
}
Loop, 4
LV_ModifyCol(A_Index, "Auto")
Gui, Show
return

GuiClose:
ButtonClose:
ExitApp


Not bad, but say I don't know how the length of the array (f). How do you LV_Add then?

_________________
-Jeremiah


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 9:33 pm 
Sorry this is error prone
Code:
StringSplit, f, A_LoopField,  `,
don't use it unless you are 100% sure you will have only a very basic csv input.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 9:35 pm 
Jeremiah wrote:
Not bad, but say I don't know how the length of the array (f). How do you LV_Add then?
See the links I gave earlier. Basically you cound the columns BEFORE you write the gui listview line, and use a variable to define the columns. (and again don't use stringsplit)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 9:37 pm 
Offline

Joined: December 12th, 2011, 10:25 pm
Posts: 251
Something like this works, though you could probably tweak the code into a shorter one and also tweak it to not actually need to specify how many fields:

Code:
Gui, Add, ListView, w1000 r50 vdisplayLocation, Field1     | Field2     
Gui, Add, Button, w60, Close
Gui, Show

Loop, Read, C:\SomePath\SomeFile.csv ; parse each line
{
   Loop, Parse, A_LoopReadLine, `, ; now parse each value
   {
      Field%A_Index% = %A_LoopField%
   }
   TotalLines = %A_Index%
   Field1ofLine%A_Index% = %Field1%
   Field2ofLine%A_Index% = %Field2%
   ;....  goes on for as many fields youre trying to read.
}
Loop %TotalLines%
    Lv_Add("", Field1ofLine%A_Index%, Field2ofLine%A_Index%) ; the parameter of Lv_add also go on for as many fields as you have in each line.

return

GuiClose:
ButtonClose:
ExitApp


EDIT: Changed some lines. Should work now. Sorry for posting it untested before.


Last edited by G. Sperotto on February 7th, 2012, 10:04 pm, edited 5 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 9:39 pm 
Don't do this
Code:
Loop, Parse, A_LoopField, `, ; now parse each value
:D


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 10:13 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
Okay, either I asked it incorrectly, or everyone is misunderstanding me. Regardless of the delimiter, I need to add an unknown amount of columns and rows to the listview.

So, if I don't know the amount of columns that may be present, and I don't know the number of rows that will be present, I need to the loops. That we agree on.

By doing something like this:
Code:
Loop, Parse, csvFile, `n, `r ; parse each line
{
   Loop, Parse, A_LoopField, CSV ; now parse each value
      Col%A_Index%:=A_LoopField
    LV_Add("", Col1,Col2,Col3)
}


isn't going to work because I have no idea how many Cols there are. We know the variable Col%A_Index% will contain that figure, but I can't do an LV_Add("", Col1, Col2, Col3) because if there are 5 columns, or 10 columns, or 78 columns, I'll never know.

_________________
-Jeremiah


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 10:25 pm 
And you don't need to know (in advance).
Like I said count the columns before you create the Listview gui
The answer is in the links I provided earlier, really I know because I put them there ;-)

But you wanted to know so here goes
Code:
csvFile=
(join`r`n
1,2,3,a,b,c
4,5,6,d,e,f
)

Loop, Parse, csvFile, `n, `r ; parse each line
{
 Row:=A_Index
 Loop, Parse, A_LoopField, CSV ; now parse each value
  {
   Col_%Row%_%A_Index%:=A_LoopField
   MaxCol:=A_Index ; yes pretty useless but bare with me you don't want to seem to use the libraries
  }
}
 
Loop, % MaxCol ; yay here it is
   Header .= "c" A_Index "|" ; c is just something you can use anything you like
StringTrimRight, header, header, 1   

Gui, Add, ListView, w1000 r50 vdisplayLocation,%header%
Loop, % Row ; from above
   {
    PutRow:=A_Index
    Loop, % MaxCol
     {
      Lv_Add("")
        LV_Modify(PutRow, "Col" . A_Index, Col_%PutRow%_%A_Index%)
       }
   }
   
Gui, Add, Button, w60, Close
Gui, Show
return


GuiClose:
ButtonClose:
ExitApp

Using one of the libraries will simplify your life but you probably don't want that ;-)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 10:56 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
Anonymous wrote:
Using one of the libraries will simplify your life but you probably don't want that ;-)


It's not that I don't want to use them, I want to learn how it's done. I'd rather know how my car works before I drive it. That way, I can properly troubleshoot any issues I may have.

Plus, isn't that library for CSV files only?

_________________
-Jeremiah


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2012, 11:12 pm 
There are two libs, one called csv, one table, but you can set the delimiter meaning it can be a , a tab a space a ; a "whatever you want it to be"

DSV would be a more proper name, "Delimiter Seperated Values"

Study the code to learn more, its all in there, you don't have to use it but you can learn from it and become a proper Delimiter Data Parser Mechanic ;-)


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 26 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, Google Feedfetcher, hyper_, immunity, sjc1000 and 76 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group