I made a few mods that you might find instructive. And added an undocumented option 6. Add a few accounts and see what that does!
#!/usr/bin/perl -W use strict; use 5.010; use Data::Dumper; my $heading = <<HEAD; A script that allows a user to perform system administration tasks Programmer name: Michael A. Gregor Course: COSC 146 Lab#: 7 Due Date: 3/10/16 lab7.pl HEAD print $heading; my %passwords = (tom => "BigApple"); my %types = (tom => "admin"); my $menu = <<MENU; System Administration Tasks 1- Create a new account 2- Delete an account 3- Change an account password 4- List users in alphabetical order 5- Exit the system MENU while (print $menu) { my $choice = prompt4input ("Make your choice: "); if ($choice == 1) {createAccount(); next;} if ($choice == 2) {deleteAccount(); next;} if ($choice == 3) {changePassword(); next;} if ($choice == 4) {listUsers(); next;} if ($choice == 5) {print "Good-bye!";exit(0);} if ($choice == 6) {print Dumper (\%passwords); print Dumper (\%types); next;} print ("Bad Choice...Try Again!\n"); } sub prompt4input #returns response to a prompt string { my $prompt = shift; print $prompt; my $input = <STDIN>; $input =~ s/^\s*//; #remove leading whitespace $input =~ s/\s*$//; #remove trailing whitespace return $input; } sub createAccount { print "Creating Account...\n"; my $userID = prompt4input ("Enter your userID: "); my $password = prompt4input ("Enter your password: "); if (exists $passwords{$userID}) { print "Error: Duplicate user id: $userID "; return; #### update added } my $account_type; while ($account_type = prompt4input( "Enter the account type, admin (or regular): "), $account_type !~ /admin|regular/) { $passwords{$userID} = $password; $types{$userID} = $account_type; } print "Account for \'$userID\' was successfully created \n"; } sub deleteAccount { my $userID = prompt4input ("Enter your userID: "); my $password = prompt4input ("Enter your password: "); if ($passwords{$userID} != "$password") { print "invalid user id/password combination!\n"; return; } if ($types{$userID} != "admin") { print "$userID is not an admin...can not delete!\n"; return; } delete $passwords{$userID}; delete $types{$userID}; } sub changePassword { my $userID = prompt4input ("Enter your userID: "); my $password = prompt4input ("Enter your password: "); if ($passwords{$userID} != "$password") { print "invalid user id/password combination!\n"; return; } my $newPassword = prompt4input ("Enter new password: "); $passwords{$userID} = $newPassword; } sub listUsers { # sort the users in descending order my $counter = 1; for my $userID (reverse sort keys %passwords) { print "$userID\t"; print "\n" if $counter++ % 3 == 0; #divisible by 3 evenly } print "\n"; }
In reply to Re: How do you sort keys of a hash in descending order and print 3 per line?
by Marshall
in thread How do you sort keys of a hash in descending order and print 3 per line?
by MikeyG
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |