[Charlotte.PM] find index of array element

diona kidd diona at studio12a.com
Tue Mar 22 08:01:46 PST 2005

just realized this didn't go to the list...



Thanks for the reply. This is generally a pretty quiet list so I wasn't
sure how long it would be...

I guess I could do it that way, but it seems to make it more complex
than it probably should be. The bonus of arrays are that they are
quicker although that may be negligible here. I also have the same habit
of typing '%' before even thinking about it. :)

I'm creating a wizard that has a next button (in Mason). Each element of
the array is the next tab in the interface and is used to create the
href for the 'Next' button. I do have a hash that looks like so:

my %tab = ( summary     => 'summary.html',
            description => 'description.html',
            contact     => 'contact.html',
            venue       => 'venue.html',
            price       => 'price.html',
            schedule    => 'schedule.html',
            sale        => 'sale.html',
            access      => 'access.html',
            hold        => 'hold.html',
            commission  => 'commission.html'

It's used to determine which component to call next and create an href
in the Mason code...

# figure out which component to call next
my $component = undef;
if( ! $r->param('tab') ) {
    $component = 'summary.html';
} elsif($tab{$r->param('tab')}) {
    $component = $tab{$r->param('tab')};
} else {
    # do something here?

Then I need to find the next tab to call in the 'next' href...

my @next = qw(summary description contact venue price schedule sale hold
# there's got to be an easier way, maybe with grep?
my $cnt = 0;
my $next = undef; # name of next tab
foreach(@next) {
    if($_ eq $r->param('tab') ) {
        $next = $next[$cnt+1];
    } else {
        $next = $next[0];

I guess the question is, which is cleaner?

On Tue, 2005-03-22 at 10:38 -0500, drewhead at drewhead.org wrote:
> Hash: SHA1
> > my @array = qw( item1 item2 item3 item4 );
> > find the index of item3. 
> The knock on grep is that it is pretty much looping though every
> like map would, which essientailly makes them the same as a foreach
> Those things don't tend to scale well.
> Why do you need to know an array index? Can you control the structure
of the array?
> If so makeing this a hash when you build the data structure where your
> keys are the element values of the array and the hash key value would
be a
> count scalar incremented with each assignment.  Then you could get the
> index, or more appropriately stated the ordinal position of an element
> assignment, simply by refrencing it's hash value.
> my %hash = { 
>     'item1' => 1,
>     'item2' => 2,
>     'item3' => 3,
>     'item4' => 4,
>            };
> my $index = $hash{'item3'};
> This assumes your array elements are unique.
> I know this isn't really what you asked, but depending on your
> it and how many times you need to lookup an index value it may be
worth it
> to build this structure even if you don't have control of the array
> itself.
> I find I only use arrays when I know I'm never going to care about the
> index of the elements and I know I'll never have need to look up a
> value.  Otherwise I type % without even thinking about it.  :)
> - -- 
>      Drew Dowling               Drewhead
>  drewhead at drewhead.org    |                    |    WWW      / \ Alpha
Phi Omega
> Concord, North Carolina   |                    |  Nimat     /   \
Gamma Lambda
> CLEMSON UNIVERSITY ALUMNI |                    | Apatschin /_____\
>       TIGER BAND!         |      VGAP4 Hosting at
> Comment: Public key available at http://www.drewhead.org
> uWO0eBT5R1D6NSR+gB+BgMM=
> =f23b

More information about the charlotte mailing list