Geeks Talk

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.

accessing elemnts in a array

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?...

Go Back   Geeks Talk > Web Development > PERL
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

PERL PERL Scripts - Tips, Tricks, Issues, Latest News, PERL Resource and PERL related topics can be discussed in this forum.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-21-2007
Expert Member
 
Join Date: Apr 2006
Location: India, Hyderabad
Posts: 130
Thanks: 19
Thanked 48 Times in 18 Posts
bvani will become famous soon enough
accessing elemnts in a array

how to access the mid element in an array in perl?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-22-2007
Contributing Member
 
Join Date: Mar 2007
Location: renukoot(sonbhadra)
Posts: 39
Thanks: 5
Thanked 4 Times in 4 Posts
rohit dwivedi9450 is on a distinguished road
Re: accessing elemnts in a array

sorry i don't have any knowledge about pearl this time
Reply With Quote
  #3 (permalink)  
Old 03-22-2007
Contributing Member
 
Join Date: Feb 2007
Location: Delhi
Posts: 51
Thanks: 3
Thanked 12 Times in 6 Posts
smganesh is on a distinguished road
Re: accessing elemnts in a array

Hi Bvani,

I think, You have posted your query in a wrong forum. Better, post it in appropriate forum.

Regards,
Ganesh
Reply With Quote
  #4 (permalink)  
Old 03-22-2007
Contributing Member
 
Join Date: Feb 2007
Location: Delhi
Posts: 51
Thanks: 3
Thanked 12 Times in 6 Posts
smganesh is on a distinguished road
Re: accessing elemnts in a array

Hi,

I am sorry. It was my mistake. I though this is a QTP forum

Regards,
Ganesh
Reply With Quote
  #5 (permalink)  
Old 03-22-2007
Expert Member
 
Join Date: Apr 2006
Location: India, Hyderabad
Posts: 130
Thanks: 19
Thanked 48 Times in 18 Posts
bvani will become famous soon enough
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
Reply With Quote
  #6 (permalink)  
Old 02-06-2008
Junior Member
 
Join Date: Oct 2007
Location: India
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
amitbhosale is on a distinguished road
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
Reply With Quote
  #7 (permalink)  
Old 02-06-2008
Junior Member
 
Join Date: Oct 2007
Location: India
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
amitbhosale is on a distinguished road
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;
Reply With Quote
  #8 (permalink)  
Old 02-06-2009
Junior Member
 
Join Date: Feb 2009
Location: In dia
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
vinoth.g is on a distinguished road
Re: accessing elemnts in a array

Quote:
Originally Posted by amitbhosale View Post
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;
We can also use like this,
print $array[(scalar @array)/2];
Reply With Quote
Reply

  Geeks Talk > Web Development > PERL

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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


All times are GMT -4. The time now is 05:14 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved