f77coder has asked for the wisdom of the Perl Monks concerning the following question:
I'm converting some Matlab code into perl. Here's the Matlab code.
while ~feof(fileIDS); m1=fgetl(fileIDS); [a,b]=intersect(dataD{1},{deblank(m1)}); if (~isempty( a)) data=dataD{2}(b); elseif(~isempty(intersect(dataT{1},{deblank(m1)})) ) data=1.0; else data=0.0; end; end;
dataD is a 2D array, dataT is a 1D array
m is a scalar value read from a file
a=returns index if there is one and null if it doesn't and b=value at index a
Basically what this does is the following
intersection (2D data,scalar) if intersection exists not null a=index of location data= value of location at b elseif intersection (1D data,scalar) data=1.0 else data=0.0 endif
I'm reading through this http://perldoc.perl.org/perlfaq4.html#How-do-I-test-whether-two-arrays-or-hashes-are-equal%3f but this assumes that the arrays are of equal dimensions (1D).
I'd like to know what is the simplest and fastest way?
use 5.12.0; use strict; use warnings; use diagnostics; use List::MoreUtils; open my $TEST, '<', $f_Test or die "Could not open=> $f_Test : $!"; my $data; while (my $scalar=<$TEST>) { chomp($scalar); [my $a, my $b]=firstidx { $_ == $scalar } @arrayPFA; if(!exists $a) { $data=$arrayPFA[$b]; } elsif(if any { ! defined($scalar) } @arrayT;) { $data=1.0; } else { $data=0.0; }; }; close $TEST
Thanks for any help.
|
|---|