Accessing Hash Elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • autodidact
    New Member
    • Sep 2007
    • 24

    Accessing Hash Elements

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

    [CODE=perl]
    #!/usr/bin/perl
    use strict;
    use warnings;

    $family_name{"f red"} = "astaire"; [/CODE]
    I get the following error

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

    and when i try

    [CODE=perl]
    #!/usr/bin/perl
    use strict;
    use warnings;

    @family_name{"f red"} = "astaire"; [/CODE]
    It says Scalar value @family_name{"f red"} better written as $family_name{"f red"} at filename.plx syntax error at filename.plx, near @family_name{"

    Why is Perl Contradicting itself?

    Thanks In Advance!
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    You first need the hash. Then you can add elements to it.

    [CODE=perl]#!/usr/bin/perl

    use strict;
    use warnings;

    my %family_name = ();

    $family_name{'f red'} = 'astaire';
    print $family_name{'f red'};[/code]

    Comment

    • autodidact
      New Member
      • Sep 2007
      • 24

      #3
      Originally posted by eWish
      You first need the hash. Then you can add elements to it.

      [CODE=perl]#!/usr/bin/perl

      use strict;
      use warnings;

      my %family_name = ();

      $family_name{'f red'} = 'astaire';
      print $family_name{'f red'};[/code]
      Thanks!! Sometime programming just gets me riled up when i discover silly mistakes

      Comment

      Working...