Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^4: Ignore a range of numbers ina List

by Laurent_R (Canon)
on Jun 25, 2017 at 22:52 UTC ( [id://1193534]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Ignore a range of numbers ina List
in thread Ignore a range of numbers ina List

Thanks, Marshall for your recommendation.

Well, as I said, I first thought that Marshall's idea of a solution was good, but since I felt that is was probably a bit complicated. I was hoping to make something simpler. As I said, I ended up doing independently something essentially similar (although written differently and using an additional $flag variable, which isn't really necessary, as Marshall has shown).

Trying to simplify (or at least shorten) the code I tried to use a ternary conditional, something along these lines:

push $flag ? @temp : @result, $num;
But the compiler doesn't like this. Even adding parentheses does not seem to help.

So that's why I next came to the idea of pushing the values to a variable containing a reference to either the @temp or the @result array, depending on whether we are in a 6..7 range or not. I think this is pretty clean and clear, and quite concise, but using references this way may be beyond the knowledge of a beginner (although the OP uses an array reference).

Replies are listed 'Best First'.
Re^5: Ignore a range of numbers ina List
by choroba (Cardinal) on Jun 25, 2017 at 23:05 UTC
    It seems you need
    push @{ $flag ? \@temp : \@result }, $num;
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Hi choroba,

      thanks for your input. This certainly looks quite clever. I can't test right now on my mobile device, but certainly will as soon as possible.

      Update: I tried it now, and it works just fine.

        Back in the good ol' days, push and its ilk could take as their first argument an EXPR that evaluated to an unblessed array reference, making the syntax slightly less convoluted:

        c:\@Work\Perl>perl -wMstrict -MData::Dump -le "print qq{perl version $]}; ;; my (@foo, @bar); my $fooish = 1; push $fooish ? \@foo : \@bar, 'quux'; dd \@foo; dd \@bar; " perl version 5.014004 ["quux"] []
        Today, we have Postfix Dereference Syntax (see perlref). Ah, the good ol' days... (sigh)

        Update: To avoid leading anyone astray, I should have mentioned that, per the docs: "Starting with Perl 5.14, an experimental feature allowed push to take a scalar expression. This experiment has been deemed unsuccessful, and was removed as of Perl 5.24." (This behavior was also removed from related built-ins pop, keys, values, and other such functions that, for a time, operated by reference on arrays and hashes.) (Update: For the official announcement, see The autoderef feature has been removed in perl5240delta.)


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-28 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found