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

The push reads better without the obfuscating ref/deref stuff:

push (($flds[-1] eq 'Y' ? @streams : @spaces), $flds[0]);

Update: Ok, so that was a fail - struck

Perl is the programming world's equivalent of English

Replies are listed 'Best First'.
Re^4: Perl : Split/Regex
by AnomalousMonk (Archbishop) on Sep 04, 2014 at 00:57 UTC

    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) ()

      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