On Sun, 15 May 2005, Frank wrote:

> If i know the element of array, can I get the numeric index  of this
> element?

In general, you'd have to write code to "walk" through the array, then
make a note of the index when you get the value you want. Something like
this might do what you're asking for (this is untested! may have bugs!):

    my $element = "Kaplan";

    for (0 .. $#aliases) {
        if ( $aliases[$_] eq $element ) {
            print "The index of $element in [EMAIL PROTECTED] is $_.";
        }
    }

But generally, this isn't how Perl coders write things.

If the problem you're working on makes it natural to work on elements
within a set, then most of the time a hash is a better choice than an
array. The clear advantage is that you can scrap most of the above code,
and just write something like:

    print "Element $element in hash \%aliases is $aliases{$element}.";

The downside is that the collection is unordered, so if you're trying to
preserve the sequence of elements in the collection, then you have to
turn to a more complex data structure (say, a hash of hashes, where one
of the nested items is the sequence and another is a label, etc). But
making random access lookups into the set is so much easier with a hash
that it's usually worth it to do things this way.


All of this and much more is covered in introductory books like
_Learning Perl_ or, if you want to focus on biological analysis,
_Beginning Perl for Bioinformatics_.



-- 
Chris Devers

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to