Home > other >  perl for loop issue with understanding it
perl for loop issue with understanding it

Time:06-21

I'm looking at some old Perl code and I'm trying to figure out what does this statement exaclty mean - I have done programming, but not in Perl. Having a hard time with what this for statement actually means.

for ($xx = $x 1 ; $contents[$xx] !~ m/^\:1S\:XXX/  ; $xx  )

Is this looping until it finds :1S:XXX

If not, then what does this mean?

Can someone help with this?

Thanks

CodePudding user response:

This might be more clear:

$xx = $x 1;
$xx   until substr($contents[$xx], 0, 7) eq ':1S:XXX';

However both this and your for loops forever if :1S:XXX isn't found. So I would consider this which leaves $xx not defined if the searched string isn't found at the beginning of an array element at or after the starting point in $x 1.

use List::Util 'first';
my $xx = first { $contents[$_] =~ /^:1S:XXX/ } $x 1 .. @contents-1;

In perl regexes : can have \ in front but don't need it.

  •  Tags:  
  • perl
  • Related