Reputation: 105
I have data.txt with data as shown below. I have around hundred names with 3 constant ids 100, 200, 300 and each have 32 data values.
NAME: xyz
ID: 100
DATA: 10 15 99 13 ...
ID: 200
DATA: 23 45 78 90..
ID: 300
DATA: 45 67 89 56
NAME: abc
ID: 100
DATA: 2 4 787 8..
ID: 200
DATA: 12 14 17..
ID: 300
DATA: 45 34 22..
I need to write this data to another file which looks like
xyz_100, xyz_200,xyz_300,abc_100,...
10 , 23 , 45 ,2
15 , 45 ,67 ,4
I build a hash to store the values, but right now my code overwrites the first two entries and store the last entry. How can I save the first two entries too, please let me know if I can simplify the code.
#!/usr/local/bin/perl
use diagnostics;
use strict;
use warnings;
my @avar_names;
my %record;
local $/ = '';
open my $fh, '<', 'datalog.dat' or die "failed: $!";
while (<$fh>) {
chomp;
my %avar;
while (/^([A-Z:]+):\s*(.*)/mg) {
$avar{$1} = $2;
}
my $avar_name = "$avar{NAME}_$avar{ID}";
push @avar_names, $avar_name;
$record{$avar_name} = $avar{DATA};
use Data::Dumper;
print Dumper \%record;
}
Upvotes: 2
Views: 94
Reputation: 28840
There is a consistency problem: the local
line makes the first while process the whole NAME/ID data as a whole (then, thanks to the while(//mg)
loop), but the code as it is expects a line by line processing.
I suggest simplicity: process the file lines one by one, for each one, identify a "NAME/ID" couple (ID could be DATA) thanks to a regex on the current line.
In order for that to work, %avar
needs not to be local to the while
on <$fh>
, since it has to keep track of the last NAME/ID reference it got from the last lines of the file.
The @avar_names
array is not used.
Finally, you want to print the resulting records at the end (I supposed), but that is your choice.
This is your program from which I tried to change only the things mentioned above:
#!/usr/local/bin/perl
use diagnostics;
use strict;
use warnings;
my @avar_names;
my %record;
#local $/ = ''; ## commented out
open my $fh, '<', 'datalog.dat' or die "failed: $!";
my %avar; ## moved here
while (<$fh>) {
chomp;
if (m/^([A-Z]+):\s*(.*)/) { ## if, not a while
$avar{$1} = $2;
## ensure we have at least our 3 keys, process only if we met DATA
if (exists($avar{NAME}) && exists($avar{ID}) && exists($avar{DATA}) && $1 eq 'DATA') {
my $avar_name = "$avar{NAME}_$avar{ID}";
push @avar_names, $avar_name; ## not used
$record{$avar_name} = $avar{DATA};
}
}
}
use Data::Dumper;
print Dumper \%record;
Output:
$VAR1 = {
'xyz_100' => '10 15 99 13 ...',
'xyz_300' => '45 67 89 56',
'abc_200' => '12 14 17..',
'abc_300' => '45 34 22..',
'abc_100' => '2 4 787 8..',
'xyz_200' => '23 45 78 90..'
};
Upvotes: 2
Reputation: 97958
You need something like this:
my @avars;
# ... some code
while (<$fh>) {
# more code
my %curr;
while (/^([A-Z:]+):\s*(.*)/mg) {
$curr->{$1} = $2;
}
if (%curr) {
push @avars, \%curr;
my $avar_name = "$curr{NAME}_$curr{ID}";
push @avar_names, $avar_name;
$record{$avar_name} = $avar{DATA};
}
# ....
Upvotes: 1