Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on accessing elemnts in a array within the PERL forums, part of the Web Development category; how to access the mid element in an array in perl?...
|
|||||||
| PERL PERL Scripts - Tips, Tricks, Issues, Latest News, PERL Resource and PERL related topics can be discussed in this forum. |
![]() |
| LinkBack | Thread Tools | Display Modes |
|
|||
|
Re: accessing elemnts in a array
its ok no probs it happens to every one sometimes.....
ok guys here is my answer which i have given in one of my interview... but the interviewer was not satisfied he was asking me if there were any direct funtions to do the same. step one sort the array in the ascending order step two obtain the first and last values to variables $low = $a[0]; $high = $#a; step three add the first and last element and divide by 2 and obtain the int() value of the result $mid = int(($low + $high)/2); step four $a[$mid] is the middle element of the array.... anyways it would be helpful if any one can give me any other answers or any reference for the same.. thanks in advance |
|
|||
|
Re: accessing elemnts in a array
#!/usr/bin/perl
use strict; my @array=qw(hello World Show must go on); my $count=scalar @array; print "\n Count :$count"; print "\n $array[($count/2)]"; exit; Ans is: Count :6 Show |
|
|||
|
Re: accessing elemnts in a array
Hi
you can do the same thing by this way also #!/usr/bin/perl use strict; my @array=qw(hello World Show must go on); print "\n $array[(($#array+1)/2)]"; exit; |
|
|||
|
Re: accessing elemnts in a array
Quote:
print $array[(scalar @array)/2]; |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help on Dynamic Array | nancyphilips | C and C++ | 8 | 12-25-2007 09:59 AM |
| Getting a list of ipaddress from the array using perl | anushya | Scripting | 1 | 02-10-2007 02:42 AM |
| cursor to array. | Barbie | Oracle | 1 | 02-06-2007 08:57 AM |