By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,245 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,245 IT Pros & Developers. It's quick & easy.

Accessing Hash Elements

P: 24
Would anyone happen to know why i'm getting an error with the following code?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3. use strict;
  4. use warnings;
  5.  
  6. $family_name{"fred"} = "astaire"; 
I get the following error

syntax error at filename.plx, near "$family_name{"
Execution of filename.plx aborted due to compilation errors.

and when i try

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3. use strict;
  4. use warnings;
  5.  
  6. @family_name{"fred"} = "astaire"; 
It says Scalar value @family_name{"fred"} better written as $family_name{"fred"} at filename.plx syntax error at filename.plx, near @family_name{"

Why is Perl Contradicting itself?

Thanks In Advance!
Jan 7 '08 #1
Share this Question
Share on Google+
2 Replies


eWish
Expert 100+
P: 971
You first need the hash. Then you can add elements to it.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %family_name = ();
  7.  
  8. $family_name{'fred'} = 'astaire';
  9. print $family_name{'fred'};
Jan 7 '08 #2

P: 24
You first need the hash. Then you can add elements to it.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %family_name = ();
  7.  
  8. $family_name{'fred'} = 'astaire';
  9. print $family_name{'fred'};
Thanks!! Sometime programming just gets me riled up when i discover silly mistakes
Jan 7 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.