FlashMX wrote:
> Got it so far.
> 
> open CHECKFILE, "< file.in" or die qq(Cannot open file "file.in": $!);
>       @filecontent = <INFILE>;
>       foreach $line (@filecontent) {
>               print " $line";
>       }
> close (INFILE);

This is not a good approach for processing a file. Don't slurp the whole
file into an array unless you have to. If you're doing line-by-line
processing, just read a line at a time.

The usual idiom is:

   open F, "< file.in" or die $!;
   while(<F>) {
       # ... do something with the line in $_
   }


> 
> 
> The input file (list of files) looks something like this:
> 
>   P: 1                N: 11222
>   P: 2                N: 22333
>   P: 3                N: 33444
>   P: 4                N: 44555
> 
> How can I output just from the above code?
> 
> 11222
> 22333
> 33444
> 44555

Inside the loop body above, $_ will hold the entire line, something like

   "P: 1                N: 11222\n"

Note that the line terminator is there unless you use chomp().

So, you need to extract the "11222" from there. Several ways to do it. Some
that come to mind are: regular expressions, split(), substr(), and unpack().

split() can give you the last token on the line easily enough:

   (split)[-1]

So inside the loop body, you could do something like:

   print +(split)[-1], "\n";

There are lots of other ways to approach it. Which one is best depends on
the characteristics of your data. Lots of folks will probably recommend a
regex approach over split.

HTH

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to