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