shyamasoni has asked for the wisdom of the Perl Monks concerning the following question:

Thank you all for great help.....

Replies are listed 'Best First'.
Re: Getting element of array with a match
by Eily (Monsignor) on Dec 09, 2014 at 14:11 UTC

    Your problem description wasn't very clear, and it kind of looks like an XY Problem actually, why are you trying to do that? Are you sure there's not a better way to achieve your goal?.

    Anyway, it looks like you're asking for tr///, tr/a-zA-Z//cd; will remove any non-alphabetic char from your string, tr/a-zA-Z//cdr; will return a new string equal to the first without the non-alphabetic chars.

Re: Getting element of array with a match
by choroba (Cardinal) on Dec 09, 2014 at 14:18 UTC
    You can build a regular expression from your string. If you just want the characters appear in the same order, you can place .* between each two characters. The regular expression for "filswjs" will then be f.*i.*l.*s.*w.*j.*s, which means "f, than anything, then i, then anything, then l, ...". Then just grep the arrays for matches:
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my @array1 = qw(fil01_sw100056_02.04.js mn_sw100868_15.17.js roof_app01_sw12345_26.02.hex roof_dir01_sw101481_01.32 +.dir); my @array2 = qw(fil01_sw100056_02.03.js mn_sw100868_15.16.js roof_app01_sw101483_25.05.hex roof_dir01_sw101481_01.2 +5.dir); my $string = 'filswjs'; (my $regex = $string) =~ s/(.)(?=.)/$1.*/g; say for grep /$regex/, @array1, @array2;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      If I understood correctly, OP wanted to ignore everything but alphabetic ("string") characters, as opposed to numbers ("no") and special characters. So the regex would have to be $regex = join "[^a-zA-Z]*", split //, $string;

      Though the more I look at the question, the more it looks like an XY problem

Re: Getting element of array with a match
by trippledubs (Deacon) on Dec 09, 2014 at 14:24 UTC
    Hashes are good for determining uniqueness of things. The grep tests every value in @array2 and populates @common with each value that returns true.
    #!/usr/bin/env perl use strict; use warnings; use Data::Dump; my @array1 = (1,2,3,4); my @array2 = (4,5,6,7); my %hash; @hash{@array1} = undef; #Hash Slice populates keys of hash using value +s from array my @common = grep { exists $hash{$_} } @array2; dd @common; #4