Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: question on Arrays

by BioLion (Curate)
on Jul 29, 2010 at 15:44 UTC ( [id://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...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found