How do I efficiently create a perl hash of consecutive numbers? - perl

I need to create something like this:
my $valueref = {
1 => 1,
2 => 2,
3 => 3,
4 => 4
};
Based on certain conditions, it might be up to 40, or 50 or 60. In each case it would be consecutive integers, as show in the example. Once created, it would never be changed, simply passed to a preexisting subroutine. Since both the keys and the values will be consecutive, I could also create the hash using a for loop. I was curious what would be the fastest and/or most efficient way to create the hash? Or if there was yet another way it could be done?

Using map would suffice:
my $valueref = { map { $_ => $_ } 1 .. 40 };
Though one might note here that this is actually an array...
my #array = 0 .. 40;
So $valueref->{$n} is actually $array[$n]. I don't know if there is any benefit to using a hash in this case.

I'd probably use map for this:
my $highest_value = 50;
my %foo = map { $_ => $_ } 1 .. $highest_value ;
Remember that order is not guaranteed in a hash.

"Once created, it would never be changed, simply passed to a
preexisting subroutine"
Sounds like a good candidate for the state keyword (or a closure):
use feature 'state';
sub foo {
my ( $param1, $param2, $limit ) = #_;
state $valueref = { map { $_ => $_ } 0 .. $limit };
...
}
This enables one to initialize the data structure and then not have to worry about passing it as an argument later on.

Hash slice? Something like this:
my %hash;
my $count = 10;
#hash{1..$count} = (1..$count);

Related

Why does perl insert an undef value into my hash?

Let me start off with a simple minimal example:
use strict;
use warnings;
use Data::Dumper;
my %hash;
$hash{count} = 4;
$hash{elems}[$_] = {} for (1..$hash{count});
print Dumper \%hash;
Here is the result (reformatted):
$VAR1 = {
'count' => 4,
'elems' => [undef, {}, {}, {}, {}]
};
I do not understand, why did the first element of $hash{elems} become an undef?
I know there are probably easier ways to do what I am doing, but I am creating these empty hashes so that I can later do my $e = $hash{elems}[$i] and continue to use $e to interact with the element, eg continue the horror of nested structures with $e->{subelems}[0] = 100.
Array indices start at 0 in Perl (and in most programming languages for that matter).
In the 1st iteration of $hash{elems}[$_] = {} for (1..$hash{count});, $_ is 1, and you thus put {} at index 1 of $hash{elems}.
Since you didn't put anything at index 0 of $hash{elems}, it contains undef.
To remedy this, you could use push instead of assigning to specific indices:
push #{$hash{elems}}, {} for 1 .. $hash{count};
push adds items at the end of its first argument. Initially, $hash{elems} is empty, so the end is the 1st index (0).
Some tips:
The parenthesis are not needed in for (1..$hash{count}): for 1 .. $hash{count} works just as well and looks a bit lighter.
You could initialize your hash when you declare it:
my %hash = (
count => 4,
elems => [ map { {} } 1 .. 4 ]
);
Initializing elems with an arrayref of hashrefs is often useless, thanks to autovivification. Simply doing $hash{elems}[0]{some_key} = 42 will create an arrayref in $hash{elems}, a hashref at index 0 in this array, containing the key some_key with value 42.
In some cases though, your initialization could make sense. For instance, if you want to pass $hash{elems} (but not $hash) to a function (same thing if you want to pass $hash{elems}[..] to a function without passing $hash{elems}).

How to alter the hash values with new values in perl?

