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

I have the data file linecount.txt which contain the following :
hi.pl ~ 6454 low.pl ~ 757 cow.pl ~ 43 low.pl ~ 757
I want to read that file and catch the first low.pl and set
my $file = low.pl; my $lineCount = 757;
I am doing something like this
open(INFILE, "linecount.txt") my @hold; while (<INFILE>) { push(@hold, $1)&& last if (m/$thefileIamLookingfor/); } $value = join(' ', @hold); my( $file, $linecount) = split(/~/, $value);
however, I don't get the value , it keeps returning null .. thanks for help or ideas.

Replies are listed 'Best First'.
Re: Regex problem
by chromatic (Archbishop) on Jul 16, 2002 at 04:57 UTC

    Unless $thefileIamLookingfor contains an expression with capturing parenthesis, you're not capturing anything. I'm not sure if you're better capturing something, or pushing $_. Pick one. :)

Re: Regex problem
by zejames (Hermit) on Jul 16, 2002 at 05:13 UTC
    Hello,

    here is how I would do it :
    $filename = 'cow.pl'; undef $/; my ($file, $lineCount) = (<DATA> =~ m/^(\Q$filename\E) ~ (\d+)$/m); __DATA__ hi.pl ~ 6454 low.pl ~ 757 cow.pl ~ 43 low.pl ~ 757

    HTH

    --
    zejames
      thanks ;;)
Re: Regex problem
by Abigail-II (Bishop) on Jul 16, 2002 at 10:08 UTC
    What a piece of garbage code! What on earth are you doing with @hold? Just do something like:
    open my $fh => "linecount.txt" or die "Open linecount.txt: $!"; my ($file, $linecount); while (<$fh>) { ($file, $linecount) = split /\s*~\s*/; last if $file && $file eq $thefileIamLookingfor; }

    Abigail

      thanks for the help ,, my code is not garbage ,, it works somtimes :)