If you can guarantee the order in which the keys appear, you may not have to 
build a hash to hold the entire data.
Instead you can read block by block and print the result.

--
my $csno;
while (<>) {
        chomp;
        s/^\s+//g; s/\s+$//g;                     ### weed out all whitespaces
        if (/^$/) {
                print_line($hash);
                $hash = undef;
                next;
        }
        my ($k, $v) = split(/=/, $_, 2);             ### get the kvp
        $csno = $v, next if ($k =~ /csno/);     ## capture csno here
        $hash->{$csno}->{$k} = $v;              ### Assign kvp to last known 
csno
}
--

print_line is something you have to write to pretty print your output.

 
<img src="http://www.gnome.org/friends/banners/associate.png"; alt="Become a 
Friend of GNOME" border="0" />


________________________________
 From: Chris Stinemetz <chrisstinem...@gmail.com>
To: beginners@perl.org 
Sent: Wednesday, December 21, 2011 6:46 AM
Subject: parsing data
 
Hello list,

I am having some trouble coming up with a solution for what I am
trying to accomplish.

Any advice is greatly appreciated.

A sample of the data I would like to parse is below:

I am trying to build a table like structure where the string to the
left of the "=" sign is the header and the value to the right of the
"=" is the data value. I want to make sure to account for null value
(no value to the right of the "=")

Each new group begins with >csno and ends with a new line. I am
thinking that I am going to need to create a hash and possibly
incorporated regex, but I am not real sure how to begin this task.

I am trying to obtain the following format with the input data:

*** desired output ***
csno    rfpi    vrp0    vrp1    trl    line    low    high
1    1    3423000    3423000    1700000            5
1    2    3423000    3423000    1700000            5
1    3    3423000    3423000    1700000            5


*** input ***
>csno=1
rfpi=1
vrp0=3423000
vrp1=3423000
trl=1700000
line=
low=
high=5

>csno=1
rfpi=2
vrp0=3423000
vrp1=3423000
trl=1700000
line=
low=
high=5

>csno=1
rfpi=3
vrp0=3423000
vrp1=3423000
trl=1700000
line=
low=
high=5

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/

Reply via email to