How can i reset the hash values in perl
use warnings;
use strict;
my %hash = qw(one 1 two 2 three 3 four 4);
my #key = keys(%hash);
my #avz = (9..12);
my %vzm;
print "Original hash and keys : ",%hash,"\n";
for(my $i = 0; $i<=scalar #avz; $i++){
my #new = "$key[$i] $avz[$i] ";
push(%vzm , #new);
}
print "modified hash and keys",%vzm,"\n";
I tried to alter the keys of original hash with another keys. How can i do it
This program give the error is:
Original hash and keys : three3one1two2four4
Not an ARRAY reference at key.pl line 10.
I expect the output is
Original hash and keys : three3one1two2four4
modified hash and keys : three11one9two10four12
How can i do it
Ok, first off - you're doing something nasty in your code:
You're trying to take an ordered data structure - an array - and push it into a keyed data structure, which has no particular ordering defined.
This isn't going to work very well - it technically works, because internally perl treats arrays and hashes similarly.
But for example your first assignment - what you're actually getting is:
my %hash = (
one => 1,
two => 2,
three => 3,
four => 4
);
You can access the keys (in no particular order) via keys(). And the values via values(). But to try and treat it like an array is undefined behaviour.
To add elements to your array:
$hash{'nine'} = 9;
To delete elements from your array:
delete ( $hash{'one'} );
You can iterate on keys or values - and combined with sort even do them in some sort of order. (Just bear in mind for sorting alphanumeric numbers you'll have a custom sort job).
foreach my $key ( sort keys %hash ) {
print "$key => $hash{$key}\n";
}
(Note - this is sorting by alphanumeric string, so gives:
four => 4
one => 1
three => 3
two => 2
If you want to sort by value:
foreach my $key ( sort { $hash{$a} <=> $hash{$b} } keys %hash ) {
print "$key => $hash{$key}\n";
}
And so you'll get:
one => 1
two => 2
three => 3
four => 4
So the real question remains - what are you actually trying to accomplish? The point of a hash is to give you an unordered mini-database of key-value pairs. Treating one like an array doesn't make an awful lot of sense. Either you're iterating hash elements in arbitrary order, or you're applying a specific sort to it - but one where you're relying on getting elements in a particular order is a bad plan - it may work, but it's not guaranteed to work, and that makes for bad code.
You have to keep the order of the keys in some array, or take it from original list
my #tmp = qw(one 1 two 2 three 3 four 4);
my %hash = #tmp;
# 'one', 'two', ..
my #key = #tmp[ grep !($_%2), 0 .. $#tmp ];
# ..
for my $i (0 .. $#avz) {
$vzm{ $key[$i] } = $avz[$i];
}
or using hash slice as more perlish approach,
#vzm{ #key } = #avz;
You can't do what you want (replace the values for keys in the hash in the order they originally were added) without keeping track of that order separately, since the hash doesn't have any particular order. In other words, this:
my #key = keys(%hash);
needs to be this:
my #key = ( 'one', 'two', 'three', 'four' );
Once you have that, you can just assign the values all at once with a hash slice:
my %vzm;
#vzm{#key} = #avz;
To create a hash element, you use assignment to $var{$key}.
for (my $i = 0; $i < scalar #avz; $i++) {
$vzm{$key[$i]} = $avz[$i];
}
Note also that the loop condition should be <, not <=. List/array indexes end at scalar #avz - 1.

Multidimension array in perl

I am working on a short script in which two to three variables are linked with each other.
Example:
my #batch;
my #case;
my #type = {
back => "sticker",
front => "no sticker",
};
for (my $i=0; $i<$#batch; $i++{
for (my $j=0; $j<$#batch; $j++{
if ($batch[$i]=="health" && $case[$i]$j]=="pain"){
$type[$i][$j]->back = "checked";
}
}
}
In this short code I want to use #type as $type[$i][$j]->back & $type[$i][$j]->front, but I am getting error that array referenced not defined . Can anyone help me how to fix this ?
Perl two-dimensional arrays are just arrays of arrays: each element of the top level array contains a (reference to) another array. The best reference for this is perldoc perlreftut
From what I can understand, you want an array of arrays of hashes. $type[$i][$j]->back and $type[$i][$j]->front are method calls in Perl, and what you want is $type[$i][$j]{back} and $type[$i][$j]{front}.
use strict;
use warnings;
my #batch;
my #case;
# Populate #batch and #case
my #type;
for my $i (0 .. $#batch) {
for my $j (0 .. $#{ $batch[$i] } ) {
if ($batch[$i] eq 'health' and $case[$i][$j] eq 'pain') {
$type[$i][$j]{back} = 'checked';
}
}
}
But I am very worried about your design. #type will be full of undefined elements, with only occasional ones set to checked. A proper fix depends entirely on what you need to do with #type once you have built it.
I hope this helps
Perl doesn't have multiple dimension variables. To emulate multidimential arrays, you can use what are called references. A reference is a way of referring to a memory location of another Perl structure such as an array or hash.
References allows you to build up more complex structures. For example, you could have an array and instead of each element in the array having a distinct value, it could point to another array. Using this, I can treat my array of arrays as a two dimensional array. But it's not a two dimensional array.
In a two dimensional array, each column ($j) has the same length. That's guaranteed. In Perl, what you have is each row ($i), pointing to a different array of columns ($j), and each of those column arrays could have a different number of elements (or even none at all! That inner array $j may not even be defined!).
There for, I have to check each column and see exactly how many values it might have:
for my $i ( 0..$#array ) {
if ( ref $array[i] ne "ARRAY" ) {
die qq(There is no sub array! for \$array[$i]!\n);
}
my #temp_j_array = #{ $array[$i] } { # This is how you dereference a reference
for my $j ( 0..$#temp_j_array ) {
# Here be dragons...
}
}
Note that I have to see exactly how many columns are in my inner ($j) array before I can go through it.
By the way, notice how I use .. to index my arrays. It's a lot cleaner than using that three part for loop which is very error prone. For example, should you check $i < $#array or $i <= $#array`? See the difference?
Since you're already dealing with a very complex structure (an array of arrays), I'm going to make it even more complex: (An array of arrays of hashes). This added complexity allows me to get rid of three separate variables. Instead of trying to keep #batch #case and #type in sync with each other, I can make these keys to my inner most hash:
my #structure = ... # Some sort of structure...
for my $i ( 0..$#structure ) {
my #temp = #{ $structure[$i] }; # This is a reference to an array. Dereference it.
for my $j ( 0..$#temp ) {
if ( $structure[$i]->[$j]->{batch} eq "health"
and $structure[$i]->[$j]->{case} eq "pain" ) {
$structure[$i]->[$j]->{back} = "checked";
}
}
}
This is a very common way to use Perl references to build more complex data structures:
my %employees; # Keyed by employee number:
$employees{1001}->{NAME} = "Bob";
$employees{1001}->{JOB} = "Yes man";
$employees{1002}->{NAME} = "Susan";
$employees{1002}->{JOB} = "sycophant";
You had some syntax errors, and were using the wrong boolean operator (==) instead of (ne).

Iterating over data structure

I am trying to iterate over this data structure:
$deconstructed->{data}->{workspaces}[0]->{workspace}->{facts}[0]->{code}
where fact[0] is increasing. It's several files I am processing so the number of {facts}[x] varies.
I thought this might work but it doesn't seem to be stepping up the $iter var:
foreach $iter(#{$deconstructed->{data}->{workspaces}[0]->{workspace}->{facts}}){
print $deconstructed->{data}->{workspaces}[0]->{workspace}->{facts}[$iter]->{code}."\n";
}
I'm totally digging data structures but this one is stumping me. Any advice what might be wrong here?
$iter is being set to the content of each item in the array not the index. e.g.
my $a = [ 'a', 'b', 'c' ];
for my $i (#$a) {
print "$i\n";
}
...prints:
a
b
c
Try:
foreach $iter (#{$deconstructed->{data}->{workspaces}[0]->{workspace}->{facts}}){
print $iter->{code}."\n";
}
$iter is not going to be an index that you can subscript the array with, it is rather the current element of the array. So I guess you should be fine with:
$iter->{code}
Your $iter contains the data sctructure. What you basiclly want is:
foreach my $elem ( #{$deconstructed->{data}->{workspaces}[0]->{workspace}->{facts}} ){
print $elem->{code};
}
or:
foreach my $iter ( 0 .. scalar #{$deconstructed->{data}->{workspaces}[0]->{workspace}->{facts}} ){
print $deconstructed->{data}->{workspaces}[0]->{workspace}->{facts}[$iter]->{code}."\n";
}
Since you are looping over the array, your misnamed $iter is the value you are looking for, not an index.
If you want to loop over the indexes instead, do:
foreach $iter ( 0 .. $#{$deconstructed->{data}->{workspaces}[0]->{workspace}->{facts}} ) {
print "Index $iter: ",
$deconstructed->{data}->{workspaces}[0]->{workspace}->{facts}[$iter]->{code}."\n";
}
Also note that you can drop -> between two [] or {}:
$deconstructed->{data}{workspaces}[0]{workspace}{facts}[$iter]{code}
I recommend reading http://perlmonks.org/?node=References+quick+reference.
When you have ugly data structures like this, make an interface for it so your life is easier:
foreach my $fact ( $data_obj->facts ) { # make some lightweight class for this
....;
}
Even without that, consider using a reference to just the part of the data structure you need so you don't think about the rest:
my $facts = $deconstructed->{data}{workspaces}[0]{workspace}{facts};
foreach my $fact ( #$facts ) {
print "Thing is $fact->{code}\n";
}
It's just a reference, so you're not recreating anything. Since you only have to think about the parts beyond the facts key, the problem doesn't look as hard.

how does this work: map used with ternary hook operator and ()

In a previous question, seaworthy asked how to remove the first 5 elements from an array:
How do I remove the first five elements of an array?
Among several suggestions, friedo offered this:
my $cnt = 0;
#array = map { ++$cnt < 5 ? ( ) : $_ } #array;
I don't get the ( ) bit. Please could explain how this works to me, because I can't get my head around it?
I know that the ternary hook operator works like this:
(if something) ? (then do this) : (otherwise do this)
For example: $a=2; print ($a==2 ? 3 : 4) # this prints: 3
because we have: ($a==2 ? 3 : 4)
which means: (if $a is equal to 2) ? (then print 3) : (otherwise print 4)
so with friedo's code, first $cnt is increased to 1, then we have:
$cnt < 5 ? ( ) : $_
which means:
if $cnt is less than 5 ? then ( ) : otherwise $_
I can see how the $_ bit works because i sometimes use map like this:
#array = map { $_, "\n" } #array
This copies an element from #array, places the copy into $, then adds a \n newline, then it copies the value in $ back to #array (and it does this with all values in #array so basically it adds newlines to every element in #array)
therefore:
#array = map { if $cnt is less than 5 then ( ) otherwise $_ } #array
means something like:
#array = map { if $cnt is less than 5 then ( ) otherwise copy the element back to #array }
so clearly ( ) means something like 'get rid of it'
but i'm just not sure how it works. Please could you explain it?
In a map, each item from the array is passed into the code block (in $_) where it can be transformed into some other value. In other words, map transforms a list.
In this case, we want to throw away values where the count ($cnt) is less than 5. So how to we make a map block return "nothing" when that condition is true?
We can't say
my $cnt = 0; #array = map { ++$cnt < 5 ? undef : $_ } #array;
Because then we'd end up with an array that looks like
( undef, undef, undef, undef, undef, 6, 7, 8 ... )
which is not what we wanted.
But returning ( ) instead returns an empty list. Consider push #foo, ( ); or #bar = ( 1, 2, 3, ( ), 4, 5, 6 ); In each of these cases, the empty set of parens is a list of zero items, which doesn't have any affect on the arrays concerned.
The empty list is useful in ternaries where you need to either return a list item or nothing at all. It's also useful to force list context on an expression to get a count:
my $count = ( ) = $str =~ /\d/g;
Here, we put the regex in list context by assigning it to an empty list, giving us the count of digits in the string. Then we assign that empty list to $count.
Another frequent example of using lists in map is when you're transforming something into a hash. For example,
my %unique = map { $_ => 1 } #duplicates;
Here each single item in #duplicates get transformed into a two-element list that looks like ( 'foo' => 1 ) although it's not as obvious since no parens are involved. All the two-item lists then get built up into one big list of alternating keys and values which construct the hash. Let's say you wanted to make this hash but exclude some items. In this case we either need to return a key/value, or nothing. So that's a good chance to use an empty list:
my %filtered_unique = map { some_test( $_ ) ? ( ) : ( $_ => 1 ) } #duplicates;
I know I'm a bit late in the game here, but why not do something simple?
my #truncated = #array[5 .. $#array]
Apparantly you can return a list instead of an element, and map constructs the result by joining those lists and elements. () is just the empty list in this case. For more insight, copy-paste the example, and replace () by (1, 2, 3).