Flagging something as "WRONG", without providing a reason, is not particularly useful.
"push @{ ARRAYREF }, LIST" was probably the syntax you wanted; however, that says nothing about whether the logic or functionality is "RIGHT" or "WRONG", nor what those terms mean in either context.
Hashes have no inherent ordering. Stating "When using only the hash, the output is ..." is meaningless.
If you need to keep track of the order of hash keys, you have to do it yourself and an array is usually the best tool for this task. See keys for a discussion of this; there's a lot more detail in the Hash Algorithm section of "perlsec: Algorithmic Complexity Attacks".
Before writing any more code, I'd recommend that you look at "perlreftut - Mark's very short tutorial about references", "perldsc - Perl Data Structures Cookbook" and "perllol - Manipulating Arrays of Arrays in Perl".
Next consider what data you need to collect and how you're going to use it. That should provide insight into how to store and subsequently access the data.
Storing the data without sufficient forethought, and then finding you have to go back to square one and start all over again, is a waste of time, effort and money (not to mention just plain, old boring).
Here's a possible approach you could take:
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use Data::Dumper;
my @out;
open my $in_fh, '<', 'pm_1054339_data.txt';
while (<$in_fh>) {
chomp;
my ($key, $value) = split /=/;
for ($key) {
/component/ && do { push @out, { $key => $value }; last };
/version/ && do { $out[-1]{$key} = $value; last };
/sourcefile/ && do { push @{$out[-1]{order}}, $value; last };
/sourcesum/ && do { $out[-1]{sources}{$out[-1]{order}[-1]} =
+$value };
}
}
close $in_fh;
print Dumper \@out;
With this input:
$ cat pm_1054339_data.txt
component=HF
version=NULL
sourcefile=filename1
sourcesum=checksumfile1
sourcefile=filename2
sourcesum=checksumfile2
sourcefile=filename3
sourcesum=checksumfile3
component=SVM
version=10.0.70.102
sourcefile=filename4
sourcesum=checksumfile4
component=HF_2
version=NULL
sourcefile=file10
sourcesum=checksum10
sourcefile=file2
sourcesum=checksum2
sourcefile=file3
sourcesum=checksum3
You get this output:
$ pm_1054339_data.pl
$VAR1 = [
{
'order' => [
'filename1',
'filename2',
'filename3'
],
'sources' => {
'filename3' => 'checksumfile3',
'filename1' => 'checksumfile1',
'filename2' => 'checksumfile2'
},
'version' => 'NULL',
'component' => 'HF'
},
{
'version' => '10.0.70.102',
'order' => [
'filename4'
],
'sources' => {
'filename4' => 'checksumfile4'
},
'component' => 'SVM'
},
{
'version' => 'NULL',
'sources' => {
'file10' => 'checksum10',
'file3' => 'checksum3',
'file2' => 'checksum2'
},
'order' => [
'file10',
'file2',
'file3'
],
'component' => 'HF_2'
}
];
|