Unable to store array as a hash value - arrays

I'm trying to store an array (not array ref) in a hash but it is treating the array in scalar context and only storing the last value of array in the $hash->{key}.
use Data::Dumper;
$h->{'a'} = ( 'str_1', 'str_2' );
print Dumper $h;
Output: $VAR1 = { 'a' => 'str_2' };
Why can't I store an array in hash-Key and access the array elements as $hash->{key}[index]

$h->{'a'} = [ 'str_1', 'str_2' ];
You can only store scalar as a hash value, and scalar can be simple value or array reference.
Check perldoc.

Values of a hash have to be scalar values, cannot be arrays or hashes. So you need to use array reference as the value of $h->{'a'}:
$h->{'a'} = [ 'str_1', 'str_2' ];
and access them by using
$h->{'a'}->[0]; # for 'str_1'
$h->{'a'}->[1]; # for 'str_2'
By the way, as pointed out by #RobEarl, you also can use the following syntax
$h->{'a'}[0]; # for 'str_1'
$h->{'a'}[1]; # for 'str_2'
See perlref for how to create and use different kind of references.

Related

How to efficiently get an array of values from a referenced hash and an array of keys in perl?

I currently have an referenced hash and an array of keys that the hash contains. I want to get an array of the values corresponding to my array of keys.
I know how to do this in multiple lines:
# Getting hash reference and array of keys.
my $hashRef = {
one => 'foo',
two => 'bar',
three => 'baz'
};
my #keys = ('one', 'three');
# Getting corresponding array of values.
my #values;
foreach my $key (#keys) {
push #values, $hashRef->{$key};
}
However, I believe that there must be a much better way that doesn't make use of a loop. But unfortunately I just can't figure it out. How can I efficiently
get an array of values from a referenced hash and an array of keys; ideally in one line if possible?
Easily:
my #values = #$hashRef{#keys};
Or, on Perl 5.24+:
my #values = $hashRef->#{#keys};
Or, on Perl 5.20+ by enabling some additional features:
use feature qw(postderef);
no warnings qw(experimental::postderef);
my #values = $hashRef->#{#keys};
This takes advantage of the fact that you can get the values for multiple keys (a "slice") of a %hash with the #hash{LIST} syntax. You just have to dereference it first. See perldoc for more information.

Assigning to a slice of a 3D array using the range operator

I have a 3 dimensional array. I want to set three elements of it like this:
$array[$x][$y][0 .. 2] = (0, 1, 2);
but perl tells me:
Useless use of a constant (1) in void context
In array context:
#array[$x][$y][0 .. 2] = (0, 1, 2);
but perl tells me:
syntax error near "]["
presumably meaning that it expects me to give it two indices and then assign to the third dimension as a separate array? However, on this page, under Example: Assignment Using Array Slices, it suggests that it is possible to assign to a slice using the range operator where it says:
#array1[1..3] = #array2[23..25];
How can I assign to a slice of the array like this, or do I have to assign each index individually?
You need to dereference the inner array:
#{ $arr[$x][$y] }[ 0 .. 2 ] = (0, 1, 2);
$array[$x][$y][0..2] isn't a slice; it's just an element lookup.
When you attempted to change it into a slice, you sliced the wrong array. You sliced #arr instead of #{ $arr[$x][$y] }.
The key here is to realize that there's no such thing as 3d arrays in Perl. What you have is an array of references to arrays of references to array, which is colloquially called array of array of array, and often abbreviated to AoAoA.
Array slices have the following syntax:
#NAME[LIST]
#BLOCK[LIST]
#$REF[LIST]
EXPR->#[LIST][1]
You could use any of the following:
The first syntax can't be used since the array to slice doesn't have a name.
#{ $array[$x][$y] }[0..2] = 0..2;
my $ref = $array[$x][$y]; #$ref[0..2] = 0..2;
$array[$x][$y]->#[0..2] = 0..2;[1]
See Dereferencing Syntax.
Requires Perl 5.24+. Available in Perl 5.20+ by adding both use feature qw( postderef ); and no warnings qw( experimental::postderef );.

Perl - How do I update (and access) an array stored in array stored in a hash?

