Assign multiple local vars to array entries in perl - arrays

In Perl, I've always been confused about how to cleanly assign multiple local variables from array entries.
I use the following syntax in subs all the time, so I'm somewhat familiar with it:
my ($var1, $var2) = #_
but other variations of this confuse me. For instance, I have the following code that works:
for my $ctr (0 .. $#matchingLines) {
my $lineNo = $matchingLines[$ctr][0];
my $text = $matchingLines[$ctr][1];
Where "#matchingLines" is an array of two-element arrays.
I wish I could convert the last two lines to the obvious:
my ($lineNo, $text) = $matchingLines[$ctr];
That of course does not work. I've tried numerous variations, but I can't find anything that works.

Just dereference the array ref:
my ( $lineNo, $text ) = #{ $matchingLines[$ctr] };
Check out Perl Data Structures Cookbook for additional examples.

It sounds like you have an array of arrays. this means that the inner arrays will be array references. If you want to allocate them to vars then you need to derference them.
use strict;
use warnings;
my #matchingLines = (['y','z'],['a','b']);
for my $ctr (0 .. $#matchingLines) {
my ($lineNo, $text) = #{$matchingLines[$ctr]};
print "#Array index: $ctr - lineno=$lineNo - text=$text\n"
}
this produces the output
#Array index: 0 - lineno=y - text=z
#Array index: 1 - lineno=a - text=b

Related

How to Iterate through multiple Perl arrays

I am hoping to make a loop that allows me to use less lines of code to make changes to a settings file with Perl. Currently my code reads an XML file and locates a settings ID and replaces the setting value in that ID with a new one. The current request involves a lot of changes to the settings file and the code is very long. I have set my values in an array and my settings ID's in an array. Like this:
#GreetGoalDP1 = (3, 5, 7, 10);
#GreetSIDSunDP1 = ('//xsd:Settings/xsd:Setting[#SID="7012"]/xsd:Value',
'//xsd:Settings/xsd:Setting[#SID="7013"]/xsd:Value',
'//xsd:Settings/xsd:Setting[#SID="7014"]/xsd:Value',
'//xsd:Settings/xsd:Setting[#SID="7015"]/xsd:Value');
and run the following.
my($matchSunDP1G1) = $xpc->findnodes($GreetSIDSunDP1[0]);
$matchSunDP1G1->removeChildNodes();
$matchSunDP1G1->appendText($GreetGoalDP1[0]);
#GreetB
my($matchSunDP1G2) = $xpc->findnodes($GreetSIDSunDP1[1]);
$matchSunDP1G2->removeChildNodes();
$matchSunDP1G2->appendText($GreetGoalDP1[1]);
#GreetC
my($matchSunDP1G3) = $xpc->findnodes($GreetSIDSunDP1[2]);
$matchSunDP1G3->removeChildNodes();
$matchSunDP1G3->appendText($GreetGoalDP1[2]);
#GreetD
my($matchSunDP1G4) = $xpc->findnodes($GreetSIDSunDP1[3]);
$matchSunDP1G4->removeChildNodes();
$matchSunDP1G4->appendText($GreetGoalDP1[3]);
I would like to run these changes through a loop just using the array [0] - [3] until completed as I have to do this same set of 4 multiple times. I am not too familiar with looping arrays. Is this something I can do in Perl? If so, what would be the most efficient way to do so?
A simple take
use warnings;
use strict;
...
for my $i (0..$#GreetGoalDP1) {
my ($matchSunDP1G) = $xpc->findnodes( $GreetSIDSunDP1[$i] );
$matchSunDP1G->removeChildNodes();
$matchSunDP1G->appendText( $GreetGoalDP1[$i] );
}
I take it that you don't need all those individual $matchSunDP1G1 etc. It's assumed that the two arays always have the same length, and their elements are needed in pairs at same indices.
The syntax $#aryname is for the last index in the array #aryname, and .. is the range operator, so 0 .. $#GreetGoalDP1 for your example is the list 0,1,2,3.
Then there are libraries that help with use of multiple arrays in parallel, that can be particularly useful when things get messier or more complicated. An example of using an iterator
use List::MoreUtils qw(each_array);
my $it = each_array #GreetSIDSunDP1, #GreetGoalDP1;
while ( my ($sidsun, $goal) = $it->() ) {
my ($matchSunDP1G) = $xpc->findnodes($sidsun);
$matchSunDP1G -> removeChildNodes();
$matchSunDP1G -> appendText( $goal );
}
If the lists are uneven in size the iterator keeps going through the length of the longer one. After the shorter one gets exhausted its would-be value is undef.
Following code sample demonstrates how you could use %hash for alternation you try to achieve.
my %hash = (
3 => '//xsd:Settings/xsd:Setting[#SID="7012"]/xsd:Value',
5 => '//xsd:Settings/xsd:Setting[#SID="7013"]/xsd:Value',
7 => '//xsd:Settings/xsd:Setting[#SID="7014"]/xsd:Value',
10 => '//xsd:Settings/xsd:Setting[#SID="7015"]/xsd:Value')
);
while( my($k,$v) = each %hash ) {
my $match = $xpc->findnodes($v);
$match->removeChildNodes();
$match->appendText($k);
}
Reference: hash, hash operations
Yet Another Way, using zip from the core List::Util module:
#!/usr/bin/env perl
use warnings;
use strict;
use List::Util qw/zip/;
...;
my #GreetGoalDP1 = (3, 5, 7, 10);
my #GreetSIDSunDP1 = ('//xsd:Settings/xsd:Setting[#SID="7012"]/xsd:Value',
'//xsd:Settings/xsd:Setting[#SID="7013"]/xsd:Value',
'//xsd:Settings/xsd:Setting[#SID="7014"]/xsd:Value',
'//xsd:Settings/xsd:Setting[#SID="7015"]/xsd:Value');
foreach my $pair (zip \#GreetSIDSunDP1, \#GreetGoalDP1) {
my ($matchSunDP1G1) = $xpc->findnodes($pair->[0]);
$matchSunDP1G1->removeChildNodes();
$matchSunDP1G1->appendText($pair->[1]);
}

