|
|
Question : grep
|
|
I use grep to check if a value is present in an array like this: my $Hier = grep /$found_value/,@From;
Let's say @from contains "6,7,8,9,10" and $found_value has the value 1.
$Hier should have the value 0 with this example. Though it finds the 1 from the 10 in the array, which isn't the idea. $Hier should only return the value 1 if there is an exact match with the value in the array.
How to solve this ?
|
Answer : grep
|
|
If you use 'last' in a grep-block, the loop terminates without adding the current element to the output array. If you were thinking along the lines of:
$count = grep { last if $_ eq 1 } @from;
IT WOULD NOT WORK!
My 'last' example was not using 'grep'. The $found variable contains 1 if the item is found and contains '' if the item is not found. It's a little different from the 'grep' examples: usually one uses grep because you want to do something with the array that it produces, hopefully to do more than count the elements to determine if there are zero vs one or more, but if that's all you wanted to do it does work.
|
|
|
|
|