Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Last Command (Break) 1

Status
Not open for further replies.

zincyo

Programmer
Jun 13, 2007
35
US
I am trying to run a loop which ends after it satisfies the If for the first time. I read that you are supposed to use the last command, similar to Break in C#. Can someone perhaps explain why the last is being ignored in this code, and where it should go. THanks!

for (my $i=0; $i < $#UndlyPx; $i++) {

if ($TheoPrice < $UndlyPx[$i]) {
print $UndlyPx[$i - 1] . "\n";
print $UndlyPx[$i] . "\n";

last;
}

}

}
close(FILE);
 
Hi

In the code fragment you posted there are two opening braces ( { ) and three closing braces ( } ). Because your code fragment looks correct, I suppose the error depends on the statement which opens the first brace, which you did not posted. If the missing statement is another [tt]for[/tt] you may want to use a label to tell to [tt]last[/tt] where to exit.

Feherke.
 
Not quite sure what you mean by that-- here is the entire section:

while (<FILE>) {
$csv->parse($_) or die "parse() failed: " . $csv->error_input();
my @data = $csv->fields();

if ($searchkey <= $#data && $data[$searchkey] =~ m/\Q$searchField\E/) {
print "\n\n";
print "Key: " . $data[$searchkey] . "\n";
print "mSym: " . $data[$field1key] . "\n" if $field1key <= $#data;
print "UndlyPx: " . $data[$field2key] . "\n" if $field2key <= $#data;
print "TheoPx: " . $data[$field3key] . "\n" if $field3key <= $#data;
print "\n\n";
}

my @UndlyPx= split(',', $data[$field2key]);
my @Underlying= split(',', $data[$field3key]);



for (my $i=0; $i < $#UndlyPx; $i++) {

#if ($TheoPrice > $UndlyPx[$i - 1] && $TheoPrice < $UndlyPx[$i] )
if ($TheoPrice < $UndlyPx[$i])
{
print $UndlyPx[$i - 1] . "\n";
print $UndlyPx[$i] . "\n";
last;

}

}
}

close(FILE);
 
Hi

Please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] tags. And do a favor to yourself too and indent your code.

You have to do just as the documentation shows for the [tt]last[/tt] :
Code:
[red]OUTERLOOP:[/red] while (<FILE>) {
[small]    $csv->parse($_) or die "parse() failed: " . $csv->error_input();
    my @data = $csv->fields();

    if ($searchkey <= $#data && $data[$searchkey] =~ m/\Q$searchField\E/) {
        print "\n\n";
        print "Key: " .     $data[$searchkey] . "\n";
        print "mSym: "    . $data[$field1key] . "\n" if $field1key <= $#data;
        print "UndlyPx: " . $data[$field2key] . "\n" if $field2key <= $#data;
        print "TheoPx: "  . $data[$field3key] . "\n" if $field3key <= $#data;
        print "\n\n";
    }

my @UndlyPx= split(',', $data[$field2key]);
my @Underlying= split(',', $data[$field3key]);



for (my $i=0; $i < $#UndlyPx; $i++) {

#if ($TheoPrice > $UndlyPx[$i - 1] && $TheoPrice < $UndlyPx[$i] )
if ($TheoPrice < $UndlyPx[$i])
 {
print $UndlyPx[$i - 1] . "\n";
print $UndlyPx[$i] . "\n";[/small]
last [red]OUTERLOOP[/red];
[small]
}

}
}

close(FILE);[/small]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top