Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

how to initiate an array or an hash with name from captured string in pattern matching?

by hihilo (Initiate)
on Nov 14, 2010 at 13:31 UTC ( #871316=perlquestion: print w/replies, xml ) Need Help??
hihilo has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I'm trying to parse long text strings to upload into various tables in a database, nd have managed to break down the strings into pieces such as FIL.date=2009-0707, SPE.reference_channel=4.097e+03, etc, whereby FIL, SPE, etc are the names of the tables in the database, date, reference_channel, etc are column headers in appropriate tables, and the values following "=" are meant for each column.

I thought it'd be easier to upload data to the database if entries to each table are organized into an hash while parsing the strings. To that end, I have the following, so far:

open (my $fd, '<','./dump2.txt') or die "Can't open dump2.txt"; while (<$fd>) { chomp; my @dump = split(/\t/); my $tbl_last = "FIL"; foreach my $it (@dump) { if ($it =~ m/([A-Z]{3})\.(.+)/) { my $tbl = $1; my ($col, $val) = split('=',$2); # print "$it\t$tbl\t$col\t$val\n"; my @tmp = (); if ($tbl eq $tbl_last) { push(@tmp,$col,$val); } else { my %$tbl = (@tmp); $tbl_last = $tbl; print Dumper \%$tbl; } } } }

It didn't like %$tbl. I tried %($tbl) and %"$tbl", all to noavail. Perhaps this is the wrong approach to the problem? Any suggestions/advice appreciated.

here's the first line of the dump2.txt file:

FIL.date=2009-07-07 FIL.file_name="/opt/uip/data/class/55019.cso" + INH.sca n_number=1 INH.obs_number=1 INH.scan_type=5 INH.u +t_date_tim e="2009-07-07 04:18:55" INH.azimuth=9.0150894e+01 INH.elevation +=5.9999599 e+01 INH.tau_225=5.5000000e-02 MAP.ra_offset=0.0000000e+00 + MAP.dec _offset=0.0000000e+00 MAP.az_offset=0.0000000e+00 MAP.el_offset +=-0.000000 0e+00 MAP.gl_offset=0.0000000e+00 MAP.gb_offset=0.0000000e+00 + SOU.sou rce_name="Freq Cal" SOU.ra_epoch=0.0000000000000000e+00 SOU.d +ec_epoch=0 .0000000000000000e+00 SOU.epoch=0.0000000e+00 SOU.velocity= +-5.7749530 e-13 SOU.velocity_type=0 SOU.off_mode=0 SOU.off_x=0 SOU.o +ff_y=0 SPE.spectrometer_number=5 SPE.receiver_number=0 SPE.line_name +="12co2-1" SPE.rest_frequency=2.3053797000000000e+02 SPE.lock_freq +uency=0.00 00000000000000e+00 SPE.if_frequency=0.0000000000000000e+00 + SPE.sid eband=0 SPE.harmonic=0 SPE.frequency_offset=0.0000000000000000e+00 + SPE.vel ocity_offset=0.0000000000000000e+00 SPE.if_offset=0.0000000000000 +000e+00 SPE.bandwidth=1.3311705e+03 SPE.number_channels=2048 SPE.r +eference_c hannel=9.2074707e+02 SPE.system_temperature=1.0000000e+00 SPE.o +n_time=1.0 156640e+00 SPE.off_time=0 TEL.fixed_az_offset=0.0000000e+00 + TEL.fix ed_el_offset=-0.0000000e+00 TEL.focus_offset=0 TEL.y_offset= +0 TEL.sec ondary_x=0 TEL.secondary_y=0 TEL.secondary_z=0 TEL.s +econdary_t heta=0 TEL.ut_date_time="2009-07-07 04:18:55"
  • Comment on how to initiate an array or an hash with name from captured string in pattern matching?
  • Select or Download Code

Replies are listed 'Best First'.
Re: how to initiate an array or an hash with name from captured string in pattern matching?
by ig (Vicar) on Nov 14, 2010 at 16:52 UTC

    A hash of hashes is probably what you should be using. Maybe something like:

    open (my $fd, '<','./dump2.txt') or die "Can't open dump2.txt"; while (<$fd>) { chomp; my @dump = split(/\t/); my $tbl_last = "FIL"; my %data; foreach my $it (@dump) { if ($it =~ m/([A-Z]{3})\.(.+)/) { my $tbl = $1; my ($col, $val) = split('=',$2); # print "$it\t$tbl\t$col\t$val\n"; $data{$tbl}->{$col} = $val; } } print Dumper(\%data);

    You can learn more about such data structures by reading perldsc.

      Indeed, this does it very nicely! Thanks for the tip. Will read on perldsc.

Re: how to initiate an array or an hash with name from captured string in pattern matching?
by Generoso (Parson) on Nov 14, 2010 at 14:29 UTC

    It will be easier to understand what you are trying to do if you could give a small sample of the dump2.txt file

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://871316]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-04-24 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?