http://qs1969.pair.com?node_id=620456

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: grep with multiple choice
by ferreira (Chaplain) on Jun 11, 2007 at 11:55 UTC
    grep { /^Julia_Roberts/ } @arr
    should be enough. There's nothing particularly different with this "grep with multiple choice". The only thing it seems you are missing is that the grep block can match different values provided you code a suitable matching sub, like I did with a regex. If your problem is really to find out the array elements starting with $s, this will work:
    grep { /^\Q$s\E/ } @arr; # regex with quoted interpolation # or grep { index $_, $s == 0 } @arr; # more efficient
Re: grep with multiple choice
by citromatik (Curate) on Jun 11, 2007 at 12:19 UTC

    If I understood correctly, you want to provide 2 names (for example, "Julia_Roberts" and "Cintheia"), look if both exist in the array and append the "(1,5)" thing to the second. If this is what you want, an array doesn't seem the best way to manage your data, and maybe a hash could be a better solution:

    use strict; use warnings; use Tie::RegexpHash; tie my %names, 'Tie::RegexpHash' or die $!; %names = ( qr/Julia_Roberts/ => ["Julia_Roberts","(1,5)"], qr/Cintheia/ => ["Cintheia"], qr/Chelsia/ => ["Chelsia"] ); while (<DATA>){ chomp; my @pair = split; $names{$pair[1]}->[1]= $names{$pair[0]}->[1]; print join ("",@{$names{$_}}),"\n" for (keys %names); print "\n"; } __DATA__ Julia_Roberts Cintheia Julia_Roberts(1,5) Cintheia Julia_Roberts Chelsia
    This Outputs:
    Julia_Roberts(1,5) Cintheia(1,5) Chelsia Julia_Roberts(1,5) Cintheia(1,5) Chelsia Julia_Roberts(1,5) Cintheia(1,5) Chelsia(1,5)

    If it is not what you are trying to do, please be more specific

    Hope this helps!

    citromatik

Re: grep with multiple choice
by reasonablekeith (Deacon) on Jun 11, 2007 at 11:17 UTC
    Sorry sroy5, but I've read that 5 times and I'm still non-the-wiser to what you're actually trying to do. I'll wait to be proved wrong, but I think you'll have to expand your description of what you're doing before you'll get a reasonable answer here.

    It might be worth your time reading through this too... I know what I mean. Why don't you?

    PS: Welcome to perlmonks

    ---
    my name's not Keith, and I'm not reasonable.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: grep with multiple choice
by agianni (Hermit) on Jun 11, 2007 at 16:19 UTC

    A couple of map statements should make this pretty easy to do with an array:

    my @array = map { chomp and $_ } <DATA>; my $want = 'Julia_Roberts'; my $change = 'Cintheia'; my ( $suffix ) = map { m/\A$want(\(\S+\))\z/smx and $1 } @array; my @final = map { ( /\A($change)\z/ and "$change$suffix" ) or $_ } @ar +ray if $suffix; warn Dumper \@final; __DATA__ Julia_Roberts(1,5) Cintheia Chelsia Rohan

    The first map statement goes through the array and identifies the suffix for the pattern to be matched and the second applies the suffix to the desired element. I would probably do some kind of error checking after the first map, getting an array instead of a scalar from the map and checking to see if there's only one element returned -- what do you do if Julia_Roberts(1,5) and Julia_Roberts(2.8) are in the list?

    perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'
Re: grep with multiple choice
by Roy Johnson (Monsignor) on Jun 11, 2007 at 20:45 UTC
    use warnings; use strict; my @array = qw(Julia_Roberts(1,5) Cintheia Chelsia Rohan); my $pattern = 'Julia_Roberts'; my $change = 'Cintheia'; # Find your pattern my ($found) = grep /$pattern/, @array; # Change your pattern into the new value $found =~ s/$pattern/$change/; # Replace the change item with your new value for (@array) { s/$change/$found/; } print map "$_\n", @array;

    Caution: Contents may have been coded under pressure.
      Hello Johnson,
      Thanks for the code.Your interpretation was wright. What 
      actually I was looking for is as follows: 
      the array was  my @array = qw(Julia_Roberts(1,5) Cintheia Chelsia Rohan);
      The above @array is actually dynamic feeded array. In which 
      the possibility is I may not get "Cintheia" all the time.So 
      I may need to grep for "Cintheia" along with the "Julia_Roberts" . If both of them found then the next code 
      you wrote follows as it is .
      
      Actually I dont know how to grep for more than one words 
      from the array.
      
      Would look for your comments
      SR
      
        If you had two different things to grep for, you would do two different greps. I do not know what the final result would be if you started with "Cintheia(2,3)" in the array and grepped for "Cintheia".

        Caution: Contents may have been coded under pressure.