On Fri, Jan 10, 2003 at 10:22:37PM -0500, David Nicely wrote:
> Hello,

Hi David,

> I am a total beginner, so any help or a pointer to an appropriate doc
> will be appreciated.
> 
> I am trying to read a file, and find all the lines that look like;
> "Finding 111" where "111" could be any number with any amount of
> digits.
> 
> What I am trying to do now is to assign a variable to that number for
> later use. or even better; gather all the numbers (from those specific
> lines only) and put them into an array.

If that log file is (or could possibly ever be) large, you don't want to
read everything into one variable. Instead, loop through one line at a
time:


        #!/usr/bin/perl
        use strict;
        use warnings;

        my $logfile = 'logfile.whatever';
        my @numbers;

        open(LOG, $logfile) or die "Couldn't open $logfile: $!\n";

        # loop through each line, assigning $line as we go
        while( my $line = <LOG> ){
                # test for the pattern
                if( $line =~ /Finding (\d+)/ ){
                        # $1 contains the first captured string
                        push(@numbers, $1);
                }
        }
        close(LOG);

        # print out all the captured numbers
        map{ print "$_\n"; } @numbers;


Hope that helps,
-- 
Michael
[EMAIL PROTECTED]
http://www.jedimike.net/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to