Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Re: Pulling by regex II

by JayBonci (Curate)
on Dec 14, 2002 at 06:49 UTC ( [id://219835]=note: print w/replies, xml ) Need Help??


in reply to Re: Pulling by regex II
in thread Pulling by regex II

Alternately, you could build a month => month_num hash with a one-line map statement ala:
my %months = map { (qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec +/)[$_] => $_}(0..11);
I like doing stuff like this because it saves you the possible typo(s) if you userstand how map works. I'm always looking for more efficient ways to do it, however. Can anyone think of any?

    --jb

Replies are listed 'Best First'.
Re: Re: Re: Pulling by regex II
by BrowserUk (Patriarch) on Dec 14, 2002 at 08:12 UTC

    Why use an array and map and a hash. A string and index will do it.

    my $month_num = index( 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov De +c', $month_str)/4;

    Examine what is said, not who speaks.

      I'd want to get a hash out because of efficiency and grace if I were to call it multiple times. I wouldn't want to slug around that entire index function call invokation every time I wanted to lookup a month_name to month_num transform.

          --jb

        I was talking crap! :(


        Examine what is said, not who speaks.

      my $month_num = index('JanFebMarAprMayJunJulAugSepOctNovDec', $month_str)/3;
      Now we're talking.

        use constant MONTHS => 'JanFebMarAprMayJunJulAugSepOctNovDec'; ... my $month_num = index(MONTHS, $month_str)/3;

        Better still?

        Unfortunately, this (against my expectations) turns out to be between 30% & 50% slower than the hash solution.

        It uses much less momory, but it seems that hashing a 3 char string, indexing and returning the value is faster than a linear search for a 3 char string + a division.

        Surprised me


        Examine what is said, not who speaks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found