#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2016
    Posts
    25
    Rep Power
    0

    Exclamation Needing help with foreach loop in reference to using a has (please help ASAP!!)


    Below is a copy of the assignment that I'm currently working on, however, I'm completely confused
    because I can not seem to get the foreach loop to output the data from my hash. Not to mention, I cannot seem to get
    the users input to generate the values within my hash as well. I reached out to my professor and he provided me a foreach loop
    example but it appears to be for an array; therefore, leaving me even more confused.

    Can some please review my code and provide me some assistance and/or guidance??

    Assignment Problem

    Create a hash to store the names of six or seven states and their capital cities. The state names should be the keys and the capital cities should be the values. Then use a foreach loop to output the hash, showing the state names and their capitals. Finally, prompt the user to enter a state name and then display the capital of that stat

    Professors Example
    @list = (2, 20, 30, 40, 50);

    # foreach loop execution
    foreach $a (@list){
    print "value of a: $a\n";
    }

    My Code
    Code:
    #!c:\Dwimper1\perl\bin\perl1.exe
    
    use strict;
    use warnings;
    
    my %states_cities = (
    	       "Georgia" => "Atlanta", 
                   "California" => "Sacramento",
    		"Texas" => "Austin", 
    		"Ohio" => "Columbus", 
    		"Hawaii" => "Honolulu",
    		 "Maryland" => "Annapolis");
    
    foreach my $key (keys %states_cities){
                   print "$key, $values{$key}";}
    
    print "Please enter a state of your choosing: '\n'";
    my $userinput = <STDIN>;
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    SC
    Posts
    3
    Rep Power
    0
    First, within the foreach, you're pulling from a hash called 'values' which doesn't exist.. and second, I haven't had luck with print and hashes... try printf:
    printf "%s, %s\n",$key,$states_cities{$key};

    Third, $userinput should get what the user enters. Just be aware that it will probably get the newline along with the test entered. use 'chomp' to get rid of it, and if the value equals a state, print the capitol/:

    my $userinput = <STDIN>;
    chomp $userinput;
    printf "Entered: %s. Capitol = %s\n",$userinput,$states_cities{$userinput};

    Final code:

    Code:
    use strict;
    use warnings;
    
    my %states_cities = (
    	  "Georgia" => "Atlanta", 
        "California" => "Sacramento",
    		"Texas" => "Austin", 
    		"Ohio" => "Columbus", 
    		"Hawaii" => "Honolulu",
    		"Maryland" => "Annapolis");
    
    foreach my $key (keys %states_cities){
                   print "$key, $states_cities{$key}\n";}
    
    print "Please enter a state of your choosing: '\n'";
    my $userinput = <STDIN>;
    chomp $userinput;
    printf "Entered: %s. Capitol = %s\n",$userinput,$states_cities{$userinput};
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,229
    Rep Power
    1231
    I haven't had luck with print and hashes... try printf
    Using the print function is easier than the printf function, unless you need fixed width field output or one of the other formats available in the printf function.

    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my %states_cities = (
        "Georgia"    => "Atlanta",
        "California" => "Sacramento",
        "Texas"      => "Austin",
        "Ohio"       => "Columbus",
        "Hawaii"     => "Honolulu",
        "Maryland"   => "Annapolis"
    );
    
    foreach my $key ( keys %states_cities ) {
        print "$key, $states_cities{$key}\n";
    }
    
    print "Please enter a state of your choosing:\n";
    my $userinput = <STDIN>;
    chomp $userinput;
    
    if (exists $states_cities{$userinput}) {
        print "The state to entered is $userinput and its capitol is $states_cities{$userinput}\n";
    }
    else {
        print "$userinput is not a state in the list\n";
    }

IMN logo majestic logo threadwatch logo seochat tools logo