Perl assign array to hash and vice-versa - arrays

I would like to get your help to figure out how to add a long array to a hash key value, how to assign the key value to a temporay array from where the data will be manipulated, than assign the array (or what I have left) back to the same hash key.
A sample of what I am looking for is below:
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
My set of arrays that will contain only integers
my #Array1 = ['01', '02', '03', '04', '09', '10', '11', '12'];
my #Array2 = ['05', '06', '07', '08', '03', '04', '09'];
my #Array3 = ['09', '10', '11', '12', '02', '03', , '07', '08' ];
my #ArrayN = ['N', 'N', 'N', 'N'];
My hash with keys identified by integers (Is it possible?)
my %Hash;
Assigning arrays to key values;
%Hash{'1'} = #Array1 (or $Array1);
%Hash{'2'} = #Array2 (or $Array2);
%Hash{'3'} = #Array3 (or $Array3);
%Hash{'N'} = #ArrayN (or $ArrayN);
Assign a hash key value to a temporary array
my #TempArray = $Hash{'1'};
Some process with the temporary array including:
(Delete the first element/item of the temporary array, let's say the
numnber "01"');
(Check if the array has a especifc value, let's say numbers 03 and
09);
(Delete especific values from the array, let's say the
numnbers 03 and 09');
(Check how many elements/items exist in the array);
than assign the value back to the hash
%Hash{'1'} = #TempArray;
I have found many posts that are not helping so much, as they don't talk about long arrays.

By using square brackets for your lists, you've actually created an array reference, which is a scalar. Your assignments should thus be:
my $Array1 = ['01', '02', '03', '04', '09', '10', '11', '12'];
my $Array2 = ['05', '06', '07', '08', '03', '04', '09'];
etc.
Next, when assigning these references as hash keys, you must use the $ sigil since you're referring to the hash value (a scalar) and not the hash itself.
$Hash{'1'} = $Array1;
$Hash{'2'} = $Array2;
Now if you want to assign these references to array variables, you must dereference them:
my #TempArray = #{ $Hash{'1'} };
As for your list of operations/checks on these arrays:
Delete the first item:
shift #TempArray;
Check if a certain value is an element of the array:
my $check = grep { $_ == $some_number } #TempArray;
($check will be the number of matches returned, and thus will evaluate to false if you get zero matches);
Delete an specific element (index unknown) from the array:
Every occurrence of a certain value?
#TempArray = grep { $_ != $some_number } #TempArray;
A specific element (not necessarily first or last)? Need to know the offset.
splice #TempArray => $index;
Check the length of the array:
my $length = #TempArray;
(referring to an array in list context returns its length)

My set of arrays that will contain only integers
Actually, your arrays contain strings :-)
my #Array1 = ['01', '02', '03', '04', '09', '10', '11', '12'];
Here's your first serious error. Arrays are initialised from lists - so you need round brackets, (...), not square brackets, [...].
You wanted this:
my #Array1 = ('01', '02', '03', '04', '09', '10', '11', '12');
Alternatively, you could use your original square brackets. But they give you an array reference. A reference is a scalar value, so you store it in a scalar variable.
my $Array1_ref = ['01', '02', '03', '04', '09', '10', '11', '12'];
My hash with keys identified by integers (Is it possible?)
Well, the keys of a hash are always strings. But that's ok. Perl will seamlessly convert integers to strings. And it's not necessary here because you're actually using strings ('1'), not integers (1).
%Hash{'1'} = #Array1;
A couple of errors here. The values in a hash are scalars. So you access them using a scalar sigil ($, not %). And, of course, an array isn't a scalar so you can't store it in a hash value. You can, however, store an array reference in a hash value (as references are scalar values.
$Hash{'1'} = \#Array1; # use \ to get a reference
Alternatively, if you stuck with the [...] version and stored your array reference in $Array_ref, then you can do:
$Hash{'1'} = $Array_ref;
Assign a hash key value to a temporary array
The value in your hash is a reference to your array. So you need to dereference it before storing it in an array variable.
#Temp_Array = #{ $Hash{'1'} };
Delete the first element/item of the temporary array, let's say the numnber "01"'
shift #Temp_Array;
Check if the array has a especifc value, let's say numbers 03 and 09
if (grep { $_ eq '03' } #Temp_Array) {
say "03 is in the array";
}
Delete especific values from the array, let's say the numnbers 03 and 09
#Temp_Array = grep { $_ ne '03' and $_ ne '09' } #Temp_Array;
Check how many elements/items exist in the array
Simply evaluate the array in a scalar expression.
$num_of_elements = #Temp_Array;
then assign the value back to the hash
Once again, you need to take a reference to the array.
$Hash{'1'} = \#Temp_Array
It's worth pointing out that you don't need to copy the array to a temporary array variable in order to manipulate it. Anywhere that I have used #Temp_Array in the examples above, you can replace it with #{ $Hash{'1'} } and you will be manipulating the array inside the hash directly.
shift #{ $Hash{'1'} }; # for example
I have found many posts that are not helping so much, as they don't talk about long arrays.
That's probably because long arrays and short arrays (and middle-sized arrays) are all handled in exactly the same way in Perl.

Related

Deleting elements in a perl array without index

I have an array which contains several strings. There is another array which contains strings which may or may not be contained in the first array.
What is the best way to remove any instances of a string in array b from the strings in array a. I have tried to use grep as in the example below but it completely empties the array.
my #Aray = ('Chris', 'John', 'Paul', 'Max', 'Jim', 'John');
my #Bray = ('Chris', 'John');
foreach $name (#Bray){
#Aray = grep {$_ != $name} #Aray;
}
This would result in no elements left in #Aray, opposed to 'Paul', 'Max', 'Jim'
I also tried with a traditional for loop and indexing each name in #Bray with the same result.
#choroba shows above how to solve the specific problem you are asking.
Deleting elements of one array based on the elements on another is a common problem in Perl, and there is a useful idiom that speeds up such operations. Specifically:
Place the elements you want to delete (Array B) in a Hash H.
Loop over Array A and test whether any of its elements are in Hash H, if so delete them, otherwise keep them
This method has the benefit of only reading the delete array (Array B) a single time instead of for every element of Array A
my #Aray = ('Chris', 'John', 'Paul', 'Max', 'Jim', 'John');
my #Bray = ('Chris', 'John');
my %to_delete = map { $_ => 1 } #Bray;
#Aray = grep { ! $to_delete{$_} } #Aray;

How to pass value of a hash of arrays into a subroutine?

I have a hash of arrays. I'm trying to do the following: as I'm looping through the keys of the hash, I would like to pass the value of the hash (in this case the array) into a subroutine.
Once I'm in the subroutine, I'd like to do a bunch of calculations with the array including take the average of the numeric values in the array. Lastly return a value based on the calculations.
Here's a minimal representation of what I have:
#!/usr/bin/perl
use warnings; use strict;
use List::Util qw(sum);
%data_set = (
'node1' => ['1', '4', '5', '2'],
'node2' => ['3', '2', '4', '12'],
'node3' => ['5', '2', '3', '6'],
);
foreach my $key ( keys %data_set ) {
foreach ( #{$data_set{$key}} ) {
my #array = split; # it's not letting me
calculate(\#array); ### I'm getting the error here!!
}
}
sub calculate{
my #array = #{$_};
my $average = mean(\#array);
# do some more calculations
return; # return something
}
# sum returns the summation of all elements in the array
# dividing by #_ (which in scalar content gives the # of elements) returns
# the average
sub mean{
return sum(#_)/#_;
}
Quick clarification: On the first iteration node1, I'd like to pass the array '1', '4', '5', '2' into the subroutine.
I think for my purposes this might be a bit more efficient than passing a reference to the hash of arrays into each subroutine. What do you guys think? In any case, can you guys help me figure this out? Any suggestions are appreciated. thanks
I think you are a bit confused about when you need to reference and dereference variables as well as what you are passing, where.
Lets look at this closer,
foreach my $key ( keys %data_set ) {
foreach ( #{$data_set{$key}} ) {
my #array = split; # it's not letting me
calculate(\#array); ### I'm getting the error here!!
}
}
You loop over the hash to get the keys, but then you use them to access the values to dereference the arrays and split each array into a single element array (of a string). Then you pass a reference to that 1-element array to calculate. Basically, you are doing too much work if the only thing that you want to do is pass each value of the hash (the arrays) into the calculate subroutine. Try something like this:
foreach my $key (keys %data_set){
calculate($data_set{$key}); # This is already a reference to an array
}
Additionally, in calculate you need to change my #array = #{$_}; to either my #array = #{$_[0]}; or my #array = #{(shift)};
Your mean subroutine should look something like this:
sub mean{
my #array = #{(shift)};
return sum(#array)/scalar(#array);
}

What is the difference between lists and arrays?

On this page, it shows how to initialize an array, and if you scroll down a bit, under the section called "The Lists" it "explains" what lists are and how they're different from arrays.
Except it uses an example that's just exactly the same as declaring an array, and doesn't explain it whatsoever.
What is the difference?
Take a look at perldoc -q "list and an array". The biggest difference is that an array is a variable, but all of Perl's data types (scalar, array and hash) can provide a list, which is simply an ordered set of scalars.
Consider this code
use strict;
use warnings;
my $scalar = 'text';
my #array = (1, 2, 3);
my %hash = (key1 => 'val1', key2 => 'val2');
test();
test($scalar);
test(#array);
test(%hash);
sub test { printf "( %s )\n", join ', ', #_ }
which outputs this
( )
( text )
( 1, 2, 3 )
( key2, val2, key1, val1 )
A Perl subroutine takes a list as its parameters. In the first case the list is empty; in the second it has a single element ( $scalar); in the third the list is the same size as #array and contains ( $array[0], $array[1], $array[2], ...), and in the last it is twice as bug as the number of elements in %hash, and contains ( 'key1', $hash{key1}, 'key2', $hash{key2}, ...).
Clearly that list can be provided in several ways, including a mix of scalar variables, scalar constants, and the result of subroutine calls, such as
test($scalar, $array[1], $hash{key2}, 99, {aa => 1, bb => 2}, \*STDOUT, test2())
and I hope it is clear that such a list is very different from an array.
Would it help to think of arrays as list variables? There is rarely a problem distinguishing between scalar literals and scalar variables. For instance:
my $str = 'string';
my $num = 99;
it is clear that 'string' and 99 are literals while $str and $num are variables. And the distinction is the same here:
my #numbers = (1, 2, 3, 4);
my #strings = qw/ aa bb cc dd /;
where (1, 2, 3, 4) and qw/ aa bb cc dd / are list literals, while #numbers and #strings are variables.
Actually, this question is quite well answered in Perl's FAQ. Lists are (one of) methods to organize the data in the Perl source code. Arrays are one type of storing data; hashes are another.
The difference is quite obvious here:
my #arr = (4, 3, 2, 1);
my $arr_count = #arr;
my $list_count = (4, 3, 2, 1);
print $arr_count, "\n"; # 4
print $list_count; # 1
At first sight, there are two identical lists here. Note, though, that only the one that is assigned to #arr variable is correctly counted by scalar assignment. The $list_count stores 1 - the result of evaluating expression with comma operator (which basically gives us the last expression in that enumeration - 1).
Note that there's a slight (but very important) difference between list operators/functions and array ones: the former are kind-of omnivores, as they don't change the source data, the latter are not. For example, you can safely slice and join your list, like this:
print join ':', (4,2,3,1)[1,2];
... but trying to 'pop' it will give you quite a telling message:
pop (4, 3, 2, 1);
### Type of arg 1 to pop must be array (not list)...
An array is a type of variable. It contains 0 or more scalars at monotonously increasing indexes. For example, the following creates array #a:
my #a;
Being variables, you can manipulate arrays. You can add elements, change the values of elements, etc.
"List" means many things. The two primary uses for it are to refer to list values and instances of the list operator.
A list value is an ordered collection of zero or more scalars on the stack. For example, the sub in the following code returns a list to be assigned to #a (an array).
my #a = f();
List values can't be manipulated; they are absorbed in whole by any operator to which they are passed. They are just how values are passed between subs and operators.
The list operator (,) is an N-ary operator* that evaluates each of its operands in turn. In list context, the list operator returns a list consisting of an amalgamation of the lists returned by its operands. For example, the list operator in the following snippet returns a list value consisting of all the elements of arrays #a and #b:
my #c = ( #a, #b );
(By the way, parens don't create lists. They're just there to override precedence.)
You cannot manipulate a list operator since it's code.
* — The docs say it's a binary operator (at least in scalar context), but it's not true.
Simple demonstration of difference.
sub getarray{ my #x = (2,4,6); return #x; }
sub getlist { return (2,4,6); }
Now, if you do something like this:
my #a = getarray();
my #b = getlist();
Then #a and #b will both contain the same value - the list (2,4,6). However, if you do this:
my $a = getarray();
my $b = getlist();
Then $a will contain the value 3, while $b will contain the value 6.
So yes, you can say that arrays are variables containing list values, but that doesn't tell the whole story, because arrays and literal lists behave quite differently at times.
Lists are comma-separated values (csv) or expressions (cse) . Arrays (and hashes) are containers.
One can initialize an array or hash by a list:
#a = ("profession", "driver", "salary", "2000");
%h = ("profession", "driver", "salary", "2000");
One can return a list:
sub f {
return "moscow", "tel-aviv", "madrid";
}
($t1, $t2, $t3) = f();
print "$t1 $t2 $t3\n";
($t1, $t2, $t3) is a list of scalar containers $t1, $t2, $t3.
Lists are a form of writing perl expressions (part of syntax) while arrays are data structures (memory locations).
The difference between lists and arrays confuses many. Perl itself got it wrong by misnaming its builtin function wantarray(): "This function should have been named wantlist() instead." There is an answer in perlfaq4, "What is the difference between a list and an array?", but it did not end my confusion.
I now believe these to be true:
An array in scalar context becomes a count of its elements.
The comma operator in scalar context returns the last element.
You can't make a reference to a list; \(2, 4, 6) returns a list of references to the scalars in the list. You can use [2, 4, 6] to make a reference to an anonymous array.
You can index a list (to get its nth element) without making an array if you make a list slice, so (2, 4, 6)[1] is 4.
But what if I want to count the elements in a list, or get the last element of an array? Should I convert between arrays and lists somehow?
You can always convert a list to an array with [...] syntax. One way to count the elements in a list is to make an anonymous array, then immediately dereference it in scalar context, like so:
sub list { return qw(carrot stick); }
my $count = #{[list()]};
print "Count: $count\n"; # Count: 2
Another way is to use the list assignment operator, like so:
sub list { return qw(carrot stick); }
my $count = (()=list());
print "Count: $count\n"; # Count: 2
There is no array in this code, but the list assignment operator returns the number of things being assigned. I assign them to an empty list of variables. In code golf, I write ()=$str=~/reg/g to count the regular expression matches in some string.
You need not convert an array to a list, because an array in list context is already a list. If you want the last element of an array, just say $array[-1].
The comma operator would return the last element of a list, but I can't use it to get the last element of an array. If I say ((),#array) in scalar context, then #array is in scalar context and I get the count.
You need not make an array to index a list. You can make an anonymous array, as in [list()]->[1], or you can make a list slice, as in (list())[1]. I had trouble with list slices because they have different syntax. A list slice needs parentheses! In C or Python or Ruby, func()[1] would do the array index on the function's return value, but in Perl, func()[1] is a syntax error. You must say (func())[1].
For example, I want to print the 3rd highest number in array. Because I'm lazy, I sort the array and take the 3rd last element:
my #array = (112, 101, 114, 108, 32, 104, 97, 99, 107);
print +(sort { $a <=> $b } #array)[-3], "\n"; # prints 108
The unary + prevents the print() function stealing my parentheses.
You can use a list slice on an array, as in (#array)[1]. This works because an array is a list. The difference between lists and arrays is that arrays can do $array[1].
An Array Vs A List
A list is a different kind of data structure from an array.
The biggest difference is in the idea of direct access Vs sequential access. Arrays allow both; direct and sequential access, while lists allow only sequential access. And this is because the way that these data structures are stored in memory.
In addition, the structure of the list doesn’t support numeric index like an array is. And, the elements don’t need to be allocated next to each other in the memory like an array is.
Arrays
An array is an ordered collection of items, where each item inside the array has an index.
here my answer about sigils and context
but main difference is this:
arrays have a scalar-context-value like count of elements.
lists have a scalar-context-value like LAST element in list.
so, you need to know about goat-operator: =()=.
Usage?
perl -e '$s =()= qw(a b c); print $s' # uh? 3? (3 elements, array context)
perl -e '$s = qw(a b cLastElementThatYouSee); print $s' # uh? cLastElementThatYouSee? (list context, last element returned)
as you see, =()= change context to 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!

Filling hash of multi-dimensional arrays in perl

Given three scalars, what is the perl syntax to fill a hash in which one of the scalars is the key, another determines which of two arrays is filled, and the third is appended to one of the arrays? For example:
my $weekday = "Monday";
my $kind = "Good";
my $event = "Birthday";
and given only the scalars and not their particular values, obtained inside a loop, I want a hash like:
my %Weekdays = {
'Monday' => [
["Birthday", "Holiday"], # The Good array
["Exam", "Workday"] # The Bad array
]
'Saturday' => [
["RoadTrip", "Concert", "Movie"],
["Yardwork", "VisitMIL"]
]
}
I know how to append a value to an array in a hash, such as if the key is a single array:
push( #{ $Weekdays{$weekday} }, $event);
Used in a loop, that could give me:
%Weekdays = {
'Monday' => [
'Birthday',
'Holiday',
'Exam',
'Workday'
]
}
I suppose the hash key is the particular weekday, and the value should be a two dimensional array. I don't know the perl syntax to, say, push Birthday into the hash as element [0][0] of the weekday array, and the next time through the loop, push another event in as [0][1] or [1][0]. Similarly, I don't know the syntax to access same.
Using your variables, I'd write it like this:
push #{ $Weekdays{ $weekday }[ $kind eq 'Good' ? 0 : 1 ] }, $event;
However, I'd probably just make the Good/Bad specifiers keys as well. And given my druthers:
use autobox::Core;
( $Weekdays{ $weekday }{ $kind } ||= [] )->push( $event );
Note that the way I've written it here, neither expression cares whether or not an array exists before we start.
Is there some reason that
push #{ $Weekdays{Monday}[0] }, "whatever";
isn’t working for you?

Resources