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

Say you have a function that returns a list, like split. And you only want a few of the items it returns. What's the best way to get only the values you want?

If the ones you want are at the beginning of the list, it's easy:

($foo, $bar) = split(" ", $baz);

But if they're somewhere in the middle, I typically do this:

($trash, $trash, $foo, $trash, $bar) = split(" ", $baz);
Using $trash makes it easy for me to see which values I actually care about.

There are other ways, too. You can enclose the split in parens and use brackets to access particular elements, like an array. But is there a widely accepted method that I'm missing?

---
A fair fight is a sign of poor planning.

Replies are listed 'Best First'.
Re: Tossing unwanted values from returned list
by boo_radley (Parson) on Nov 18, 2003 at 00:04 UTC

    instead of wasting the trash scalar, you can use LHS undefs:

    (undef, $foo, undef, undef, $bar) =(1..5); print "$foo $bar";
    but in general, I will use split 'in parens with brackets' as you mentioned. It's pretty direct, you don't waste any characters dealing with elements that you're ignoring.
    ($foo, $bar) =(1..5)[1,4]; print "$foo $bar";

Re: Tossing unwanted values from returned list
by simonm (Vicar) on Nov 18, 2003 at 00:05 UTC
    I personally like the parens-and-brackets strategy.
    my ( $foo, $bar ) = ( split " ", $baz )[2,4];

    Hard to see how it could get much better than that...

Re: Tossing unwanted values from returned list
by dpuu (Chaplain) on Nov 18, 2003 at 00:04 UTC
    you can use undef instead of $trash:
    (undef, undef, $foo, undef, $bar) = split(" ", $baz);
    This avoids using hte dummy variables. --Dave.
Re: Tossing unwanted values from returned list
by Roger (Parson) on Nov 18, 2003 at 00:49 UTC
    You can use slices for this purpose in one easy step -
    ($foo, $bar) = (split ' ', $baz)[2,4];
    Update: Oh my, I hit the submit button and found half a dozen answers before me already. I love PM. :)

Re: Tossing unwanted values from returned list
by doran (Deacon) on Nov 18, 2003 at 05:33 UTC
    If the unwanted values can be described with a regex, you can also use grep. This can be useful if you don't know where in the original value the unwanted values will occur.
    #!/usr/bin/perl use warnings; use strict; my $list = 'foo #comment bar <tag> baz'; my ($first, $second, $third) = grep(/^\w+$/, split(' ',$list)); print<<_; 1. $first 2. $second 3. $third _ ; C:\>perl test.pl 1. foo 2. bar 3. baz
      You know, I don't think I've ever used grep in a perl script. I tend to go the long way around and run things through an if ($foo =~ /.../) construct. I'll have to make it a point to try grep a few times so I can add it to my toolbox.

      ---
      A fair fight is a sign of poor planning.

Re: Tossing unwanted values from returned list
by pizza_milkshake (Monk) on Nov 18, 2003 at 03:52 UTC
    pizza@pizzabox:~/pl$ perl -le'($a, undef, $b) = 1..3; print "$a $b"' 1 3

    perl -e"map print(chr(hex(( q{6f634070617a6d692e7273650a}=~/../g)[hex]))), (q{375542349abb99098106c}=~/./g)"

Re: Tossing unwanted values from returned list
by Anonymous Monk on Nov 18, 2003 at 14:34 UTC
    while (<XFERLOG>) { ($file, $dir, $login, $status) = (split)[8,11,13, 17]; ... }