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

  • Home
  • SEARCH
  • 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 1047027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:14:21+00:00 2026-05-16T16:14:21+00:00

I load the following YAML stream into a Perl’s array and I want to

  • 0

I load the following YAML stream into a Perl’s array and I want to traverse the array associated with Field2.

use YAML;

my @arr = Load(<<'...');
---
Field1: F1
Field2:
 - {Key: v1, Val: v2}
 - {Key: v3, Val: v4}
---
Field1: F2
Field2:
 - {Key: v5, Val: v6}
 - {Key: v7, Val: v8}
...

foreach (@arr) {
    @tmp = $_->{'Field2'};   
    print $#tmp; # why it says 0 when I have 2 elements?

    # Also why does the below loop not work? 
    foreach ($_->{'Field2'}) {
    print $_->{'Key'} . " -> " $_->{'Val'} . "\n";
 }
}

I appreciate any feedback. Thank you.

  • 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-16T16:14:22+00:00Added an answer on May 16, 2026 at 4:14 pm

    Because you are not using references correctly. You may want to reread perldoc perlreftut and perldoc perlref.

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use YAML;
    
    my @arr = Load(<<'...');
    ---
    Field1: F1
    Field2:
     - {Key: v1, Val: v2}
     - {Key: v3, Val: v4}
    ---
    Field1: F2
    Field2:
     - {Key: v5, Val: v6}
     - {Key: v7, Val: v8}
    ...
    
    for my $record (@arr) {
         print "$record->{Field1}:\n";
         for my $subrecord (@{$record->{Field2}}) {
             print "\t$subrecord->{Key} = $subrecord->{Val}\n";
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following file: department=value1 location=valueA location=valueB department=value2 I use the following to load
rake db:fixtures:load The YAML file which I put in the test/fixtures following is imported
For the following jQuery code: $(#select).change(function() { $(#output).load(/output/, {}, function(data) { // I want
What recommendations can you give for a system which must do the following: Load
I'm using the following html to load dojo from Google's hosting. <script src=http://www.google.com/jsapi></script> <script
I have the following code in my file to load a div with HTML
I load data into my grid using a WCF service. When a user clicks
I have the following Python 2.6 program and YAML definition (using PyYAML ): import
I want to include a hash and list inside a YAML file that I'm
I dump the data from database to test/fixtures/*.yml, but when I use rake db:fixtures:load

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.