perl exit loop

169

perl exit loop -

# Basic syntax:
last;
# Where last is analogous to break in Python, Bash, R, etc

# Example usage:
for my $i (0 .. 5) {
	print "index is $i\n";
    # Exit the for loop if the index equals 3
	if ($i == 3) {
		last;
    }
}

perl exit loop example -

my $counter = 1;
foreach $foo (sort {$importHash{$b} <=> $importHash{$a}} (keys %importHash))
{
  # push the string $foo onto the @tags array
  push @tags, $foo;
  
  # break out of our perl loop when the counter reaches 10
  last if ($counter++ == 10);
}

Comments

Submit
0 Comments