Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 523715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:29:28+00:00 2026-05-13T08:29:28+00:00

This is the code snippet I am working with: my %photo_details = ( ‘black_cat’

  • 0

This is the code snippet I am working with:

my %photo_details = (
 'black_cat' => (
  ('size' => '1600x1200', 'position' => -25),
  ('size' => '1280x1024', 'position' =>  25),
  ('size' =>   '800x600', 'position' =>   0),
 ),
 'race_car' => (
  ('size' => '1600x1200', 'position' =>  10),
  ('size' =>   '800x600', 'position' =>   5),
 ),
);

my $photo = 'black_cat';

foreach my $photo_detail ($photo_details{$photo})
{
 my $size     = $photo_detail{'size'};
 my $position = $photo_detail{'position'};

 print ("size = $size, position = $position\n");
}

What I am expecting to get is:

size = 1600×1200, position = -25

size = 1280×1024, position = 25

size = 800×600, position = 0

What I do get is:

Use of uninitialized value $size in concatenation (.) or string at C:\Test.pl line 23.

Use of uninitialized value $position in concatenation (.) or string at C:\Test.pl line 23.

size = , position =

The foreach statement is clearly wrong as not only are there no values for $size and $position, it has only gone through the loop once instead of three times. I have tried all sorts of variants of variable prefixes and found none that work.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:29:28+00:00Added an answer on May 13, 2026 at 8:29 am

    Here is some updated code, with an explanation below:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    my %photo_details = (
        'black_cat' => [
            {'size' => '1600x1200', 'position' => -25},
            {'size' => '1280x1024', 'position' =>  25},
            {'size' =>   '800x600', 'position' =>   0},
        ],
        'race_car' => [
            {'size' => '1600x1200', 'position' =>  10},
            {'size' =>   '800x600', 'position' =>   5},
        ],
    );
    
    
    print Dumper( %photo_details );
    foreach my $name ( keys %photo_details ) {
        foreach my $photo_detail ( @{ $photo_details{$name} } ) {
            my $size     = $photo_detail->{'size'};
            my $position = $photo_detail->{'position'};
    
            print Dumper( $photo_details{$photo} );
    
            print ("size = $size, position = $position\n");
        }
    }
    

    I’ve replaced some of your parentheses with square and curly brackets. In Perl, square brackets give you a reference to an anonymous array, and curly brackets denote a reference to an anonymous hash. These are called anonymous because there’s no explicit variable name for the anonymous array or hash.

    As Perl data structures make you store a reference to a hash rather than the actual hash, you need these to construct the references. You can do this in two steps like this:

    my @array = ( 1, 2, 3 );
    my $array_ref = \@array;
    my %hash = ( 'one' => 1, 'two' => 2, 'three' => 3 );
    my $hash_ref = \%hash_ref;
    

    To get data out of $array_ref and $hash_ref, you need the -> operator:

    print $array_ref->[0], "\n";
    print $hash_ref->{one}, "\n";
    

    You don’t need the quotes inside of the {} when referencing a hash key, although some people consider quotes on a hash key to be good practice.

    I added an example of iteration over the entire data structure as an example rather than just looking at one reference. Here’s the first line:

    foreach my $name ( keys %photo_details ) {
    

    The keys method returns all of the keys in a hash, so that you can get them in order. The next line iterates over all of the photo_detail hashrefs in %photo_details:

        foreach my $photo_detail ( @{ $photo_details{$photo} } ) {
    

    The @{ $photo_details{$photo} } de-references the reference $photo_details{$photo} into an array, which you can iterate over it with foreach.

    The last thing that I added is a call to Data::Dumper, a very useful module distributed with Perl that prints out data structures for you. This is very handy when building up data structures like this, as is its closely related cousin Data::Dumper::Simple. This module is unfortunately not distributed with Perl, but I prefer its output as it includes variable names.

    For some further reading about how to build up complex data structures using references, check out perlreftut.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code snippet I am trying to run. It is working fine
Uh..... I have no idea why this simple code snippet isn't working: function increment()
This code snippet isn't working, I'm getting an Authentication Failed. response from the server.
This code snippet is from C# in Depth static bool AreReferencesEqual<T>(T first, T second)
Take this code snippet for example: window.location.href = 'mysite.htm#images'; Already being on the site
VS 2008 I have this code snippet I found on a VB website. But
I have this code snippet: <div id=div1> </div> <div id=div2> <h3>This is the content</h3>
A friend gave me this code snippet in Clojure (defn sum [coll acc] (if
I am using this code snippet to add KeyDown event handler to any element
I have this code snippet inside a function that checks if an object exists

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.