De-reference x number of times for x number of data structures

I've come across an obstacle in one of my perl scripts that I've managed to solve, but I don't really understand why it works the way it works. I've been scouring the internet but I haven't found a proper explanation.
I have a subroutine that returns a reference to a hash of arrays. The hash keys are simple strings, and the values are references to arrays.
I print out the elements of the array associated with each key, like this
for my $job_name (keys %$build_numbers) {
print "$job_name => ";
my #array = #{#$build_numbers{$job_name}}; # line 3
for my $item ( #array ) {
print "$item \n";
}
}
While I am able to print out the keys & values, I don't really understand the syntax behind line 3.
Our data structure is as follows:
Reference to a hash whose values are references to the populated arrays.
To extract the elements of the array, we have to:
- dereference the hash reference so we can access the keys
- dereference the array reference associated to a key to extract elements.
Final question being:
When dealing with perl hashes of hashes of arrays etc; to extract the elements at the "bottom" of the respective data structure "tree" we have to dereference each level in turn to reach the original data structures, until we obtain our desired level of elements?
Hopefully somebody could help out by clarifying.
Line 3 is taking a slice of your hash reference, but it's a very strange way to do what you're trying to do because a) you normally wouldn't slice a single element and b) there's cleaner and more obvious syntax that would make your code easier to read.
If your data looks something like this:
my $data = {
foo => [0 .. 9],
bar => ['A' .. 'F'],
};
Then the correct version of your example would be:
for my $key (keys(%$data)) {
print "$key => ";
for my $val (#{$data->{$key}}) {
print "$val ";
}
print "\n";
}
Which produces:
bar => A B C D E F
foo => 0 1 2 3 4 5 6 7 8 9
If I understand your second question, the answer is that you can access precise locations of complex data structures if you use the correct syntax. For example:
print "$data->{bar}->[4]\n";
Will print E.
Additional recommended reading: perlref, perlreftut, and perldsc
Working with data structures can be hard depending on how it was made.
I am not sure if your "job" data structure is exactly this but:
#!/usr/bin/env perl
use strict;
use warnings;
use diagnostics;
my $hash_ref = {
job_one => [ 'one', 'two'],
job_two => [ '1','2'],
};
foreach my $job ( keys %{$hash_ref} ){
print " Job => $job\n";
my #array = #{$hash_ref->{$job}};
foreach my $item ( #array )
{
print "Job: $job Item $item\n";
}
}
You have an hash reference which you iterate the keys that are arrays. But each item of this array could be another reference or a simple scalar.
Basically you can work with the ref or undo the ref like you did in the first loop.
There is a piece of documentation you can check for more details here.
So answering your question:
Final question being: - When dealing with perl hashes of hashes of
arrays etc; to extract the elements at the "bottom" of the respective
data structure "tree" we have to dereference each level in turn to
reach the original data structures, until we obtain our desired level
of elements?
It depends on how your data structure was made and if you already know what you are looking for it would be simple to get the value for example:
%city_codes = (
a => 1, b => 2,
);
my $value = $city_codes{a};
Complex data structures comes with complex code.

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 );.

