capturing a match

Discussion in 'Perl Misc' started by Chris, Feb 3, 2004.

  1. Chris

    Chris Guest

    Below is a grepped list of data I want to parse in Perl. This was
    initially going to be done on a one-liner hence why grep was
    used.

    out.lis:
    profs/pr00001.clean:Query id = asni00107
    profs/pr00002.clean:Query id = asni00108
    profs/pr00003.clean:Query id = asni00109
    profs/pr00004.clean:Query id = asni00110
    profs/pr00005.clean:Query id = asni00111
    profs/pr00006.clean:Query id = asni00112
    profs/pr00007.clean:Query id = asni00113
    profs/pr00008.clean:Query id = asni00114
    profs/pr00009.clean:Query id = asni00115
    profs/pr00010.clean:Query id = asni00116

    From this list I need the 'pr000??' data and the 'asni00??' data
    from each line.

    The following script does what I want (ie. print
    'asni00107=pr00001'):

    #! /usr/bin/perl -w

    use strict;

    open (FILE, "out.lis") or die "Can't open file: $!\n";

    my %h;

    while (<FILE>) {

    my @F = split;
    print "4th field: $F[3]\n";
    $F[0] =~ /(pr\d{5})/;
    $h{$F[3]} = $1;
    }
    close(FILE);

    foreach my $k (sort keys %h) {
    print "$k=$h{$k}\n";
    }
    exit;

    But I wanted to do this:
    my $h{$F[3]} = $F[0] =~ /(pr\d{5})/;

    However, this gives 'asni00107=1'. How can I get the capturing to
    return the match rather than the number of matches? I've done
    this before with success and I'm sure I haven't missed anything
    obvious (have I?).
    Any pointers appreciated.

    Chris.
    Chris, Feb 3, 2004
    #1
    1. Advertising

  2. Chris

    Paul Lalli Guest

    On Tue, 3 Feb 2004, Chris wrote:

    > But I wanted to do this:
    > my $h{$F[3]} = $F[0] =~ /(pr\d{5})/;
    >
    > However, this gives 'asni00107=1'. How can I get the capturing to
    > return the match rather than the number of matches? I've done
    > this before with success and I'm sure I haven't missed anything
    > obvious (have I?).
    > Any pointers appreciated.
    >


    You need list context.

    my ($h{$F[3]}) = $F[0] =~ /(pr\d{5})/;

    Paul Lalli
    Paul Lalli, Feb 3, 2004
    #2
    1. Advertising

  3. Chris

    Brad Baxter Guest

    On Tue, 3 Feb 2004, Chris wrote:

    > But I wanted to do this:
    > my $h{$F[3]} = $F[0] =~ /(pr\d{5})/;
    >
    > However, this gives 'asni00107=1'. How can I get the capturing to
    > return the match rather than the number of matches? I've done
    > this before with success and I'm sure I haven't missed anything
    > obvious (have I?).
    > Any pointers appreciated.


    I think this is what you're after:

    ($h{$F[3]}) = $F[0] =~ /(pr\d{5})/;

    Or perhaps ...

    perl -F'\W+' -lane'print"$F[5]=$F[1]"' out.lis
    perl -F'\W+' -lape'$_="$F[5]=$F[1]"' out.lis

    Regards,

    Brad
    Brad Baxter, Feb 3, 2004
    #3
  4. [posted & mailed]

    On 3 Feb 2004, Chris wrote:

    >my $h{$F[3]} = $F[0] =~ /(pr\d{5})/;


    First of all, the 'my' will give you a syntax error.

    Anyway, just put the $h{...} in parentheses. This enforces list context
    on the right-hand side of the =.

    ($h{$F[3]}) = $F[0] =~ /(pr\d{5})/;

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
    Jeff 'japhy' Pinyan, Feb 3, 2004
    #4
  5. Chris <> wrote:

    > Below is a grepped list of data I want to parse in Perl. This was
    > initially going to be done on a one-liner hence why grep was
    > used.
    >
    > out.lis:
    > profs/pr00001.clean:Query id = asni00107
    > profs/pr00002.clean:Query id = asni00108
    > profs/pr00003.clean:Query id = asni00109
    > profs/pr00004.clean:Query id = asni00110
    > profs/pr00005.clean:Query id = asni00111
    > profs/pr00006.clean:Query id = asni00112
    > profs/pr00007.clean:Query id = asni00113
    > profs/pr00008.clean:Query id = asni00114
    > profs/pr00009.clean:Query id = asni00115
    > profs/pr00010.clean:Query id = asni00116
    >
    > From this list I need the 'pr000??' data and the 'asni00??' data
    > from each line.


    grep ... | perl -ne 'print "$2=$1\n" if m{^\S+/(pr\d+)\S+ id = (asni\d+)$}'

    There are lots of other ways to write the regex; I just took a lazy way.
    David K. Wall, Feb 3, 2004
    #5
  6. David K. Wall <> wrote:
    > Chris <> wrote:
    > > profs/pr00001.clean:Query id = asni00107

    [...]
    > > profs/pr00010.clean:Query id = asni00116
    > >
    > > From this list I need the 'pr000??' data and the 'asni00??' data
    > > from each line.

    >
    > grep ... | perl -ne 'print "$2=$1\n" if m{^\S+/(pr\d+)\S+ id = (asni\d+)$}'


    or use -p instead of -n
    grep ... | perl -pe 's/^.*(pr\d+).*(asni\d+).*/$2=$1/'

    why use grep at all?
    perl -lne 'next unless /^.*(pr\d+).*(asni\d+).*/; print "$2=$1"' input

    --
    Glenn Jackman
    NCF Sysadmin
    Glenn Jackman, Feb 3, 2004
    #6
  7. Chris

    Chris Guest

    Brad Baxter <> wrote in message news:<>...
    > On Tue, 3 Feb 2004, Chris wrote:
    >
    > > But I wanted to do this:
    > > my $h{$F[3]} = $F[0] =~ /(pr\d{5})/;
    > >
    > > However, this gives 'asni00107=1'. How can I get the capturing to
    > > return the match rather than the number of matches? I've done
    > > this before with success and I'm sure I haven't missed anything
    > > obvious (have I?).
    > > Any pointers appreciated.

    >
    > I think this is what you're after:
    >
    > ($h{$F[3]}) = $F[0] =~ /(pr\d{5})/;
    >


    Thanks very much to all of you. It was indeed list context that I
    needed. BTW the 'my $h{$F[3]}' is a mistake. I'd copied it across
    whilst trying rememdy the problem half-way through...
    Chris, Feb 4, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    630
  2. Victor
    Replies:
    2
    Views:
    629
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    356
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    463
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    155
Loading...

Share This Page