Which element do you want to extract? Those strings look about the same to me (only the numbers changed)... what kind of arbitrary text will appear before the pattern (sometimes)?
You Tell me you want to match a pattern... what pattern? Perrhaps you should see perlre.
They say that time changes things, but you actually have to change them yourself. Andy Warhol
| [reply] [d/l] |
ditto on JediWizard's reply. in addition, though, i will take a guess that with the two sample lines you're looking for an integer element and want to track what comes before it. Simply match both parts and then store accordingly (below shows how to keep track of just the counts, or in addition to keep the elements themselves too, in which case the count is jsut the size of the array ref)
my %cts;
if( $s =~ /^(.*?) (\d+)/ ){
my ($text, $element) = ($1, $2);
$cts{$text}++;
# OR
push @{$cts{$text}}, $element;
}
| [reply] [d/l] |
Maybe not the most elegant solution (and I bet rather slow as well)
It finds the longest string that is at the start of every line in the
imput.
use warnings;
use strict;
my %ch;
my $part;
my $lines;
while (<DATA>) {
chomp;
$part="";
map { $part .= $_;
print $part."\n";
$ch{$part}++;
} split (//);
$lines ++;
}
my $max;
foreach (keys (%ch) ) {
if ( ($ch{$_} == $lines) and (length($_) > length($max)) ) {
$max = $_;
}
}
print $max;
__DATA__
abcdefg
abcd
abcd grtf
abcd abd
abcd daf
si_lence | [reply] [d/l] |