using split function twice on same line 
Author Message
 using split function twice on same line

I have a CSV file, with the "heading" row (i.e. column names from a
spreadsheet) in the format of:

grp1:item1, grp1:item2, grp1:item3, grp2:item4, grp2:item5, ...

and so on for 20+ categories (the "grp") and 200+ items.
[Note: the data itself does not have any delimiter, just the
headings.]

I want to eventually get this into XML format like:
<grp1>
  <item1>data</item1>
  <item2>data</item2>
  <item3>...
<grp2>
  <item4>data</item4>
  <item5>...
... and so on.

So, I am trying to initially shift off and split the header row on
both the comma, and the colon, and then process the data using just
commas for separators.  I guess, but haven't got this far yet, that it
would be in a hash of a hash that will hold the end result:
{grp1 => {item1 => data}, {item2=>data}}

My initial question is in regards to the split() function.  Is this
the best way to "double" parse the header row.



Mon, 08 Nov 2004 02:47:20 GMT  
 using split function twice on same line

Im unfamiliar with CSV, but if your looking to get that string in a
hash like you noted...

my $line = "grp1:item1, grp1:item2, grp1:item3, grp2:item4,
grp2:item5"; #whatever

my %hash;

while($line =~ /(\w+):(\w+),?) {
    $hash{$1}{$2}++;

Quote:
}

Im sure there is a way to do it using split using a similar exp. Maybe
one of the seasoned vets here can show-n-tell :)

xgunnerx

Quote:
>I have a CSV file, with the "heading" row (i.e. column names from a
>spreadsheet) in the format of:

>grp1:item1, grp1:item2, grp1:item3, grp2:item4, grp2:item5, ...

>and so on for 20+ categories (the "grp") and 200+ items.
>[Note: the data itself does not have any delimiter, just the
>headings.]

>I want to eventually get this into XML format like:
><grp1>
>  <item1>data</item1>
>  <item2>data</item2>
>  <item3>...
><grp2>
>  <item4>data</item4>
>  <item5>...
>... and so on.

>So, I am trying to initially shift off and split the header row on
>both the comma, and the colon, and then process the data using just
>commas for separators.  I guess, but haven't got this far yet, that it
>would be in a hash of a hash that will hold the end result:
>{grp1 => {item1 => data}, {item2=>data}}

>My initial question is in regards to the split() function.  Is this
>the best way to "double" parse the header row.



Mon, 08 Nov 2004 10:23:33 GMT  
 using split function twice on same line

Whoops messed up that paste :)

while($line =~ /(\w+):(\w+),?/g) {
    $hash{$1}{$2}++;

Quote:
}

>Im unfamiliar with CSV, but if your looking to get that string in a
>hash like you noted...

>my $line = "grp1:item1, grp1:item2, grp1:item3, grp2:item4,
>grp2:item5"; #whatever

>my %hash;

>while($line =~ /(\w+):(\w+),?) {
>    $hash{$1}{$2}++;
>}

>Im sure there is a way to do it using split using a similar exp. Maybe
>one of the seasoned vets here can show-n-tell :)

>xgunnerx

>>I have a CSV file, with the "heading" row (i.e. column names from a
>>spreadsheet) in the format of:

>>grp1:item1, grp1:item2, grp1:item3, grp2:item4, grp2:item5, ...

>>and so on for 20+ categories (the "grp") and 200+ items.
>>[Note: the data itself does not have any delimiter, just the
>>headings.]

>>I want to eventually get this into XML format like:
>><grp1>
>>  <item1>data</item1>
>>  <item2>data</item2>
>>  <item3>...
>><grp2>
>>  <item4>data</item4>
>>  <item5>...
>>... and so on.

>>So, I am trying to initially shift off and split the header row on
>>both the comma, and the colon, and then process the data using just
>>commas for separators.  I guess, but haven't got this far yet, that it
>>would be in a hash of a hash that will hold the end result:
>>{grp1 => {item1 => data}, {item2=>data}}

>>My initial question is in regards to the split() function.  Is this
>>the best way to "double" parse the header row.



Mon, 08 Nov 2004 10:24:44 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Using SPLIT function with a Period

2. Using a period as a delimiter in the split() function

3. perlcc does not compile when split to array function is used

4. Using a period as a delimiter in the split() function

5. Splitting a line partially into subarrays / Using splice ?

6. How to use split function to split on a backslash

7. Problem with join function (and split function)

8. split a long line into many lines

9. PERLFUNC: split - split up a string using a regexp delimiter

10. Using split to return a @ without splitting on spaces

11. PERLFUNC: split - split up a string using a regexp delimiter

12. Writing lines to file twice or more (newbie question)

 

 
Powered by phpBB® Forum Software