Results 1 to 8 of 8

Thread: accessing elemnts in a array

  1. #1
    Expert Member
    Join Date
    Apr 2006
    Answers
    124

    accessing elemnts in a array

    how to access the mid element in an array in perl?


  2. #2
    Contributing Member
    Join Date
    Mar 2007
    Answers
    34

    Re: accessing elemnts in a array

    sorry i don't have any knowledge about pearl this time


  3. #3
    Contributing Member
    Join Date
    Feb 2007
    Answers
    48

    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


  4. #4
    Contributing Member
    Join Date
    Feb 2007
    Answers
    48

    Re: accessing elemnts in a array

    Hi,

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

    Regards,
    Ganesh


  5. #5
    Expert Member
    Join Date
    Apr 2006
    Answers
    124

    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


  6. #6
    Junior Member
    Join Date
    Oct 2007
    Answers
    5

    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


  7. #7
    Junior Member
    Join Date
    Oct 2007
    Answers
    5

    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;


  8. #8
    Junior Member
    Join Date
    Feb 2009
    Answers
    1

    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];


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact