I have a method where I give two arguments: one array and one string. The problem is when I initialise two different variables, one for the array and one for the string, I get the first element of the array in the first variable and the second element in the second variable. My question is how can I get the whole array in a variable and the string in a different variable?
Analyze.pm
sub analyze {
my $self = shift;
my ($content, $stringToSearch) = @_;
# my ($stringToSearch) = @_;
print "$stringToSearch";
if (!defined($stringToSearch) or length($stringToSearch) == 0) { die 'stringToSearch is not defined yet! ' }
foreach my $element ($content) {
#print "$element";
my $loc = index($element, $stringToSearch);
# print "$loc\n";
given ($stringToSearch) {
when ($stringToSearch eq "Hi") {
if ($loc != 0) {
print "Searched word is Hi \n";
} else {
print "No word found like this one! "
}
}
#when ($stringToSearch == 'ORIENTED_EDGE') {
# print 'Searched word is ORIENTED_EDGE';
#} # Printed out because i dont need it now!
}
break; # For testing
}
}
example.pm
my @fileContent = ('Hi', 'There', 'Its', 'Me')
my $analyzer = Analyze->new();
$analyzer->analyze(@fileContent, 'Hi');
When I change $content
to @content
it puts all the values of the array and the string in @content
I hope someone is able to help me. I'm a beginner in Perl. Thanks in advance
break
is not a Perl keyword. What you want here is probablylast
.