Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

print problem

by yueli711 (Sexton)
on May 08, 2018 at 13:17 UTC ( [id://1214212]=perlquestion: print w/replies, xml ) Need Help??

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 (Bishop) 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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1214212]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found