< Your comment is incorrect. s/^\s+//g removes leading whitespace, the /g option is superfluous I agree on that.
< If there is no empty line at the end of the file then the last record will be skipped. Either put a print_line($hash) after the loop ends or test for eof here. good catch ________________________________ From: John W. Krahn <[email protected]> To: Perl Beginners <[email protected]> Sent: Wednesday, December 21, 2011 12:25 PM Subject: Re: parsing data Binish A.R wrote: > 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 Your comment is incorrect. s/^\s+//g removes leading whitespace, the /g option is superfluous because the pattern is anchored at the beginning of the string. s/\s+$//g removes trailing whitespace, the /g option is superfluous because the pattern is anchored at the end of the string. Any whitespace between the beginning and the end of the string is not touched. Also, chomp removes the newline at the end of the string, as does s/\s+$//g so you don't really need the chomp. > if (/^$/) { If there is no empty line at the end of the file then the last record will be skipped. Either put a print_line($hash) after the loop ends or test for eof here. > 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. John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] http://learn.perl.org/
