in reply to Re^3: Perl : Split/Regex
in thread Perl : Split/Regex

Reads better perhaps, but doesn't actually work. First argument to push must be something starting with the  @ sigil or, with Perl 5.14+, a reference to an unblessed array.

c:\@Work\Perl>perl -wMstrict -le "print $]; ;; my (@streams, @spaces); my $y = 'Y'; push (($y eq 'Y' ? @streams : @spaces), 'Z'); print qq{(@streams) (@spaces)}; " 5.014004 Not an ARRAY reference at -e line 1. c:\@Work\Perl>perl -wMstrict -le "print $]; ;; my (@streams, @spaces); my $y = 'Y'; push (($y eq 'Y' ? \@streams : \@spaces), 'Z'); print qq{(@streams) (@spaces)}; " 5.014004 (Z) ()

Replies are listed 'Best First'.
Re^5: Perl : Split/Regex
by GrandFather (Saint) on Sep 04, 2014 at 01:48 UTC

    Interesting! I'd checked using a numeric constant for the conditional and that works as I expected:

    use strict; use warnings; my @streams; my @spaces; push ((0 ? @streams : @spaces), 0); push ((1 ? @streams : @spaces), 1); print "$]\n"; print "Streams: '@streams'\n"; print "Spaces: '@spaces'\n";

    Prints:

    5.016003 Streams: '1' Spaces: '0'

    but anything other than a numeric constant fails as you describe. That'll teach me to test one thing then post another!

    Perl is the programming world's equivalent of English

      Huh?!? (closes mouth, wipes away drool, ponders...)   Oh, yeah: constant folding (if that's the right term).

      c:\@Work\Perl>perl -wMstrict -le "print $]; ;; my (@streams, @spaces); push ((0 ? @streams : @spaces), 'ZIT'); print qq{(@streams) (@spaces)}; push ((1 ? @streams : @spaces), 'ZOT'); print qq{(@streams) (@spaces)}; " 5.014004 () (ZIT) (ZOT) (ZIT) c:\@Work\Perl>perl -wMstrict -MO=Deparse,-p -le "print $]; ;; my (@streams, @spaces); push ((0 ? @streams : @spaces), 'ZIT'); print qq{(@streams) (@spaces)}; push ((1 ? @streams : @spaces), 'ZOT'); print qq{(@streams) (@spaces)}; " BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict 'refs'; print($]); my(@streams, @spaces); push(@spaces, 'ZIT'); print("(@streams) (@spaces)"); push(@streams, 'ZOT'); print("(@streams) (@spaces)"); -e syntax OK