#!/usr/bin/perl # always use strict and warnings use strict; use warnings; #hash to store names my %name_hash; print "Type 'exit' at any time to end this program\n"; #loop to add new users while(1) { print "Please enter your first name\n"; chomp (my $fname =); exit if $fname eq 'exit'; print "Please enter your last name\n"; chomp (my $lname =); exit if $lname eq 'exit'; print "Please enter a password\n"; chomp (my $password =); exit if $password eq 'exit'; my $username = "$lname, $fname"; $name_hash{$username} = $password; print "Please enter 'done' to start seachings or hit enter to add another user.\n"; chomp (my $ending =); exit if $ending eq 'exit'; last if $ending eq 'done'; } print "To search for a user please type in a name and hit enter.\n"; while() { chomp; exit if $_ eq 'exit'; next unless $_ =~ /\w/; #require at least one letter in the query my $found = 0; foreach my $key (keys %name_hash) { # guard the regex with \Q so that users can't entere their own regex if ($key =~ m/\Q$_\E/i) { print "The username is '$key' and the password is '" . $name_hash{$key} . "'\n"; $found++; } } if ($found) { print "I found $found users matching that search. "; } else { print "Sorry no users where found matching '$_'. "; } print "Please try another name to search or type exit to end\n"; }