Perhaps I have made this more complicated than I need it to be but I am currently trying to store an array that contains, among other things, an array inside a hash in Perl.
i.e. hash -> array -> array
use strict;
my %DEVICE_INFORMATION = {}; #global hash
sub someFunction() {
my $key = 'name';
my #storage = ();
#assume file was properly opened here for the foreach-loop
foreach my $line (<DATA>) {
if(conditional) {
my #ports = ();
$storage[0] = 'banana';
$storage[1] = \#ports;
$storage[2] = '0';
$DEVICE_INFORMATION{$key} = \#storage;
}
elsif(conditional) {
push #{$DEVICE_INFORMATION{$key}[1]}, 5;
}
}#end foreach
} #end someFunction
This is a simplified version of the code I am writing. I have a subroutine that I call in the main. It parses a very specifically designed file. That file guarantees that the if statement fires before subsequent elsif statement.
I think the push call in the elsif statement is not working properly - i.e. 5 is not being stored in the #ports array that should exist in the #storage array that should be returned when I hash the key into DEVICE_INFORMATION.
In the main I try and print out each element of the #storage array to check that things are running smoothly.
#main execution
&someFunction();
print $DEVICE_INFORMATION{'name'}[0];
print $DEVICE_INFORMATION{'name'}[1];
print $DEVICE_INFORMATION{'name'}[2];
The output for this ends up being... banana ARRAY(blahblah) 0
If I change the print statement for the middle call to:
print #{$DEVICE_INFORMATION{'name'}[1]};
Or to:
print #{$DEVICE_INFORMATION{'name'}[1]}[0];
The output changes to banana [blankspace] 0
Please advise on how I can properly update the #ports array while it is stored inside the #storage array that has been hash'd into DEVICE_INFORMATION and then how I can access the elements of #ports. Many thanks!
P.S. I apologize for the length of this post. It is my first question on stackoverflow.
I was going to tell you that Data::Dumper can help you sort out Perl data structures, but Data::Dumper can also tell you about your first problem:
Here's what happens when you sign open-curly + close-curly ( '{}' ) to a hash:
use Data::Dumper ();
my %DEVICE_INFORMATION = {}; #global hash
print Dumper->Dump( [ \%DEVICE_INFORMATION ], [ '*DEVICE_INFORMATION ' ] );
Here's the output:
%DEVICE_INFORMATION = (
'HASH(0x3edd2c)' => undef
);
What you did is you assigned the stringified hash reference as a key to the list element that comes after it. implied
my %DEVICE_INFORMATION = {} => ();
So Perl assigned it a value of undef.
When you assign to a hash, you assign a list. A literal empty hash is not a list, it's a hash reference. What you wanted to do for an empty hash--and what is totally unnecessary--is this:
my %DEVICE_INFORMATION = ();
And that's unnecessary because it is exactly the same thing as:
my %DEVICE_INFORMATION;
You're declaring a hash, and that statement fully identifies it as a hash. And Perl is not going to guess what you want in it, so it's an empty hash from the get-go.
Finally, my advice on using Data::Dumper. If you started your hash off right, and did the following:
my %DEVICE_INFORMATION; # = {}; #global hash
my #ports = ( 1, 2, 3 );
# notice that I just skipped the interim structure of #storage
# and assigned it as a literal
# * Perl has one of the best literal data structure languages out there.
$DEVICE_INFORMATION{name} = [ 'banana', \#ports, '0' ];
print Data::Dumper->Dump(
[ \%DEVICE_INFORMATION ]
, [ '*DEVICE_INFORMATION' ]
);
What you see is:
%DEVICE_INFORMATION = (
'name' => [
'banana',
[
1,
2,
3
],
'0'
]
);
So, you can better see how it's all getting stored, and what levels you have to deference and how to get the information you want out of it.
By the way, Data::Dumper delivers 100% runnable Perl code, and shows you how you can specify the same structure as a literal. One caveat, you would have to declare the variable first, using strict (which you should always use anyway).
You update #ports properly.
Your print statement accesses $storage[1] (reference to #ports) in wrong way.
You may use syntax you have used in push.
print $DEVICE_INFORMATION{'name'}[0], ";",
join( ':', #{$DEVICE_INFORMATION{'name'}[1]}), ";",
$DEVICE_INFORMATION{'name'}[2], "\n";
print "Number of ports: ", scalar(#{$DEVICE_INFORMATION{'name'}[1]})),"\n";
print "First port: ", $DEVICE_INFORMATION{'name'}[1][0]//'', "\n";
# X//'' -> X or '' if X is undef

How to convert array to this pointing array in Perl?

I am trying to use the package Normalize, but I have the data in arrays (#x), not in some pointing arrays which the package requires for normalization.
Wanted data format in a pointing array as hash
my %xx = ('1' => 22.595451, '2' => 20.089094, '3' => 17.380813);
Current data format
my #x = qw/22.595451 20.089094 17.380813/;
i.e. ('22.595451', '20.089094', '17.380813').
How can you convert the data to the pointing data-structure?
You can pass your array by reference instead of using a hash. Like this
use strict;
use warnings;
use Normalize;
my #x = qw/ 22.595451 20.089094 17.380813 /;
my $norm = Normalize->new(round_to => 1e-16);
$norm->normalize_to_max(\#x);
print "$_\n" for #x;
which will normalize the contents of #x in place
The actual name for what you call a "pointing array" is a hash, which is a type of associative array.
my %xx; $xx{$_} = $x[$_] for 0..$#x;
-or-
my %xx = map { $_ => $x[$_] } 0..$#x;
That said, Borodin pointed out that that module's functions accept a reference to an array as well as a reference to a hash. That means you don't need to do
my %xx = map { $_ => $x[$_] } 0..$#x;
$norm->normalize_to_max(\%xx);
since you can simply do
$norm->normalize_to_max(\#x);
The easiest way to convert an array into a hash.
my %hash;
#hash{ keys #array } = values #array;

About accessing the Array of Arrays

I once read the following example about "array of arrays". AOA is a two dimensional array
The following code segment is claimed to print the whole thing with refs
for $aref ( #AoA ) {
print "\t [ #$aref ],\n";
}
And the following code segment is claimed to print the whole thing with indices
for $i ( 0 .. $#AoA ) {
print "\t [ #{$AoA[$i]} ],\n";
}
What's the $aref stand for here? How to understand the definition of #$aref and #{$AoA[$i]}? Thanks.
$aref stands for "array reference", i.e. a reference for an array.
my $my_aref = \#somearray;
You can make an array from an array reference with the following syntax:
#{$my_aref}
#{$my_aref} is #somearray. (It's not a copy, it really is the same array.)
In second example, $AoA[$i] is an array reference, and you dereference it with the same syntax: #{$AoA[$i]}.
See perlreftut for more explanations and examples.
An "array of arrays" isn't actually an array of arrays. It's more an array of array references. Each element in the base array is a reference to another array. Thus, when you want to cycle through the elements in the base array, you get back array references. These are what get assigned to $aref in the first loop. They are then de-referenced by pre-pending with the # symbol, so #$aref is the array referenced by the $aref array reference.
Same sort of thing works for the second loop. $AoA[$i] is the $i-th element of the #AoA array, which is an array reference. De-referencing it by pre-pending it with the # symbol (and adding {} for clarity, and possibly for precedence) means #{$AoA[$i]} is the array referenced by the $AoA[$i] array reference.
Perl doesn't have multidimensional arrays. One places arrays into other arrays to achieve the same result.
Well, almost. Arrays (and hashes) values are scalars, so one cannot place an array into another array. What one does instead of place a reference to an array instead.
In other words, "array of arrays" is short for "array of references to arrays". Each value of the #AoA is a reference to another array, given the "illusion" of a two-dimensional array.
The reference come from the use [ ] or equivalent. [ ] creates an anonymous array, then creates a reference to that array, then returns the reference. That's where the reference comes from.
Common ways of building an AoA:
my #AoA = (
[ 'a', 'b', 'c' ],
[ 'd', 'e', 'f' ],
);
my #AoA;
push #AoA, [ 'a', 'b', 'c' ];
push #AoA, [ 'd', 'e', 'f' ];
my #AoA;
$AoA[$y][$x] = $n;
Keep in mind that
$AoA[$y][$x] = $n;
is short for
$AoA[$y]->[$x] = $n;
and it's equivalent to the following thanks to autovivification:
( $AoA[$y] //= [] )->[$x] = $n;
The whole mystery with multi-dimension structures in perl is quite easy to understand once you realize that there are only three types of variables to deal with. Scalars, arrays and hashes.
A scalar is a single value, it can contain just about anything, but
only one at the time.
An array contains a number of scalar values, ordered by a fixed
numerical index.
A hash contains scalar values, indexed by keys made of strings.
And all arrays, hashes or scalars act this way. Multi-dimension arrays are no different from single dimension.
This is also expressed very succinctly in perldata:
All data in Perl is a scalar, an array of scalars, or a hash of
scalars. A scalar may contain one single value in any of three
different flavors: a number, a string, or a reference. In general,
conversion from one form to another is transparent. Although a scalar
may not directly hold multiple values, it may contain a reference to
an array or hash which in turn contains multiple values.
For example:
my #array = (1, 2, 3);
Here, $array[0] contains 1, $array[1] contains 2, etc. Just like you would expect.
my #aoa = ( [ 1, 2, 3 ], [ 'a', 'b', 'c' ] );
Here, $array[0] contains an array reference. If you print it out, it will say something like ARRAY(0x398a84). Don't worry! That's still a scalar value. How do we know this? Because arrays can only contain scalar values.
When we do something like
for $aref ( #AoA ) {
print $aref; # prints ARRAY(0x398a84) or similar
}
It's no different from doing
for $number ( #array ) {
print $number;
}
$aref and $number are scalar values. So far, so good. Take a moment and lock this knowledge down: Arrays can only contain scalar values.
Now, the next part is simply knowing how to deal with references. This is documented in perlref and perlreftut.
A reference is a scalar value. It's an address to a location in memory. This location contains some data. In order to access the actual data, we need to dereference the reference.
As a simple example:
my #data = (1, 2, 3);
my $aref = \#data; # The backslash in front of the sigil creates a reference
print $aref; # print something like ARRAY(0xa4b6a4)
print #$aref; # prints 123
Adding a sigil in front of the reference tells perl to dereference the scalar value into the type of data the sigil represents. In this case, an array. If you choose the wrong sigil for the type of reference, perl will give an error such as:
Not a HASH reference
In the example above, we have a reference to a specific, named location. Both #$aref and #data access the same values. If we change a value in one, both are affected, because the address to the memory location is identical. Let's try it:
my #data = (1, 2, 3);
my $aref = \#data;
$$aref[1] = 'a'; # dereference to a scalar value by $ sigil
# $aref->[1] = 'a' # does the same thing, a different way
print #data; # prints 1a3
print #$aref; # prints 1a3
We can also have anonymous data. If we were only interested in building an array of arrays, we'd have no interest in the #data, and could skip it by doing this:
my $aref = [ 1, 2, 3 ];
The brackets around the list of numbers create an anonymous array. $aref still contains the same type of data: A reference. But in this case, $aref is the only way we have of accessing the data contained at the memory location. Now, let's build some more scalar values like this:
my $aref1 = [ 1, 2, 3 ];
my $aref2 = [ 'a', 'b', 'c' ];
my $aref3 = [ 'x', 'y', 'z' ];
We now have three scalar variables that contain references to anonymous arrays. What if we put these in an array?
my #aoa = ($aref1, $aref2, $aref3);
If we'd want to access $aref1, we could do print #$aref1, but we could also do
print #{$aoa[0]};
In this case, we need to use the extended form of dereferencing: #{ ... }. Because perl does not like ambiguity, it requires us to distinguish between #{$aoa[0]} (take the reference in $aoa[0] and dereference as an array) and #{$aoa}[0] (take the reference in $aoa and dereference as an array, and take that arrays first value).
Above, we could have used #{$aref}, as it is identical to #$aref.
So, if we are only interested in building an array of arrays, we are not really interested in the $aref1 scalars either. So let's cut them out of the process:
my #aoa = ( [ 1, 2, 3 ], [ 'a', 'b', 'c' ], [ 'x', 'y', 'z' ]);
Tada! That's an array of arrays.
Now, we can backtrack. To access the values inside this array, we can do
for my $scalar ( #aoa ) {
print #$scalar; # prints 123abcxyz
}
This time, I used a different variable name, just to make a point. This loop takes each value from #aoa -- which still is only a scalar value -- dereferences it as an array, and prints it.
Or we can access #aoa via its indexes
for my $i ( 0 .. $#aoa ) {
print #{$aoa[$i]};
}
And that's all there is to it!

Resources