Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

populate a hash, with limits set by user?

by Anonymous Monk
on Sep 12, 2000 at 05:10 UTC ( [id://32043]=perlquestion: print w/replies, xml ) Need Help??

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

I need to be able to enter in variables from the user in a hash using a for loop with a limit set by the user. Can anyone help me with this?

2006-03-07 Retitled by g0n, as per Monastery guidelines
Original title: 'Hash'

Replies are listed 'Best First'.
Re: populate a hash, with limits set by user?
by chromatic (Archbishop) on Sep 12, 2000 at 08:11 UTC
    If it's a homework assignment, why not impress your teacher? Here's something to get you extra credit:
    #!/usr/bin/perl -w use strict; print "Limit? "; my %input = map { print "Key = Value: "; split(/=/, <STDIN>); } (1 .. +(<STDIN>)); print %input, "\n";
    Guaranteed to impress old LISP hackers.
      you make my brain hurt ;).
      Slick. I like it. Maybe the regex should handle optional space around the =
      use strict; # Of course! print "Limit? "; my %input = map { print "Key = Value: "; split /\s*=\s*/, <STDIN> } ( +1 .. (<STDIN>) ); use Data::Dumper; # Pretty print 8^) print Data::Dumper->Dump([\%input], ["input"]), "\n";
Re: populate a hash, with limits set by user?
by BastardOperator (Monk) on Sep 12, 2000 at 05:38 UTC
    I probably could do a little better if your question was a little more detailed, but...
    print "What's the limit?: " chomp($limit = <STDIN>); foreach (1 .. $limit) { print "enter variable $_ : "; chomp($somehash{$_} = <STDIN>); }
    If this doesn't do it for ya, I'll try again ;)

    Looking at BlaisePascal's post, it kinda does look like a homework assignment, but I'll be nice and leave this here anyway.
RE: populate a hash, with limits set by user?
by little (Curate) on Sep 12, 2000 at 08:51 UTC
    ok, ok, I know, the following looks much like 'baby perl' but works fine for me when I need something like described above
    #!/usr/local/bin/perl use strict; $|++; my $input = 1; my %myhash; my @mykeys; my $key; print "enter names and values!\nleave name field emtpy and press 'Ente +r' when complete"; while ($input) { print "\nname :"; $input = <STDIN>; if ($input) { chop $input; next if (!$input); print "name already assigned" and $input=1 and next if ($myhas +h{$input}); while (!$myhash{$input}) { print "value :"; $myhash{$input} = <STDIN>; chop $myhash{$input}; } } } print "\n\nresults\n=======\n"; @mykeys = keys %myhash; foreach $key (@mykeys) { print "name :".$key." value: ".$myhash{$key}.". .\n"; }
    But all suggestions for improvement are very welcome. See, why I've choosen this name? :-)
Re: populate a hash, with limits set by user?
by BlaisePascal (Monk) on Sep 12, 2000 at 05:35 UTC
    Maybe... But I'm gonna need more information...

    What do you mean "enter in variables from the user"? How does the user set the limit? Why does this look so much like a homework assignment?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-03-29 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found