in reply to Re^2: Comparing Lines within a Word List
in thread Comparing Lines within a Word List
I'm guessing it's to do with the fact that 'lama' has two a's? Is it possible to easily amend this code to handle this case?
Yes, and it's a trivial amendment. Just deploy the /g modifier:
#!/usr/bin/perl use strict; use warnings; my @words = <DATA>; chomp @words; while ( @words >= 2 ) { my $model = my $regex = shift @words; if ( $regex =~ s/(.*?)[ab](.*?)/$1\[ab\]$2/g ) { my @hits = grep /^$regex$/, @words; if ( @hits ) { print join( " ", $model, "matches", @hits, "using", $regex +, "\n" ); } } } __DATA__ lama lamb
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Comparing Lines within a Word List
by dominick_t (Acolyte) on Apr 30, 2016 at 14:06 UTC | |
by graff (Chancellor) on Apr 30, 2016 at 15:27 UTC | |
by hippo (Bishop) on Apr 30, 2016 at 14:26 UTC | |
by dominick_t (Acolyte) on Apr 30, 2016 at 14:49 UTC | |
by hippo (Bishop) on Apr 30, 2016 at 15:16 UTC |
In Section
Seekers of Perl Wisdom