Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Perl : Split/Regex

by johngg (Canon)
on Sep 03, 2014 at 23:01 UTC ( [id://1099484]=note: print w/replies, xml ) Need Help??


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

Since the OP mentions that the "Y" will be at the end, why not use a [ -1 ] subscript to avoid the "defined" complication? You could also use a single push with the conditional and target arrays in a ternary. Some may find this less readable than the if ... else ... but I prefer it for simple "push to this or that depending" situations.

$ perl -Mstrict -Mwarnings -E ' open my $inFH, q{<}, \ <<EOD or die $!; ws.abc.dsfsfsfwerfrwef std 13232 ws.dfsdferferfregregrgr std 99868 Y ws.abc.asddwefewfewfewf std 45345 fwfrwfrefergegregerefds std 36754 Y ws.abc.fferfergregrfreg std 45435 EOD my @streams; my @spaces; while ( <$inFH> ) { my @flds = split; push @{ $flds[ -1 ] eq q{Y} ? \ @streams : \ @spaces }, $flds[ 0 ]; } say qq{@streams}; say qq{@spaces};' ws.dfsdferferfregregrgr fwfrwfrefergegregerefds ws.abc.dsfsfsfwerfrwef ws.abc.asddwefewfewfewf ws.abc.fferfergregrfreg $

I hope this is of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^3: Perl : Split/Regex
by AppleFritter (Vicar) on Sep 03, 2014 at 23:08 UTC

    Since the OP mentions that the "Y" will be at the end, why not use a [ -1 ] subscript to avoid the "defined" complication?

    That works, but what if you just so happen to have a "Y" in the third field? The OP's example lines only have numbers, granted, but I didn't want to assume too much about how his/her data is structured.

    You could also use a single push with the conditional and target arrays in a ternary. Some may find this less readable than the if ... else ... but I prefer it for simple "push to this or that depending" situations.

    Ah, yes, that's a good point. I was trying that, actually; it didn't work ("Not an ARRAY reference at ..."), but it didn't occur to me to wrap the whole shebang in a @{ }. OTOH, between that and the need to take references to the arrays you want to push to, it is indeed on the cryptic side.

    Nevertheless, I learned something new. Thank you for enlightening me, brother!

      ..That works, but what if you just so happen to have a "Y" in the third field?..

      For the lines without the Y there would be no *fourth field counting from 0, check or count it.
      And even if you decide to include Y in the those lines last field if($fields[-1] eq 'Y'){.. would still work.


      *Updated this in light of the discuss that follows! Which has nothing to do with the level of correctness as some may presume.
      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

        For the lines without the Y there would be no thirdth field counting from 0, check or count it.

        Correction: there would be no $field[3], but that's the fourth field, not the third, as one conventionally starts to count at one, not zero. (If you disagree, ask yourself how many hands you have, for instance. Is it one or two?)

        I thought that this did not need to be said.

Re^3: Perl : Split/Regex
by GrandFather (Saint) on Sep 03, 2014 at 23:47 UTC

    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

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-25 23:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found