in reply to Re^2: 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?

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!

Replies are listed 'Best First'.
Re^4: Perl : Split/Regex
by 2teez (Vicar) on Sep 03, 2014 at 23:19 UTC

    ..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.

        ..(If you disagree, ask yourself how many hands you have, for instance. Is it one or two?)..

        Oh! I disagree!, because I have just one hand! But humans should have two! You see? :)

        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