0

How do you write the following in Perl(provide exact Perl syntax): a) create hash "days_in_month" (hash variable name is "days_in_month") with two elements. First element with key "July" and value 31, second element with key "September" and value 30. Get hash element by key "September" and print its value (provide exact Perl syntax). What value should you get?

Would it be like this ?

       my %days_in_month = (
          'July' => '31',
          'September' => '30',
           );

       my $dayvalue = $days_in_month{September};
       print $dayvalue; 
6
  • 1
    Why do you ask? Do you get something other than 30? Commented Jan 30, 2013 at 2:38
  • I cannot run it. I don't have perl and I am trying to learn this way. Commented Jan 30, 2013 at 2:46
  • 4
    Why don't you have Perl? Do you have a machine? Which OS is it running? Are you sure you can't find Perl for that platform? (It is incredibly unlikely that Perl can't be put on it.) If you're on Windows, you can obtain Strawberry Perl or ActiveState Perl. If you're on a Linux platform, you can probably find pre-built binaries. If you're on another platform, well, you'll be able to find binaries too. Or you can build it yourself; it isn't incredibly hard (though it's not for a novice programmer). Learning Perl without Perl is going to be incredibly hard. Commented Jan 30, 2013 at 2:59
  • How can download and install Perl in Mac OS 10.6.8? Do I have to install Linux first? Thank you so much for your help! Commented Jan 30, 2013 at 4:39
  • 1
    I'd be surprised if Mac OS doesn't have Perl. You can check to see if it's there by going to Terminal and typing which perl or perl -v. You may find this question useful if you don't have it/want to install it: Is there is a website which allows me to test Perl online? Commented Jan 30, 2013 at 4:44

1 Answer 1

2

Yes, but you don't need to quote the key, and you really shouldn't quote numbers used as numbers.

my %days_in_month = (
          July => 31,
          September => 30,
           );

This is equivalent to the following code:

my %days_in_month = ('July' => 31,'September' => 30);
Sign up to request clarification or add additional context in comments.

1 Comment

He's not using the numbers as numbers in his example (but I would leave them unquoted nonetheless).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.