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

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.

Replies are listed 'Best First'.
Re^4: Pulling by regex II
by tadman (Prior) on Dec 14, 2002 at 20:10 UTC
    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.

Re: Re: Re: Re: Pulling by regex II
by JayBonci (Curate) on Dec 14, 2002 at 08:25 UTC
    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.