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

I fairly new to perl and have a simple question in regards to why Im not getting the data back? I need FRAUD-REPORT01032006.CSV or any thing that matches that to be stored into $newfile.
foreach $filelist(@filelist) { if ($filelist =~ /FRAUD-REPORT\d+\.CSV/i) { $newfile = $1; push(@curr, $newfile); } sort(@curr); } print "@curr";
Thanks in advance....

Replies are listed 'Best First'.
Re: reg expression
by Zaxo (Archbishop) on Jun 14, 2006 at 15:54 UTC

    $1 is undefined because you don't have capturing parentheses in the regex.

    if ($filelist =~ /(FRAUD-REPORT\d+\.CSV)/i) { $newfile = $1; push(@curr, $newfile); }

    After Compline,
    Zaxo

      Awesome thanks man!!! :)
Re: reg expression
by ikegami (Patriarch) on Jun 14, 2006 at 16:04 UTC
    In addition to Zaxo's fix, you'll need to add ^ and $ if you want "FRAUD-REPORT###.CSV" to be the entire string.
    foreach my $file (@filelist) { push(@curr, $1) if $file =~ /^(FRAUD-REPORT\d+\.CSV)$/i; }

    Furthermore, you can simplify things a bit if you're trying to match the entire string (as per wazoox's post):

    foreach my $file (@filelist) { push(@curr, $file) if $file =~ /^FRAUD-REPORT\d+\.CSV$/i; }

    or just

    my @curr = grep /^FRAUD-REPORT\d+\.CSV$/, @filelist;
Re: reg expression
by wazoox (Prior) on Jun 14, 2006 at 15:54 UTC
    use strict; use warnings; will probably help you finding what's wrong. What's $1 supposed to be ? It looks like you're looking for $_ instead.
    use strict; use warnings; my @filelist=qw( FRAUD-REPORT01032006.CSV toto tata ); my @curr; foreach my $file ( @filelist) { push @curr, $file if $file =~ /FRAUD-REPORT\d+\.CSV/i } sort @curr; print "@curr";
Re: reg expression
by ysth (Canon) on Jun 14, 2006 at 18:56 UTC
    sort(@curr);
    This doesn't leave @curr sorted; sort just returns the sorted list. You want:
    @curr = sort(@curr);
    and presumably you want it after the for loop, not inside the loop.
      Actually, this would make more sense for the OP:
      print sort @curr;
      Nevermind. sorry.