Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: How to check if a variable's value is equal to a member of a list of values

by CountOrlok (Friar)
on Mar 25, 2013 at 13:16 UTC ( #1025295=note: print w/replies, xml ) Need Help??

in reply to How to check if a variable's value is equal to a member of a list of values

There are several ways. Fastest is probably to put the 1000+ values in a hash (as the keys, let their value be 1 if you don't care otherwise. Then you just need to check if exists($myhash{$variable})

Another way available since version 5.10, I believe, is if your 1000+ values are in an array you can do if (@myarray ~~ $variable) { #do something }

Replies are listed 'Best First'.
Re^2: How to check if a variable's value is equal to a member of a list of values
by McA (Priest) on Mar 25, 2013 at 14:04 UTC


    intersting: Because I'm curious: Is this substantially different (memory footprint, performance) than:

    if(grep { $_ eq $string } @array) {


      It should

    • smartmatch is implemented in C
    • grep doesn't stop after matching.

      Unfortunately all the use cases of smartmatch are hard to remember.

      For repeated lookups a prepared hash scales certainly better.

      You're free to check this with benchmarks or search for older discussions.

      I hoped smartmatch could at least handle the stringification limitation of hashes, but nope:

      DB<141> $h1={} => {} DB<142> $h1 ~~ [$h1] => "" DB<143> 5 ~~ [5] => 1

      Cheers Rolf

      ( addicted to the Perl Programming Language)


      ) Brian got deep into benchmarking :)

        Thank you. An intersting link. I have to admit that I haven't worked with the smart match operator. It's a little bit too much "Eierlegendenwollmilchsau" (1) for me. And what I read about it confirmed to wait with it. But probably I'm too old fashioned.


        (1) Best translation I could find on jack of all trades device

      Just to toss in a tidbit, and not trying to hijack the thread, but while benchmarking an alternative to using the smart match operator, I discovered that using map in scalar content performed better than grep.
      if( map { /$string/ } @array) {
      rather than...
      if( grep { /$string/ } @array) {

      I was surprised, because either way you'd be getting a non-undef value to satisfy the conditional, but map did it faster. I can post my code in a new thread if anyone's interested, otherwise, carry on! :)
        grep { TEST } LIST is semantically equivalent to map { TEST ? $_ : () } LIST

        Seems obvious that more logic results in longer runtime.

        Don't you think so?

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re^2: How to check if a variable's value is equal to a member of a list of values
by mje (Curate) on Mar 25, 2013 at 16:35 UTC

    Be careful as on newer perl versions:

    $ perl -le '@a = (1,2,3); print "fred\n" if @a ~~ 1;' # prints nothing $ perl -le '@a = (1,2,3); print "fred\n" if 1 ~~ @a;' fred

    I think it changed at 5.10.1 but I didn't check that.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025295]
[ambrus]: Corion: ah, so you want a library that parses HTTP, and you want to do the IO yourself, and don't want a full AnyEvent wrapper.
[ambrus]: Corion: I think I parsed a HTTP header from a string with LWP once. You can definitely use that to create a HTTP message too. The problme is
[ambrus]: that if you do that, you'd have to find where each HTTP response ends, which is nontrivial if you want persistent connections (essential for performance if you have small requests).
[Corion]: ambrus: Yes, ideally an API that I feed the incoming data piece by piece and that I can ask "is that response done" and "what should I do next" and "please construct the appropriate redirect for me"
[Corion]: ambrus: Yes, ideally the module would do all that nasty stuff for me and give me a way to ask it what the current situation is
[ambrus]: Corion: you could also consider using some wrapper over the multi interface of curl, I think Net::Curl might be a good one, since implementing enough of what it expects from the event loop might be easier than a full AnyEvent interface.
[ambrus]: Corion: you could also consider using IO::Async and its POE driver and some HTTP module for it, but I don't know if that would solve your problems with AnyEvent+POE

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (16)
As of 2016-12-07 16:02 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (130 votes). Check out past polls.