in reply to Parsing record into hash

Your question isn't very clear but let's see what do you think about this:

open my $fd, '<', 'foo' or die open "$!\n"; my ($h, @arr); ## assuming <eor> and <eoh> are always at the end of their lines while (<$fd>) { $h .= $_; ## We store header in $h. I won't parse it. last if /<eoh>$/; } my $c = 0; while (<$fd>) { next if /^\n$/; my @fields = split / /, $_; foreach (@fields) { my ($k, $v) = $_ =~ /^(<[^>]+>)(.*)$/; next if $k eq '<eor>'; $arr[$c]{$k} = $v; } $c++ if /<eor>$/; } close $fd;

Output of use Data::Dumper;print Dumper \@arr;

$VAR1 = [ { '<time_on:6>' => '213400', '<app_jlog_eqsl_qsl_rcvd:1>' => 'Y', '<cont:2>' => 'NA', '<app_jlog_qso_number:4>' => '0001', '<app_jlog_eqsl_qsl_sent:1>' => 'Y', '<rst_rcvd:2>' => '59', '<qsl_sent_via:1>' => 'E', '<freq:2>' => '14', '<band:3>' => '20M', '<rst_sent:2>' => '59', '<cqz:1>' => '4', '<dxcc:1>' => '1', '<stx:1>' => '4', '<operator:3>' => 'VHF', '<pfx:3>' => 'VC3', '<ituz:1>' => '4', '<call:4>' => 'VC3O', '<qso_date:8:d>' => '20051029', '<app_jlog_lotw_qsl_sent:1>' => 'Y', '<srx:1>' => '4', '<mode:3>' => 'SSB', '<qsoComplete:1>' => '' }, { '<time_on:6>' => '183206', '<state:2>' => 'AB', '<cont:2>' => 'NA', '<contest_id:3>' => 'RAC', '<app_jlog_qso_number:4>' => '1257', '<app_jlog_eqsl_qsl_sent:1>' => 'Y', '<freq:8>' => '14.16299', '<rst_rcvd:2>' => '59', '<app_jlog_lotw_qslsdate:10>' => '2006-07-01', '<band:3>' => '20M', '<rst_sent:2>' => '59', '<cqz:1>' => '4', '<dxcc:1>' => '1', '<operator:3>' => 'MWB', '<stx:2>' => '27', '<pfx:3>' => 'VE6', '<operator:4>' => 'N7DQ', '<ituz:1>' => '2', '<srx:2>' => 'AB', '<qso_date:8:d>' => '20060701', '<app_jlog_lotw_qsl_sent:1>' => 'Y', '<app_jlog_eqsl_qslsdate:10>' => '2006-07-01', '<qsoComplete:1>' => '', '<mode:3>' => 'SSB', '<call:5>' => 'VE6GG' } ];

Update: Removed an intermediate step and did some other minor tweaks. Now it's smaller and probably faster.

--
David Serrano

Replies are listed 'Best First'.
Re^2: Parsing record into hash
by sxmwb (Pilgrim) on Jul 04, 2006 at 22:02 UTC
    Wow, Thank you, this what I am looking for the info between the <> is the key and the data follows. You make it look simple. Now I have to read through the code to really understand what just happened.

    Thanks Mike