treeshark has asked for the wisdom of the Perl Monks concerning the following question:
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.
%hash - from main $VAR1 = 'foo'; $VAR2 = { 'bar' => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] };
%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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pushing Arrays into Hash without nesting
by LanX (Saint) on Feb 13, 2021 at 18:28 UTC | |
by treeshark (Initiate) on Feb 14, 2021 at 15:29 UTC | |
|
Re: Pushing Arrays into Hash without nesting
by tybalt89 (Monsignor) on Feb 13, 2021 at 19:28 UTC | |
|
Re: Pushing Arrays into Hash without nesting
by 1nickt (Canon) on Feb 13, 2021 at 20:37 UTC | |
by AnomalousMonk (Archbishop) on Feb 13, 2021 at 23:51 UTC | |
by 1nickt (Canon) on Feb 14, 2021 at 00:07 UTC | |
|
Re: Pushing Arrays into Hash without nesting
by BillKSmith (Monsignor) on Feb 14, 2021 at 20:18 UTC |