in reply to Tossing unwanted values from returned list

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

Replies are listed 'Best First'.
Re: Re: Tossing unwanted values from returned list
by Sprad (Hermit) on Nov 18, 2003 at 16:17 UTC
    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.