Please, don't remove code after you post it.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.