Perl need the right grep operator to match value of variable
Posted
by
iaintunderstand
on Stack Overflow
See other posts from Stack Overflow
or by iaintunderstand
Published on 2012-06-30T13:22:06Z
Indexed on
2012/06/30
15:16 UTC
Read the original article
Hit count: 156
I want to see if I have repeated items in my array, there are over 16.000 so will automate it There may be other ways but I started with this and, well, would like to finish it unless there is a straightforward command. What I am doing is shifting and pushing from one array into another and this way, check the destination array to see if it is "in array" (like there is such a command in PHP).
So, I got this sub routine and it works with literals, but it doesn't with variables. It is because of the 'eq' or whatever I should need. The 'sourcefile' will contain one or more of the words of the destination array.
my @destination = ('hi', 'bye');
sub in_array
{
my ($destination,$search_for) = @_;
return grep {$search_for eq $_} @$destination;
}
for($i = 0; $i <=100; $i ++)
{
$elemento = shift @sourcefile;
if(in_array(\@destination, $elemento))
{
print "it is";
}
else
{
print "it aint there";
}
}
Well, if instead of including the $elemento in there I put a 'hi' it does work and also I have printed the value of $elemento which is also 'hi', but when I put the variable, it does not work, and that is because of the 'eq', but I don't know what else to put. If I put == it complains that 'hi' is not a numeric value.
© Stack Overflow or respective owner