capturing a match

C

Chris

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.
 
P

Paul Lalli

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
 
B

Brad Baxter

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
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

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})/;
 
D

David K. Wall

Chris said:
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.
 
G

Glenn Jackman

David K. Wall said:
Chris said:
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
 
C

Chris

Brad Baxter said:
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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top