Home > Mobile >  While executing the following snippet. I could see an error Global symbol "$colors" requir
While executing the following snippet. I could see an error Global symbol "$colors" requir

Time:03-10

Trying to print fruits followed by their colors using a for loop.

use strict; 
my %colors = (  apple      => 'red',
                orange     => 'orange',
                watermelon => 'green',
                grapes     => 'blue',
                rest       => 'pink' );
for (keys %colors) {    
   print("color of  $_  is   $colors($_)\n"); 
}

CodePudding user response:

$colors($_) must be $colors{$_}

Accessing hash values requires the use of { }

  • Related