liri has asked for the wisdom of the Perl Monks concerning the following question:

the logfile is in text format and contains something
like the following
Item no.0: NameDepth [2] <0> Class: [1] NetElementObjectClass Instance +: (int) [2] <1> Class: [ 57 ] Vc3TtpSnkObjectClass Instan +ce: (int) [3] Item no.1: NameDepth [2] <0> Class: [1] NetElementObjectClass Instance +: (int) [2] <1> Class: [ 57 ] Vc3TtpSnkObjectClass Instan +ce: (int) [14]
open (FILE, "< logfile") or die "Couldn't open for read logfile: $!"; while ($file_content = <FILE>) { if ( $file_content =~ m/Vc3/ ) { my @numbers = $file_content =~ /\[(\d+)\]/g; } } close (FILE);


the idea is to catch the integers between a [ ] inside
the loop so i can immidiately do something with them...


cant figure out why it isnt working. maybe im parsing it wrong.

Replies are listed 'Best First'.
Re: extracting integers from bracketes when reading from file...
by Tanktalus (Canon) on Nov 12, 2005 at 22:08 UTC

    Seems to work for me ... what are you getting, and what are you expecting, for the sample log file you're showing?

    #!/usr/bin/perl -w use strict; use warnings; while (my $file_content = <DATA>) { if ( $file_content =~ m/Vc3/ ) { my @numbers = $file_content =~ /\[(\d+)\]/g; { local $,=':'; print @numbers; print $/ } } } __DATA__ Item no.0: NameDepth [2] <0> Class: [1] NetElementObjectClass Instance +: (int) [2] <1> Class: [ 57 ] Vc3TtpSnkObjectClass Instan +ce: (int) [3] Item no.1: NameDepth [2] <0> Class: [1] NetElementObjectClass Instance +: (int) [2] <1> Class: [ 57 ] Vc3TtpSnkObjectClass Instan +ce: (int) [14]
    gives me:
    $ perl ./x.pl
    3
    14
    
    Seems right to me - are you expecting to get the 57 as well? If so, you need to tell the regexp to ignore spaces:
    my @numbers = $file_content =~ /\[\s*(\d+)\s*\]/g;
    Hope that helps.

Re: extracting integers from bracketes when reading from file...
by Aristotle (Chancellor) on Nov 12, 2005 at 22:11 UTC

    I’m not sure about whether you are asking any question at all, but it seems you need to allow some whitespace between the square brackets in the data and the digits:

    my @numbers = $file_content =~ /\[\s*(\d+)\s*\]/g;

    FWIW, I suggest using the /x option for the pattern, so that you can use whitespace to structure it:

    my @numbers = $file_content =~ / \[ \s* (\d+) \s* \] /gx;

    See perlretut in Building a regexp.

    Makeshifts last the longest.

Re: extracting integers from bracketes when reading from file...
by pg (Canon) on Nov 13, 2005 at 02:25 UTC

    If the problem was that you were not catching those 57's, it was because of those blanks within [], so just modify your regexp to allow it:

    use strict; use warnings; while (my $file_content = <DATA>) { if ( $file_content =~ m/Vc3/ ) { my @numbers = $file_content =~ /\[\s*(\d+)\s*\]/g;#modified { local $,=':'; print @numbers; print $/ } } } __DATA__ Item no.0: NameDepth [2] <0> Class: [1] NetElementObjectClass Instance +: (int) [2] <1> Class: [ 57 ] Vc3TtpSnkObjectClass Instan +ce: (int) [3] Item no.1: NameDepth [2] <0> Class: [1] NetElementObjectClass Instance +: (int) [2]

    This prints:

    57:3 57