Hi Monks,

I seek your wisdom. I've spent too much time banging my head and Googling this.
I believe it must be something to do with the difference between lists and arrays,
I can't work out how to push into my %hash{} in the same element from another hashes array without it breaking out into an array of arrays.

I'm sure that I'm doing something silly, but I can't see it and would really appreciate some of your wisdom.

The result I'm after is this:-
%hash - from main $VAR1 = 'foo'; $VAR2 = { 'bar' => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] };

Whereas I'm getting this:-
%hash - from main $VAR1 = 'foo'; $VAR2 = { 'bar' => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, [ 10, 11, 12 ] ] };

This is my code

#!/usr/bin/perl; use strict; use warnings; use Data::Dumper; my %hash; my %source; my @array = (10 .. 12); $source{'data'} = \@array; print "\n".'%source'." - from main\n". Dumper %source; push (@ { $hash{"foo"}{"bar"} } , addSomeNumbers(1,3) ); print "\n".'%hash'." - from main\n". Dumper %hash; push (@ { $hash{"foo"}{"bar"} } , addSomeNumbers(4,6) ); print "\n".'%hash'." - from main\n". Dumper %hash; push (@ { $hash{"foo"}{"bar"} } , addSomeNumbersVia2ndSub(7,9) ); print "\n".'%hash'." - from main\n". Dumper %hash; push (@ { $hash{"foo"}{"bar"} } , addSomeNumbersFromSource("data") ); print "\n".'%hash'." - from main\n". Dumper %hash; sub addSomeNumbers{ my ($start, $end) = @_; my @array = ($start .. $end); print "\n".'@array'." - from addSomeNumbers\n". Dumper @array; return @array; } sub addSomeNumbersVia2ndSub{ my ($start, $end) = @_; my @array = addSomeNumbers($start, $end); print "\n".'@array'." - from addSomeNumbersVia2ndSub\n". Dumper @a +rray; return @array; } sub addSomeNumbersFromSource{ my ($data) = @_; my @array = $source{$data} ; return @array; }

In reply to Pushing Arrays into Hash without nesting by treeshark

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.