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


In reply to Re: $1 into an array by Enlil
in thread $1 into an array by jamen_98

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.