in reply to Re: Fastest way to test membership in a constant set
in thread Fastest way to test membership in a constant set

constant, example 3.

See the update here.

-QM
--
Quantum Mechanics: The dreams stuff is made of

  • Comment on Re^2: Fastest way to test membership in a constant set

Replies are listed 'Best First'.
Re^3: Fastest way to test membership in a constant set
by BrowserUk (Patriarch) on Jul 27, 2007 at 21:13 UTC
      Sorry, I saw something that I thought worked:
      use constant { SEC => 0, MIN => 1, HOUR => 2, MDAY => 3, MON => 4, YEAR => 5, WDAY => 6, YDAY => 7, ISDST => 8, };
      Instead, it's just syntax sugar to declare multiple constants with one use constant statement.

      I searched the doc on constant, and didn't see a good example, though

      Dereferencing constant references incorrectly (such as using an array subscript on a constant hash reference, or vice versa) will be trapped at compile time.
      implies that a hash constant can be created.

      Here's how it's done:

      use constant Books_I_Own => { "The Wit and Wisdom of Mark Twain" => 1, "Attack of the Deranged Mutant Killer Monster Snow Goons" => 1, "Unix Power Tools" => 1, }
      and from the debugger
      DB<2> x Books_I_Own 0 HASH(0x1c831b8) 'Attack of the Deranged Mutant Killer Monster Snow Goons' => 1 'The Wit and Wisdom of Mark Twain' => 1 'Unix Power Tools' => 1
      ...so it is possible to have a constant set, if a hash is a set.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        Yep. A limitation of that syntax is that, whilst the hash is a constant, it contents are not.

        [14:26:14.28] c:\test>p1 use constant HASH=>{ a=>1, b=>2 };; print %{ +HASH };; a 1 b 2 HASH->{ c } = 3;; print %{ +HASH };; c 3 a 1 b 2 delete HASH->{ $_ } for keys %{ +HASH };; print %{ +HASH };;

        That can be solved using the same Hash::Util::lock_hash() method I mentioned above.


        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.