use strict; use warnings; #open files here #my $counter = 0; my @slotarray; while ( <DPSLOTFILE> ) { my $dpdrivloc = "MSL6000 Trinity"; if ( /\Q$dpdrivloc\E:\s*(\d+)/ ) { #my $slotarray{$counter} = "$1"; #print OUTFILE "$1"; #$counter++; push @slotarray, $1; } } for (@slotarray) { #print OUTFILE "$slotarray\n"; print OUTFILE $_,$/; } #close files here
The immediate problems i see with your posted code are: my $slotarray{$counter} = "$1"; which won't work as you cannot put a my when trying to assign to a hash element; which won't work with the my in front of it. So this was changed. Second when you use for (@slotarray){ } you assign each element in @slotarray to $_ which is not the same as $slotarray and hence will print whatever was in $slotarray as many times as there are elements in @slotarray.Note that you could have also done the following:
for my $slotarray (@slotarray) { print OUTFILE "$slotarray\n"; }
If you are not going to be using @slotarray for anything other than printing it back out to OUTFILE why not just print while going through the while loop?
or this could be reduced to one line: perl -nle 'print $1,$/ if /\QQMSL6000 Trinity:\s*(\d+)/' input_file > output_file;while (<DPSLOTFILE>){ next unless /\QMSL6000 Trinity:\s*(\d+)/; print OUTFILE $1,$/; }
-enlil
In reply to Re: $1 into an array
by Enlil
in thread $1 into an array
by jamen_98
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |