Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: question on Arrays

by BioLion (Curate)
on Jul 29, 2010 at 15:44 UTC ( #851972=note: print w/ replies, xml ) Need Help??


in reply to question on Arrays

Maybe better to use a hash if you are going to check existence?

use strict; use warnings; ## buffering off ++$|; ## for storing numbers entered so far my %hsh; while (1){ print "Enter something (or nothing if you are bored):\n"; ## get input my $num = <>; chomp $num; ## exit infinite loop if the user hits enter twice last if (!defined$num || $num eq ''); ## check against previous input if (exists $hsh{$num}){ print "Seen >$num< before, try again.\n"; next; } else { print "Oooh >$num< is new, thanks!\n"; ++$hsh{$num}; } } print +(join "\n\t", "Finished! You entered :", sort keys %hsh);

Sample terminal:

Enter something (or nothing if you are bored): 4 Oooh >4< is new, thanks! Enter something (or nothing if you are bored): foo Oooh >foo< is new, thanks! Enter something (or nothing if you are bored): bar Oooh >bar< is new, thanks! Enter something (or nothing if you are bored): 4 Seen >4< before, try again. Enter something (or nothing if you are bored): Finished! You entered : 4 bar foo
Just a something something...


Comment on Re: question on Arrays
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://851972]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2015-07-28 21:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (259 votes), past polls