Count Perl array size

I'm trying to print out the size of my array. I've followed a few other questions like this one on Stack Overflow. However, I never get the result I want.
All I wish for in this example is for the value of 3 to be printed as I have three indexes. All I get, from both print methods is 0.
my #arr;
$arr{1} = 1;
$arr{2} = 2;
$arr{3} = 3;
my $size = #arr;
print $size; # Prints 0
print scalar #arr; # Prints 0
What am I doing wrong, and how do I get the total size of an array when declared and populated this way?
First off:
my #arr;
$arr{1} = 1;
$arr{2} = 2;
$arr{3} = 3;
is nonsense. {} is for hash keys, so you are referring to %arr not #arr. use strict; and use warnings; would have told you this, and is just one tiny fragment of why they're considered mandatory.
To count the elements in an array, merely access it in a scalar context.
print scalar #arr;
if ( $num_elements < #arr ) { do_something(); }
But you would need to change your thing to
my #arr;
$arr[1] = 1;
$arr[2] = 2;
$arr[3] = 3;
And note - the first element of your array $arr[0] would be undefined.
$VAR1 = [
undef,
1,
2,
3
];
As a result, you would get a result of 4. To get the desired 'count of elements' you would need to filter the undefined items, with something like grep:
print scalar grep {defined} #arr;
This will take #arr filter it with grep (returning 3 elements) and then take the scalar value - count of elements, in this case 3.
But normally - you wouldn't do this. It's only necessary because you're trying to insert values into specific 'slots' in your array.
What you would do more commonly, is use either a direct assignment:
my #arr = ( 1, 2, 3 );
Or:
push ( #arr, 1 );
push ( #arr, 2 );
push ( #arr, 3 );
Which inserts the values at the end of the array. You would - if explicitly iterating - go from 0..$#arr but you rarely need to do this when you can do:
foreach my $element ( #arr ) {
print $element,"\n";
}
Or you can do it with a hash:
my %arr;
$arr{1} = 1;
$arr{2} = 2;
$arr{3} = 3;
This turns your array into a set of (unordered) key-value pairs, which you can access with keys %arr and do exactly the same:
print scalar keys %arr;
if ( $elements < keys %arr ) { do_something(); }
In this latter case, your hash will be:
$VAR1 = {
'1' => 1,
'3' => 3,
'2' => 2
};
I would suggest this is bad practice - if you have ordered values, the tool for the job is the array. If you have 'key' values, a hash is probably the tool for the job still - such as a 'request ID' or similar. You can typically tell the difference by looking at how you access the data, and whether there are any gaps (including from zero).
So to answer your question as asked:
my $size = #arr;
print $size; # prints 0
print scalar #arr; # prints 0
These don't work, because you never insert any values into #arr. But you do have a hash called %arr which you created implicitly. (And again - use strict; and use warnings; would have told you this).
You are initializing a hash, not an array.
To get the "size" of your hash you can write.
my $size = keys %arr;
I just thought there should be an illustration of your code run with USUW (use strict/use warnings) and what it adds to the troubleshooting process:
use strict;
use warnings;
my #arr;
...
And when you run it:
Global symbol "%arr" requires explicit package name (did you forget to declare "my %arr"?) at - line 9.
Global symbol "%arr" requires explicit package name (did you forget to declare "my %arr"?) at - line 10.
Global symbol "%arr" requires explicit package name (did you forget to declare "my %arr"?) at - line 11.
Execution of - aborted due to compilation errors.
So USUW.
You may be thinking that you are instantiating an element of #arr when you are typing in the following code:
$arr{1} = 1;
However, you are instantiating a hash doing that. This tells me that you are not using strict or you would have an error. Instead, change to brackets, like this:
$arr[1] = 1;

Modifications to array also change other array

I have two global multidimensional arrays #p and #p0e in Perl. This is part of a genetic algorith where I want to save certain keys from #p to #p0e. Modifications are then made to #p. There are several subroutines that make modifications to #p, but there's a certain subroutine where on occasion (not on every iteration) a modification to #p also leads to #p0e being modified (it receives the same keys) although #p0e should not be affected.
# this is the sub where part of #p is copied to #p0e
sub saveElite {
#p0e = (); my $i = 0;
foreach my $r (sort({$a<=>$b} keys $f{"rank"})) {
if ($i<$elN) {
$p0e[$i] = $p[$f{"rank"}{$r}]; # save chromosome
}
else {last;}
$i++;
}
}
# this is the sub that then sometimes changes #p0e
sub mutation {
for (my $i=0; $i<#p; $i++) {
for (my $j=0; $j<#{$p[$i]}; $j++) {
if (rand(1)<=$mut) { # mutation
$p[$i][$j] = mutate($p[$i][$j]);
}
}
}
}
I thought maybe I'd somehow created a reference to the original array rather than a copy, but because this unexpected behaviour doesn't happen on every iteration this shouldn't be the case.
$j = $f{"rank"}{$r};
$p0e[$i] = $p[$j];
$p[$j] is an array reference, which you can think of as pointing to a particular list of data at a particular memory address. The assignment to $p0e[$i] also tells Perl to let the $i-th row of #p0e also refer to that same block of memory. So when you later make a change to $p0e[$i][$k], you'll find the value of $p[$j][$k] has changed too.
To fix this, you'll want to assign a copy of $p[$j]. Here is one way you can do that:
$p0e[$i] = [ #{$p[$j]} ];
#{$p[$j]} deferences the array reference and [...] creates a new reference for it, so after this statement $p0e[$i] will have the same contents with the same values as $p[$j] but point to a different block of memory.
I think your problem will probably be this:
$p0e[$i] = $p[$f{"rank"}{$r}]; # save chromosome
Because it looks like #p is a multi-dimensional array.
The problem is - the way perl 'does' multi dimensional arrays is via arrays of references. So if you copy an inner array, you do so by reference.
E.g.:
#!c:\Strawberry\perl\bin
use strict;
use warnings;
use Data::Dumper;
my #list = ( [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ], );
print Dumper \#list;
my #other_list;
push ( #other_list, #list[0,1] ); #make a sub list of two rows;
print Dumper \#other_list;
### all looks good.
## but if we:
print "List:\n";
print join ("\n",#list),"\n";
print "Other List:\n";
print join ("\n", #other_list),"\n";
$list[1][1] = 9;
print Dumper \#other_list;
You will see that by changing an element in #list we also modify #other_list - and if we just print them we get:
List:
ARRAY(0x2ea384)
ARRAY(0x12cef34)
ARRAY(0x12cf024)
Other List:
ARRAY(0x2ea384)
ARRAY(0x12cef34)
Note the duplicate numbers - that means you have the same reference.
The easiest way of working around this is by using [] judicously:
push ( #other_list, [#{$list[0]}], [#{$list[1]}] ); #make a sub list of two rows;
This will then insert anonymous arrays (new ones) containing the dereferenced elements of the list.
Whilst we're at it though - please turn on strict and warnings. They will save you a lot of pain in the long run.
That's because it's an array of arrays. The first level array stores only references to the inner arrays, if you modify the inner array, it's changed in both arrays - they both refer to the same array. Clone the deep copy instead of creating a shallow one.

Resources