Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

stripping an array

by hotshot (Prior)
on Jan 02, 2003 at 18:01 UTC ( [id://223813]=perlquestion: print w/replies, xml ) Need Help??

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

hi guys!

I have two array and I want to compare the arrays as follows:
# if I have: my @arr1 = qw(2 4 5); my @arr2 = ('1,nfs', '2,afp', '3,cifs', '4,dns', '5,backup');
I want to stay with stay with the entries in @arr2 that their prefix is the digits from @arr1:
# therefore from the above arrays I want to get: @arr3 = qw(afp dns backup);
what is the shortest way?
Thanks

Hotshot

Replies are listed 'Best First'.
•Re: stripping an array
by merlyn (Sage) on Jan 02, 2003 at 18:09 UTC
    my @arr1 = qw(2 4 5); my @arr2 = ('1,nfs', '2,afp', '3,cifs', '4,dns', '5,backup'); my %arr1_as_hash; $arr1_as_hash{$_}++ for @arr1; my @arr3 = map { my ($key, $val) = split /,/; $arr1_as_hash{$key} ? $val : (); } @arr2;

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      For variety, here's a solution that uses a kind of Schwartzian Transform (but grepping instead of sorting).
      my %arr1_as_hash; @arr1_as_hash{@arr1} = (); my @arr3 = map { $_->[1] } grep { exists $arr1_as_hash{$_->[0]} } map { [ split /,/ ] } @arr2;

      jdporter
      The 6th Rule of Perl Club is -- There is no Rule #6.

Re: stripping an array
by broquaint (Abbot) on Jan 02, 2003 at 18:44 UTC
    Option no. 4
    use Data::Dumper; my @arr1 = qw(2 4 5); my @arr2 = ('1,nfs', '2,afp', '3,cifs', '4,dns', '5,backup'); my @arr3 = map { (split /,/)[-1] } grep { /^[@arr1],/x } @arr2; print Dumper(\@arr3); __output__ $VAR1 = [ 'afp', 'dns', 'backup' ];
    It may not be pretty, but it works :)
    HTH

    _________
    broquaint

Re: stripping an array
by Juerd (Abbot) on Jan 02, 2003 at 18:13 UTC

    what is the shortest way?

    I don't know about shortest. Don't feel like playing golf today, sorry :)

    # untested my %foo = map { split /,/, $_, 2 } @arr2; my @arr3 = map { exists $foo{$_} ? $foo{$_} : () } @arr1;
    Your arrays need better names. So does my %foo.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      I tested this one. It works and wins my vote for shortest and cleanest.

        But you're wrong. :-)
        my @arr3 = @foo{@arr1};

        And if you don't mind very short but somewhat dirty, you can do this:
        my %foo = map{/,/;$`,$'} @arr2;

        jdporter
        The 6th Rule of Perl Club is -- There is no Rule #6.

Re: stripping an array
by FamousLongAgo (Friar) on Jan 02, 2003 at 18:15 UTC
    Here's a simplistic solution, that avoids the kind of munging you are now doing with key names:
    my @arr1 = (1 3 4); # remember arrays are zero based my @arr2 = qw( nfs afp cifs dns backup); my @arr3 = @arr2[@arr1]; # slice notation
    However, here are far more elegant ways to do what you are probably after with nested data structures, and my perlmonks intuition tells me you'll see a bunch of good examples in the posts to follow.


Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://223813]
Approved by FamousLongAgo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found