954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Accessing element indices in an arrays of arrays

Hi everyone!
So here's my problem:

Given an element, I need to get the index of the array in an array of arrays.

For example, I have the following array of arrays:

my @arrOfArr = (
           [ "1023", "1472", "0751651"],
           [ "1527", "1167", "2496111" ],
           [ "M167", "1412", "1761683" ],
		  );


Is there any way that when I know the value "1527", I will know its index in the arrays of arrays which is '1'?

Thus:
'1023' is at index 0;
'1527' is at index 1;
'M167' is at index 2;

If my question was not clear, I would be glad to answer any clarifications.
TIA.

gutchi
Newbie Poster
7 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 
Is there any way that when I know the value "1527"
use strict;
use warnings;

my @arrOfArr = (
           [ "1023", "1472", "0751651"],
           [ "1527", "1167", "2496111" ],
           [ "M167", "1412", "1761683" ],
		  );

my $data = '1527';
my $flag = 0;

for my $i ( 0 .. $#arrOfArr)
{
	for my $j ( 0 .. $#{$arrOfArr[$i]})
	{
		if ( $arrOfArr[$i][$j] eq "$data")
		{
			print "\n$data found at \$arrOfArr[$i][$j]";
			$flag = 1;
		}
	}
}

print "\n$data not found in the array" if (!$flag);
k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24
 

Thanks k_manimuthu!!
You're a genius! ^:)^

gutchi
Newbie Poster
7 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

Thanks k_manimuthu!!
You're a genius! ^:)^

gutchi
Newbie Poster
7 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: