For key-value pair type of lists, hashes are the way to go. I had shown the other way using a list, which does not need the 'key-value' caveat.
However for your case, Mini, you need to tread the hash path as shown by Sudarshan. Here is a code snippet you can try. #!/usr/bin/perl my($hash) = {a=>1, b=>2, c=>3, d=>4}; my($key) = 'b'; my($index) = indexOf($hash,$key); print ("Index of $key in the hash is = $index"); sub indexOf{ my($myHash) = shift; my($val) = shift; if(exists $myHash->{$val}){ return $myHash->{$val} ; } else{ return("undef"); #WE should not get here if a match is found } } - Rex -----Original Message----- From: Sudarsan Raghavan [mailto:[EMAIL PROTECTED]] Sent: Wednesday, October 03, 2001 8:59 AM To: [EMAIL PROTECTED] Cc: Rex Arul; [EMAIL PROTECTED] Subject: Re: index of an array element [EMAIL PROTECTED] wrote: > Rex, in you r previous mail, we explicity need to give > $str = 'd=4' ; .................It is simple to give it here as I know the > key=value pair for d i.e.. d=4. But I want to use function with very large If it is a list of key=value pairs a hash is the ideal way to go about it. > > lists, and I have to search my list for the key only i.e.. "d". As I had already mentioned the search for key "d" is simply $<hashname>{d} > > > thus, what I need is : > $c = some_func(@list, "d"); .............where some_func should return > me 3......... Can anybody help me. Could you pls explain as to why you need only the index? If it is to check for its existence a defined ($<hashname>{d}) will do the job for you. (perldoc -f defined) > > > Please suggest me. > > Regards, > Mini. > > "Rex Arul" <[EMAIL PROTECTED]> on 10/03/2001 06:02:30 PM > > To: "Sudarsan Raghavan" <[EMAIL PROTECTED]>, Mini Dwivedi/HSS@HSS > cc: [EMAIL PROTECTED] > > Subject: Re: index of an array element > > But that would work only for key=value type of situations. > > If @list = (2,'a',100,'cat') > > then you cannot rely on Hashes because order cannot be preserved. At such > instances, you might need to code a custom function as shown in my previous > mail. > > Right? > > -- Rex > > ----- Original Message ----- > From: "Sudarsan Raghavan" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Cc: <[EMAIL PROTECTED]> > Sent: Wednesday, October 03, 2001 8:18 AM > Subject: Re: index of an array element > > > [EMAIL PROTECTED] wrote: > > > > > Hi , > > > > > > Say I have an array in perl, > > > > > > @list = ("a=3", "b=5", "c=8", "d=9", "e=2", "f=0"); > > > > looking at your list a hash seems like a better option, the hash will > be like > > > > %hashlist = ( > > a => 3, > > b => 5, > > c => 8, > > d => 9, > > e => 2, > > f => 0); > > the reason being, you are associating the numeric values (3, 5, 8 > etc.) to the > > characters (a, b, c etc.) > > a hash (associative array) would be more useful here. Is my assumption > correct? > > > > To access the element 'd' you will say $hashlist{d} > > You can get more information on hashes from perldoc perldata > > > > > > > > > > > Now I want to find the index of the element "d=9" ( Index of d=9 is 3 > here , as > > > we all know ). > > > > > > How do we do that ?? > > > > > > In perl what I can find that there exist a function "index", which > returns > > > position of the substring in string..........like, > > > > > > $c = index(@list, "d" ); > > > > > > > > > > > This function will return me the value 13 or 12 perhaps....as the > substring > > > "d=9" begins from 13th position in the @list. > > > > > > My requirment is : > > > > > > $c = some_func(@list, "d"); .............where some_func should return > me > > > 3......... Can anybody help me. > > > > > > TIA, > > > -Mini. > > > > > > -- > > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > -- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]