in reply to $1 into an array

Here is some slightly modified code that should work better.
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?

while (<DPSLOTFILE>){ next unless /\QMSL6000 Trinity:\s*(\d+)/; print OUTFILE $1,$/; }
or this could be reduced to one line: perl -nle 'print $1,$/ if /\QQMSL6000 Trinity:\s*(\d+)/' input_file > output_file;

-enlil

Replies are listed 'Best First'.
Re: Re: $1 into an array
by jamen_98 (Initiate) on Mar 26, 2004 at 19:40 UTC
    I tried what you had stated above and could not get it
    to work.  The file looks like:
    
    Label Location Pool Name Protection [NFK100L2] [MSL6000 Trinity: 1] MSL6000 01/19/2004 [NFK101L2] [MSL6000 Trinity: 5] MSL6000 01/19/2004
    Many times it will only be 1 line long of data but it could be up to three. I need to extract the 1 and 5 and put it into a new file. Currently I only need to pull the first line, but in the near future I need unlimited. This code works great for one line. I like the idea of just reading the line and then outputting it directly into the file, however when doing two lines, whether I use the \n or not, I only end up with the very last line (ie: 5). The code that works beautifully for one line is:
    #!/usr/bin/perl -w use strict; system ("c:\\Perl\\DataPro\\convert.bat"); open(DPSLOTFILE, "< c:\\Perl\\DataPro\\newreport.txt") or die "Can't o +pen file: $!"; open(OUTFILE, "> c:\\Perl\\DataPro\\output.txt") or die "Can't open fi +le: $!"; while ( <DPSLOTFILE> ) { my $dpdrivloc = "MSL6000 Trinity"; if ( /\Q$dpdrivloc\E:\s*(\d+)/ ) { print OUTFILE "$1"; } } close OUTFILE; close DPSLOTFILE; open (OUTFILE, "< c:\\Perl\\DataPro\\output.txt") or die "Can't open f +ile: $!"; my $slotnumber = <OUTFILE>; print "$slotnumber"; system("c:\\Perl\\DataPro\\omnimm -eject \"MSL6000 Trinity\" $slotnumb +er -location \"blahblahblah\""); close OUTFILE;
    Any comments on how to edit this to write out more than one slot number to the out file is appreciated! Thanks! Ben