Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: process array with while loop

by monsieur_champs (Curate)
on Nov 09, 2006 at 17:38 UTC ( #583170=note: print w/replies, xml ) Need Help??


in reply to process array with while loop

There are also other alternatives for foreach. My preferred one is map:

@array = qw(blue red orange brown); map print, @array;

map allows you to work loops as mathematical application (or functions), and this is the closest approach to my world model.

UPDATE:Many thanks to ww, ikegami, merlyn for their comments and corrections.

Replies are listed 'Best First'.
Re^2: process array with while loop
by blahblah (Friar) on Nov 09, 2006 at 18:27 UTC
    I was going to reply with "map in void context, for shame", but then I learned something!
      Except that print is not a void context. Therefore the map will create a list.
      #!/usr/bin/perl use strict; use warnings; my $a = ( print "Hello world\n" ); print "The answer is $a\n"; __END__
      See `perldoc -f print` for details.
        Except that print is not a void context.

        I think you might be misunderstanding. blahblah wasn't talking about the call to print. He was talking about the call to map ("map in a void context"). And the call to map is in a void context - its return value doesn't go anywhere. But as he also pointed out, that's no longer a problem. Or, at least, it's not a problem from a technical point of view. I'd still argue that it's not the clearest way to write the code.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re^2: process array with while loop
by chromatic (Archbishop) on Nov 09, 2006 at 19:37 UTC

    What advantage does this have over print @array;?

      No advantage, I just like this construct. It can even not be the more advisable, nor the one everybody uses, but it is quite readable for me and TIMTOWTDI is important. ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://583170]
help
Chatterbox?
[Your Mother]: Stick with what you know. Or post a SOPW and if it's not complicated I guarantee someone will answer; me if it's straightforward and no one else gets it better first.
[1nickt]: ++YourMother (Grammar Vigilante of Bristol)
[Lady_Aleena]: Um, how does grep do multiline? print "$file:$_" if grep { /get_(array|hash| data)\(.+\)/ } $_; only returns the same 18 lines the command line grep does.
[Lady_Aleena]: my scratchpad has what I have so far.
[choroba]: what do you mean by multiline?
[Discipulus]: thanks 1nickt!
[choroba]: grep is a filter, it selects elements from a list based on a boolean condition
[moritz]: you need to read the file in one go to do a multiline regex match
[erix]: ha ha, I like the Vigilante
[Lady_Aleena]: Forget it. I'm getting angry with it.

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2017-05-23 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?