Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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 about the Monastery: (7)
As of 2014-07-30 01:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls