http://www.perlmonks.org?node_id=1025293


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

Like so often: It depends.

How long are the keys and how much + do you have? I think that your hash approach is good as far as you have enough memory to store it. And the more you have to query the more it is profitable to build this hash initially.

McA

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

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 Jenda (Abbot) on Mar 25, 2013 at 13:20 UTC

    And if the hash happens to grow too big, you can tie it to an optimized cached on disk hash using DB_File, BerkeleyDB or similar module.

    One thing though, it's not necessary to store the 1 in the values. Keep them empty and use exists()

    my %set; @set{@values} = (); ... if (exists $set{$key}) ...

    Update: Fixed a syntax error (incorrect type of parens). Thanks go to choroba.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Seems like I'm on the right track, but this is a more elegant implementation. I was not previously familiar with the ability to suck an entire array into a hash using the @hash{@array}=() syntax.

      One quick syntactical question... using this method, how would I set each hash key to a defined static value? Using something like:

      @hash{@array}='MyValue';

      Only seems to set the value for the first key...
        > I was not previously familiar with the ability to suck an entire array into a hash

        it's called "hash slice"! see perldata#Slices

        > using this method, how would I set each hash key to a defined static value?

        assign a list of same length

        DB<156> @array=1..5 => (1, 2, 3, 4, 5) DB<157> @hash{@array} = ('my val') x @array => ("my val", "my val", "my val", "my val", "my val") DB<158> \%hash => { 1 => "my val", 2 => "my val", 3 => "my val", 4 => "my val", 5 => + "my val" }

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        UPDATE

        for completeness TIMTOWTDI:

        $hash{$_}="my val" for @array

        has less of a memory footprint.

        and

         my %hash = map { $_ => "my val" } @array;

        is DRYer and makes sure the whole hash is reinitiated.

        You have to provide a list of values on the right hand side. For example, you can try:
        @hash1{@array} = ('value') x @array; @hash2{@array} = 0 .. $#array;
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: How to check if a variable's value is equal to a member of a list of values
by gnosti (Chaplain) on Mar 25, 2013 at 18:54 UTC
    I don't see the OP mentioning whether the values to be checked are integer or floating point. Uncertainties in representing floating point values could make hash test useless.

      This is an interesting point. Do you know what value is taken for the hash key? The string representation or the floating point binary representation?

      When the first is true than what you said falls in the whole class of floating point related problems, e.g. equality of floats.

      McA

        Do you know what value is taken for the hash key?

        Hash keys are always strings.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.