> 
> 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);
> 
> 
> 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?
> 

Have you tried using a match operator?  Mastering the use of m// and
regexps in general is going to be critical path to enjoying Perl
programming.  

First:

Read: perldoc perlop

Second:

if (my ($name) = $line =~ m/.*N: (\d+)$/) {
    ...do something with $name....
}

That's complex, so let's look at that bit by bit:

if (...something...) { 
   ...do something with $name... 
}

That should be self-evident.  As to the ...something...

my ($name) = ...a match operation... 

Remember, that when called in a list context, match operators will
return (quoting from perlop) "a list consiting of the subexpressions
matched by the parentheses in the pattern i.e. ($1, $2, $3 .. )"

So, by putting the $name inside a parentheses, it has been 'promoted'
from a scalar to a list-of-one, forcing the match operation to be
evaluated in a list context.  As to the match operation ... 

$line =~ m/.*N: (\d+)$/

That says: 

    search the string $line for any number of characters of any flavor
    (could be zero), followed by a capital N, followed by a colon,
    followed by one blank, followed by (and remember this part) one or
    more digits, followed by the end-of-line.

So, that would match lines like:

My Mother Sucks Eggs N: 12345
         P: 1 N: 11111

But would not match lines like:

         P: 2 N: 22222c
The rain in spain falls mainly on the plain

So, if the match fails, it evaluates to undef, $name gets set to
undef, the if() block does not execute, no output is produced, nobody
gets soup.



-- 
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