1

I have this multidimensional hash %HASH:

 'BBBL' => {
',                   'VALUE' => 'CASH
                     'DATES' => '20163112'
                      },
  'AAA' => {
',                   'VALUE' => '70
                     'DATES' => '20170602'
  'CCC' => {
',                   'VALUE' => '70
                     'DATES' => '20170602'

and for each of AAA BBB and CCC i want to push the DATES into an array.

foreach my $symbol (keys %HASH){
     my @dates = values %HASH;
    }

but that gives me:

            'VALUE' => 'CASH
            'DATES' => '20163112'
          },
          {
',          'VALUE' => '90
            'DATES' => '20170802'
          },
          {
',          'VALUE' => '90
            'DATES' => '20171702'
          }
        ];

and I have no idea how to get just the dates.

3
  • Note that your values seem to be terminated with a Carriage Return?! Commented Sep 23, 2017 at 20:34
  • What is a field that contains a single date called "dates"? Commented Sep 23, 2017 at 20:36
  • yes it is called dates. Commented Sep 23, 2017 at 20:44

1 Answer 1

4

You could do it like this:

my @dates;
for my $symbol (keys %HASH){
    push @dates, $HASH{$symbol}{DATES};
}

This could be simplified to:

my @dates;
for my $subhash (values %HASH){
    push @dates, $subhash->{DATES};
}

Which in turn could be rewritten as:

my @dates = map $_->{DATES}, values %HASH;

Side note: It seems like your VALUE fields contain a carriage return (CR, \r) at the end. If you used Data::Dumper to produce your output, you might want to set $Data::Dumper::Useqq = 1; to make the dump more readable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.