in reply to Hash from package

This is my first post -- I hope it helps! Looking at your code, modify it as follows: print("I have an Engine " . %{$car}->{'Engine'} . "\n"); print("I have " . %{$car}->{'Doors'} . " Doors\n"); print("The color is Colour " . %{$car}->{'Colour'} . "\n"); Don't forget the spaces as needed in the print command. Good Luck, Sagacity

Replies are listed 'Best First'.
Re^2: Hash from package
by Fletch (Bishop) on Jan 04, 2007 at 13:30 UTC

    Erm, %{$hashref}->{'key'} has some extra line noise in it. You'd really want either $car->{'key'}, or alternately ${ $car }{ 'key' }. You don't use the hash sigil when accessing a single element, just like you don't use @array[$idx] to access a single element from an array.</nit>

Re^2: Hash from package
by Yoda_Oz (Sexton) on Jan 04, 2007 at 09:07 UTC
    thanks for the post mate. however thats not quite what im after. that's cheating what you've suggested.
    i want those words you added in to come from the hash, not from adding to the print statement.

    ie.
    %hash => (Engine, 1800cc, Doors, 4, Color, blue)
    print %hash
    output is:
    Engine 1800cc
    Doors 4
    Color blue

    do you see where im coming from?
      I wouldn't call it cheating. You've already hardcoded some text and the hash key. Sagacity's suggestion is perfectly reasonable.

      You're probably asking the wrong question. "How do I get key values from a hash?" is more likely what you're after.

      foreach my $key (keys %$car) { print "$key = $car->{$key}\n"; }
      ---
      my name's not Keith, and I'm not reasonable.