CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
accessing nth element in perl arrays

 



Tarun
New User

Apr 8, 2010, 5:36 AM

Post #1 of 5 (1011 views)
accessing nth element in perl arrays Can't Post

Hi all..
Context:
I am writing arrays in perl..Which looks like this
my @1 = ('X','Y','Z');
my @2 = ('P','Q','R');
my @3 = ('A','B','C');
now i define a new array to make these arrays multidimensional
my @4 = (\@1,\@2,\@3);
Problem:
I want to perform operations on the third element of arrays 1,2 & 3 i.e Z,R,C but i am unable to access them.I actually want to check the third element with some value and if the value matches then want to put this array in certain category.Actually there are a number of arrays

Please help..thanks in advance
Tarun


rovf
Veteran

Apr 8, 2010, 6:04 AM

Post #2 of 5 (1008 views)
Re: [Tarun] accessing nth element in perl arrays [In reply to] Can't Post


Quote
now i define a new array to make these arrays multidimensional


This does not make a multidimensional array, but a one-dimensional array with 9 elements.

Perl has only one-dimensional arrays, but its elements can be array references, so maybe this is the way for you to go.

See the perlref man-page.


Tarun
New User

Apr 8, 2010, 6:31 AM

Post #3 of 5 (1007 views)
Re: [rovf] accessing nth element in perl arrays [In reply to] Can't Post

Well..Thx for the reply rovf
May be i am wrong ..but the problem remains the same...I want to access the third element of each array and compare it with certain value. for example if i see the third element of @1 which is z ..now i want to compare

if third element of array 1 = z then
do some operation 'lambda'
else exit.

The problem is the number of arrays@1,@2...are many..and so i want to generalize the term..i.e check the third element of each array and then compare..


rovf
Veteran

Apr 8, 2010, 7:14 AM

Post #4 of 5 (1002 views)
Re: [Tarun] accessing nth element in perl arrays [In reply to] Can't Post

That's exactly *why* I suggested that you don't use arrays, but array references!


FishMonger
Veteran / Moderator

Apr 8, 2010, 8:53 AM

Post #5 of 5 (995 views)
Re: [Tarun] accessing nth element in perl arrays [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my @a = ('X','Y','Z');
my @b = ('P','Q','R');
my @c = ('A','B','C');
my @d = (\@a,\@b,\@c);

print Dumper \@d;

if ( $d[0][2] eq 'Z' ) {
print "That's what I wanted\n";
# do some operation 'lambda'
}
else {
print "That's NOT what I wanted\n";
exit;
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives