yueli711 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I want to print ""dino". Thank you in advance!

my @array = qw /fred barney betty dino wilma pebbles bamm-bamn/; my $result=&which_element_is("dino", names); sub which_element_is{ my($what, @array)=@_; foreach (0..$#array){ if($what eq $array[$_]){ print "$_\n"; return $_; } } print "99\n"; -1 } print "$result\n"

Replies are listed 'Best First'.
Re: print problem
by hippo (Archbishop) on May 08, 2018 at 13:24 UTC

    No strict and no warnings means that you are not alerted to the mistake of passing the bareword names to which_element_is().

    use strict; use warnings; my @array = qw /fred barney betty dino wilma pebbles bamm-bamn/; my $result = which_element_is ("dino", @array); sub which_element_is { my ($what, @array) = @_; foreach (0 .. $#array) { if ($what eq $array[$_]) { print "$_\n"; return $_; } } print "99\n"; -1; } print "Item $result is $array[$result]\n"

    Always use strict and warnings. Always.

      Thank yo so much for your great help! It works!

Re: print problem
by thanos1983 (Parson) on May 08, 2018 at 13:58 UTC

    Hello yueli711,

    Welcome to the Monastery. Also read a similar question asked before on the forum that answers your problem with different multiple ways to solve it.

    See How do I find the index of a specific array value?.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!