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:
firstidx, 'array of arrays'

 



orange
Novice

Nov 26, 2012, 2:46 AM

Post #1 of 3 (1422 views)
firstidx, 'array of arrays' Can't Post

I have a 'two dimensional array' and need to find the index (row number) of an element in column.

I've tried this:

Code
 my $index= firstidx { $_ eq '7' } @servces[][1];


but it doesn't work. Can you please help me?


Chris Charley
User

Nov 26, 2012, 11:48 AM

Post #2 of 3 (1402 views)
Re: [orange] firstidx, 'array of arrays' [In reply to] Can't Post


Code
my $index= firstidx { $_->[1] == 7 } @servces;



(This post was edited by Chris Charley on Nov 26, 2012, 11:49 AM)


BillKSmith
Veteran

Nov 26, 2012, 11:55 AM

Post #3 of 3 (1400 views)
Re: [orange] firstidx, 'array of arrays' [In reply to] Can't Post

Strictly speaking, perl does not support multi-dimensional arrays. Proper use of references largely hides this fact.

In your case, the symbol @services refers to a single dimensional array. Each element of that array is a reference. Each reference refers to a list of scalars (one "row" in your notation). The data that you think of as 'column 1" consists of the second (indexes start with zero) element of each of these arrays. To treat that colulmn as a list, you must explicitly extract it.


Code
my @col1 = map ($_->[1]) @services; 
my $index= firstidx { $_ eq '7' } @col1;


Update: Chris Charley neatly combined these two statements into one.
Good Luck,
Bill

(This post was edited by BillKSmith on Nov 26, 2012, 9:16 PM)

 
 


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

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