in reply to Re: using shift and $_ in loop
in thread using shift and $_ in loop

Yes they do suffer that- it tests for truth.

Maybe I was too vague in my posting, if so I apologize.

I expect my sub to take an unforeseen number of arguments. My sub will take the arguments, discard some, and the ones that are not discarded, will be altered- and a list of the altered arguments are returned back.

An example..

use Lingua::Names 'is_name'; my @a = qw/andy lo1 beca cris/; my @A = alter(@a); sub alter { my @newlist; for ( @_ ){ my $name = is_name($_) or next; push @newlist, uc($name); } @newlist; }

Now, how can I make this so I can use shift, maybe even a for loop with undef and last??? What might be apropriate here to make it a little more minimal? This is a little closer..

sub alter { my @newlist; while ( @_ ){ $_ = shift @_; my $name = is_name($_) or next; push @newlist, uc($name); } @newlist; }
I'm wondering if I can just shift and push from @_ and then return the modified @_ .. using $_ ??? Maybe this is just too much coffee speaking and I need to chill out?

Replies are listed 'Best First'.
Re^3: using shift and $_ in loop
by moritz (Cardinal) on Oct 21, 2009 at 16:09 UTC
    sub alter { my @newlist; for ( @_ ){ my $name = is_name($_) or next; push @newlist, uc($name); } @newlist; }

    I'd write that as

    sub alter { grep $_, map { uc is_name($_) } @_; }

    instead.

    In Perl 6 I'd maybe write something more similar to what you wrote:

    sub alter(*@a) { gather for @a { my $name = is_name($_); take uc $name if $name; } }

    Or even

    sub alter(*@a) { gather for @a.map({ is_name($_)}) { take .uc if $_; } }
    Perl 6 - links to (nearly) everything that is Perl 6.
      Are you going around tempting people to use perl6? I've looked at some specs.. pretty freaking interesting- a little scary too. Like "defined or" .. //= very sexy.. I wonder is all this stuff I'm looking at here actually implemented? Wild.
        Are you going around tempting people to use perl6?

        Yes. In fact that's how Perl got popular in the first place: people posted easy Perl solutions to problems that were posted in usegroups which got typically answered with examples in shell scripts, sed or awk.

        I wonder is all this stuff I'm looking at here actually implemented?

        I haven't tried them, but the examples I gave you use only features that are implemented in Rakudo.

        Perl 6 - links to (nearly) everything that is Perl 6.

        // and //= are present in 5.10

Re^3: using shift and $_ in loop
by ikegami (Patriarch) on Oct 21, 2009 at 16:28 UTC

    Now, how can I make this so I can use shift,

    What's wrong with the for version?

    map and grep lend themselves well here:

    sub alter { return map uc, grep is_name($_), @_; }
Re^3: using shift and $_ in loop
by keszler (Priest) on Oct 21, 2009 at 16:06 UTC
    If I'm reading this correctly, you want @A as the elements of @a that pass the is_name(x) test?
    @A = grep { is_name($_) } @a;
    perldoc -f grep is your friend.

    Update: I just noticed the uc() you used. In that case:

    @A = map { uc($_) } grep { is_name($_) } @a;
    perldoc -f map is also your friend.

      Both of your examples use the return value of is_name($_) only for truth testing, but it should (after applying uc) be assembled in the result array, at least that's what the OP's example does (don't know if it makes sense)

      Perl 6 - links to (nearly) everything that is Perl 6.
        Very good point, sir. I was assuming that is_name() returned the parameter given, or a false value. But you are correct: it could be altering/enhancing the parameter or returning anything else. Thus:
        @A = map { uc(is_name($_)) } grep { is_name($_) } @a;