469,276 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,276 developers. It's quick & easy.

Finding an element in an unordered list

Dear friends
i would like to know how do get back the keys in hashes using a print statement
and also how do we get the number of elements in a list and their indexes
eg: if i have
Expand|Select|Wrap|Line Numbers
  1. print (("nothing",255, 123, 555)[2]); it prints 123
but now that i want to know which element in the list is 123?. is there a way out to do it
please reply
Oct 6 '07 #1
3 1716
pbmods
5,821 Expert 4TB
Heya, rathankar. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=perl]
Perl code goes here.
[/CODE]

I'm going to go ahead and move this thread to the Perl forum, where our resident Experts will be better able to help you out.
Oct 6 '07 #2
numberwhun
3,503 Expert Mod 2GB
Dear friends
i would like to know how do get back the keys in hashes using a print statement
and also how do we get the number of elements in a list and their indexes
eg: if i have
Expand|Select|Wrap|Line Numbers
  1. print (("nothing",255, 123, 555)[2]); it prints 123
but now that i want to know which element in the list is 123?. is there a way out to do it
please reply
First, I completely back up pbmods with regards to the code tags. They are a best practice here on TSDN and should always be used to enclose any posted code.

As for your issue, you will want to read about the keys function that comes with Perl. That is how you access the keys in a hash.

And while you are there, you may want to read the rest of the wonderful documentation they have their, including the perlfunc page, which goes over the standard Perl functions.

Regards,

Jeff
Oct 6 '07 #3
KevinADC
4,059 Expert 2GB
Dear friends
i would like to know how do get back the keys in hashes using a print statement
Expand|Select|Wrap|Line Numbers
  1. print  keys %hash;
and also how do we get the number of elements in a list and their indexes
eg: if i have

Expand|Select|Wrap|Line Numbers
  1. print (("nothing",255, 123, 555)[2]); it prints 123
  2.  
Yes, index 2 is the third element (123) because perl arrays start at index 0 (zero).


The number of elements in an array:

Expand|Select|Wrap|Line Numbers
  1. my $n = @array;
  2. print $n;
but now that i want to know which element in the list is 123?. is there a way out to do it
please reply
You have to loop through the list/array and count where you are in the list/array to know the index number. There is no builtin function in perl5 to do that but there might be in perl6, which is still in development.

Also read the documents Jeff linked you to.
Oct 6 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by ElkGroveR | last post: by
15 posts views Thread by rhino | last post: by
7 posts views Thread by Nick | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.