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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF THEN ELSE IF OMG!!!! 1

Status
Not open for further replies.

VspecGTR3

Technical User
Feb 3, 2003
62
0
0
US
@somelist = ("1", "2", "3", "4");
foreach $list (@somelist)
if($list eq 4)
{open FIL, ">>1file.txt")
}
else {
(open FIL, ">>2file.txt")
}}
--------------------------
now heres the problem the program is gonna open both files 1file and 2file cause the looops will test each element but how do i make it that if 1 is true the entire conditional (if statment) is true regardless.
soo in other words............
test all the element in list and when its all tested and done then execute the commands (the open functions)

is this possible....
 
open FIL, ">>2file.txt" ;
@somelist = ("1", "2", "3", "4");
foreach $list (@somelist)
if($list eq 4)
{
close FIL ;
open FIL, ">>1file.txt";
}
 
If you're saying that you want to open one file if the array contains the value 4, and another if it doesn't:

[tt]@somelist = ("1", "2", "3", "4");

if (grep { $_ eq "4" } @somelist) {
open FIL, ">>1file.txt";
}
else {
open FIL, ">>2file.txt"
}
[/tt]
 
Rosenk once again u prevail for me i wish i can give u 40 stars ;) i never though of using the grep command (ill have to refer to my Perl for Meathead's book) i came up with another code taht seem to work what you take any flaws?
=========================================================
@test = ("1", "2", "3", "4");

sub testing {
foreach $t (@test) {
if($t eq 3){
return 1;
}
}
}
if(testing eq 1){
print "true"
} else {
print "false"
}
=========================================================

i made a second conditional (i replaced the open with print commands)
 
That would certainly work.

You can also use [tt]last[/tt] to get out of the foreach loop without using a sub.

[tt]@test = ("1", "2", "3", "4");

my $found = 0;

foreach $t (@test) {
if ($t eq "3") {
$found = 1;
last;
}
}

print "Yes\n" if $found;
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top