Li Ngok Lam wrote:
>
> Hi all,
Hello,
> That's about the whole story, but I'll make it short.
> For example, I have a list like this :
>
> 123ABCDEF456
> 123456
> 654WXYZ321
> 987654321
> ABCDEF123456
> WXYZ321
>
> By user's INTEGER input , I will have to find how many similar
> patterns are matched within the list according to certain chars (user's
> input ) :
>
> For example, I input '3', then I will get the result like this :
>
> Res1: 123ABCDEF456 is similar to 123456
> Res2: 123ABCDEF456 is similar to ABCDEF123456
> Res3: 654WXYZ321 is similar to 987654321
> Res4: 654WXYZ321 is similar to WXYZ321
>
> In case , if a pattern match happens, then the elem in list will not
> be shown again even another match happens. Okay, thaz my
> homework for how to deal with the output.
>
> The question I want to ask is how to tell ( or is this a good starting
> point ) the regex to compare the patterns freely ? So I can get
> 654WXYZ321 match 987654321 and also match WXYZ321 ?
>
> I hope I can explain my question well.
I'm not sure exactly what you want but maybe this will give you some ideas:
#!/usr/bin/perl
use warnings;
use strict;
my @data = qw(
123ABCDEF456
123456
654WXYZ321
987654321
ABCDEF123456
WXYZ321
);
for my $x ( @data ) {
for my $y ( @data ) {
next if $x eq $y or length( $x ) < length( $y );
my $count = () = $x =~ /[\Q$y\E]/g;
my $perc = ( $count / length $x ) * 100;
printf "%-12s %-12s %2d %2d %6.2f %%\n", $x, $y, length $x, $count, $perc;
}
}
__END__
Produces this output:
123ABCDEF456 123456 12 6 50.00 %
123ABCDEF456 654WXYZ321 12 6 50.00 %
123ABCDEF456 987654321 12 6 50.00 %
123ABCDEF456 ABCDEF123456 12 12 100.00 %
123ABCDEF456 WXYZ321 12 3 25.00 %
654WXYZ321 123456 10 6 60.00 %
654WXYZ321 987654321 10 6 60.00 %
654WXYZ321 WXYZ321 10 7 70.00 %
987654321 123456 9 6 66.67 %
987654321 WXYZ321 9 3 33.33 %
ABCDEF123456 123ABCDEF456 12 12 100.00 %
ABCDEF123456 123456 12 6 50.00 %
ABCDEF123456 654WXYZ321 12 6 50.00 %
ABCDEF123456 987654321 12 6 50.00 %
ABCDEF123456 WXYZ321 12 3 25.00 %
WXYZ321 123456 7 3 42.86 %
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]