Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Multiple foreach loops in single statement

by gurpreetsingh13 (Scribe)
on Oct 23, 2012 at 16:29 UTC ( [id://1000497]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, Need a little help. Was just trying to figure out how can we have more than one foreach loop at the end of statement instead of a nested foreach. Working code:
foreach(`cat file.pl`){ foreach(split(//,)){ print ord; } }
This also works.
foreach(`cat file.pl`){ print ord foreach(split(//,)); }
But this one fails.
print ord foreach(split(//,)) foreach(`cat file.pl`);
Tried to search for something similar but couldn't find out. Thanks in advance

Replies are listed 'Best First'.
Re: Multiple foreach loops in single statement
by ikegami (Patriarch) on Oct 23, 2012 at 16:40 UTC

    Statement modifiers are not nestable. But since

    do { ... } for ...;
    is legal, so is
    do { ... for ... } for ...;

    But for readability's sake, just don't do that! Especially since

    while (...) { ...}

    and

    do { ...} while ...;

    have different semantics.

    A functional language approach would probably achieve what you want better.

    print map ord, split //, `cat file.pl`;

    (Yeah, the outer loop is useless, as you can see.)

      Great. Just noticed that we can provide a list too in split function. The docs however mention EXPR --split /PATTERN/, EXPR-- which I assumed to be a scalar.

      Anyways thanks for the help. This was what I wanted to achieve:

      perl -e 'print chr(ord()+1) foreach(split //,`cat file.pl`);'
      (a simple one liner to encrypt a file via ascii shift).

        ...we can provide a list too in split function.

        Sorry, no. split operates on a single string (a scalar), but not on a list:

        13:57 >perl -wE " $s = q[abc]; $t = q[mno]; say for split(//, $s, $t); + " Argument "mno" isn't numeric in split at -e line 1. a b c 14:06 >perl -wE " $s = q[abc]; $t = q[mno]; say for split(//, ($s, $t) +); " Useless use of a variable in void context at -e line 1. m n o 14:06 >perl -wE " @u = (q[abc], q[mno]); say for split(//, @u); " 2 14:06 >
        The docs however mention EXPR ... which I assumed to be a scalar.

        Yes, your original assumption was correct. But split returns a list, which is why the output of split can be the subject of a foreach or a map.

        Hope that helps,

        Athanasius <°(((><contra mundum

Re: Multiple foreach loops in single statement
by blue_cowdawg (Monsignor) on Oct 23, 2012 at 16:39 UTC

    use strict; print join "",map { ord $_ } map{split(//,$_)} `cat file.pl`; ___________________ $ perl file.pl 1171151013211511611410599116591011211410511011632106111105110323434441 +099711232123321111141003236953212532109971121231151121081051164047474 +436954112532329699971163210210510810146112108965910
    Something like that?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-19 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found