I want to parse a file and push a line into an array only if it begins with a key that is part of a hash. All others lines will have to go in a different array.
I tried to use a foreach loop:
while ($line = <FILE>){
foreach $key (keys %hash){
if ($line =~ /^$key/){
push (@array1, $line);
}
else {
push (@array2, $line);
}
}
}
The problem is that non matching lines will be pushed n times (n being the number of keys in my hash) in @array2 rather than 1 time.
Is there a way around this ?
Basically I would need to include the foreach within the if.
Any idea ??
U.
I tried to use a foreach loop:
while ($line = <FILE>){
foreach $key (keys %hash){
if ($line =~ /^$key/){
push (@array1, $line);
}
else {
push (@array2, $line);
}
}
}
The problem is that non matching lines will be pushed n times (n being the number of keys in my hash) in @array2 rather than 1 time.
Is there a way around this ?
Basically I would need to include the foreach within the if.
Any idea ??
U.