perl - find row index in 2D array using grep

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,719
Reaction score
529
Hi All,
Need to search the first column of a 2D array and return the row index.

Code:
my @array = (['a','b','c'],['d','e','f'],['g','h','i']);
my @index = map { $_ -> [0] } @array;
my @result = grep { $index[$_] =~ /g/ } 0..$#index;
print @result;

Possible to grep without using map ?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Hi All,
Need to search the first column of a 2D array and return the row index.

Code:
my @array = (['a','b','c'],['d','e','f'],['g','h','i']);
my @index = map { $_ -> [0] } @array;
my @result = grep { $index[$_] =~ /g/ } 0..$#index;
print @result;

Possible to grep without using map ?

Perl:
use Data::Dumper;

my @array = (['a','b','c'],['d','e','f'],['g','h','i']);
my @index = map { $_ -> [0] } @array;
my @result = grep { $index[$_] =~ /g/ } 0..$#index;

my @result2 = grep { $array[$_][0] =~ /g/ } 0..$#array;

print Data::Dumper->Dump([\@array, \@result, \@result2], [qw(array result result2)]);

https://onlinegdb.com/pXWfsu5Xn
:)
 
Last edited:

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,719
Reaction score
529
@davidktw,
I did try this method. I even tried this $_->[0]. :(
It could be some other errors that masked this solution.
I googled and it found this post. :)

Thank you very much.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
@davidktw,
I did try this method. I even tried this $_->[0]. :(
It could be some other errors that masked this solution.
I googled and it found this post. :)

Thank you very much.
Which post are you referring to?

Execute the code provided before and observe how the various ways of dereferencing perl scalar, array and references work.
It will be kinda confusing at first, but you will get use to it once you practise more
https://onlinegdb.com/NTD5pwcuG
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Can please help explain the ?, $_, : and ().
Let me guess. The "?" is to compare and the $_ refer to the @array. But the ":" and "()" beats me.

$_ is alias to each element in the list passed into the map/grep function.

?: is commonly known as the ternary operator. It is found in a lot of other programming languages too.

<conditional expression> ? <then-expression> : <else-expression> can be written in its long form as

Perl:
if (<conditional expression>) {
  <then-expression>
}
else {
  <else-expression>
}

In the case of map, if you have read its documentation https://perldoc.perl.org/functions/map, you would have seen this statement "To omit an element, return an empty list ()"

Perl:
use Data::Dumper;

@all = map { $_ } (1..10);
print Dumper(\@all);
@none = map { () } (1..10);
print Dumper(\@none);
@even = map { $_ % 2 ? () : $_ } (1..10);
print Dumper(\@even);
@odd = map { $_ % 2 ? $_ : () } (1..10);
print Dumper(\@odd);

bIxhjao.png


For fun, this is how obfuscating you can write perl ;)
Perl:
use Data::Dumper;

@all=map{$_}1..10;
print Dumper(\@all);
@even2=map{@{([$_],[])[$_%2]}}1..10;
print Dumper(\@even2);
@odd2=map{@{([],[$_])[$_%2]}}1..10;
print Dumper(\@odd2);

bcV30bx.png
 
Last edited:
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top