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.

Recommended Answers

All 3 Replies

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.