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

(Perl 5.16 on linux).. I often prefer postfix expressions, like  say $_ for 1..10;

As a natural extension, I'd like to use nested postfix loops. ChatGPT suggested:

say $_ for 1..5 for 1..10;

which seemed a strange proposition, because how could the scalar $_ contain both $i and $j? I tried it, thinking: "I gotta SEE this!". I thought perhaps Perl was clever enough to make $_ a reference to a list, but say or print of that would only be the reference, which was even more perplexing why it was suggested.But it only throws a syntax error on the second "for".

TY

Replies are listed 'Best First'.
Re: postfix "for" question?
by SankoR (Prior) on Sep 11, 2023 at 14:31 UTC

    I wouldn't expect ChatGPT to generate valid Perl; it's hard enough for human beings to do that without knowing Perl.

    do { CORE::say $_ for 1 .. 5 } for 1 .. 10;

    You could try that. I wouldn't. But you could.

    Edit: Even more convoluted: do { CORE::say $_ for 1 .. $_ } for 1 .. 10;. I guess tinkering like this helps figure out how context and loops work, if that's the goal.
      well my REAL goal, if truth be told, as an APL-trained programmer, is to seek ways to produce a result in the fewest number of characters... Removing {} not only helps reach that goal, it makes things much more English-like..

      TY!

        perl -E 'say for (1 .. 5) x 10'

        This solution is brought to you without the help hindrance of ChatGPT.


        🦛

        How about

        map CORE::say, 1..5 for 1..10

        map is almost postfix and almost like a for

Re: postfix "for" question?
by LanX (Saint) on Sep 11, 2023 at 16:40 UTC
    ChatGPT is a text generator, not a programming tool.

    This code is just 🐂💩

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

      I think I (understand and) agree with what you mean, but not with the wording. Text generators are programming tools, too.

      GhatGPT is not sophisticated enough to be reliably used as a programming tool.
      (While it may be highly sophisticated in several other regards, but it is not in this regard…)
        All code generators are text generators, true. But not the other way round.

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

Re: postfix "for" question?
by karlgoethebier (Abbot) on Sep 12, 2023 at 10:59 UTC

     perl -E 'map {say 1..10} 1..5'

    «The Crux of the Biscuit is the Apostrophe»

      Not quite.

      perl -E 'map {say for 1..10} 1..5'

        This was actually intentional as it looks better:

        Karl@h3002993:~$ perl -E 'map {say 1..10} 1..5' 12345678910 12345678910 12345678910 12345678910 12345678910

        Or:

        karl@h3002993:~$ perl -E 'map {say join " ", 1..10} 1..5' 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

        Compared to:

        karl@h3002993:~$ perl -E 'map {say for 1..10} 1..5' 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

        Update: Probably I meant  perl -E 'map {print for 1..10,"\n"} 1..5'

        «The Crux of the Biscuit is the Apostrophe»