DENNIS'S QUICK GUIDE TO PERL
* Array & Hash Functions *
Return to index page

Array are groups of single scalars where each is addressed by a numeric integer index, starting with 0. They don't have to be pre-dimensioned. They can have multiple dimensions, and 2nd and later dimensions don't have to be all the same size. Hashes are groups of pairs of scalars, similar to dictionaries, where one of a pair is a string index (key) that used to extract the second (value). The locations in a hash of the pairs are unrelated to the order they were added.




One-Dimension Array Operations:
Create an empty array:
@myarray = ( );
Create a filled array:
@myarray = ($var0, $var1, $var2, ...);
Get the length of an array:
$arrlen = @myarray;
or
$arrlen = $#myarray + 1;
Fetch a specific single member in an array:
$myscalar = $myarray[$index];
Fetch a contiguous set of members in an array (' .. ' is literal):
@newarray = @myarray[$idx_beg .. $idx_end];
Fetch specific members in an array:
@newarray = @myarray[$idx1, $idx2, $idx3, ...];
Set a specific single member in an array:
$myarray[$index] = $myscalar;
Set a contiguous set of members in an array (' .. ' is literal):
@myarray[$idx_beg .. $idx_end] = @replac;
Set specific members in an array:
@myarray[$idx1, $idx2, $idx3, ...] = @replac;
Remove but save the scalar from the beginning of an array:
$value = shift (@myarray);
Insert a scalar at the beginning of an array (returns new size):
$newlen = unshift (@myarray, $myvar);
Insert one array at the beginning of another existing one (returns new size):
$newlen = unshift (@another, @one_array);
Remove but save the scalar at the end of an array:
$value = pop (@myarray);
Add a value at the end of an array (returns new size):
$newlen = push (@myarray, $myvar);
Append one array to another existing one (returns new size):
$newlen = push (@another, @one_array);
Delete everyting from an array and lose it all:
%myarray = ();
Delete everyting from an array but save it for later:
@rem_data = splice (@myarray);
Delete everything from a specified position and later:
@rem_data = splice (@myarray,$first_del);
pop (@myarray);    is same as    splice (@myarray, -1);
Delete several elements from a specified position and later:
@rem_data = splice (@myarray, $first_del, $num_dels);
pop (@myarray);    is same as    splice (@myarray, $#myarray, 1);
shift (@myarray);    is same as    splice (@myarray, 0, 1);
Delete several elements from a specified position and later, then replace with data from a list:
@rem_data = shift (@myarray, $first_del, $num_dels, $replac1, $replac2, ...);
or
@rem_data = shift (@myarray, $first_del, $num_dels, @replac);

Multi-Dimension Arrays:
In a 2-dimension array, the first dimension is a set of references to arrays, and each reference yields a 1-D array of scalar values for the second dimension. In a 3-D array, the first and second dimensions are sets of references to arrays, and the last dimension is a set of scalar values.

Create a filled 2-D array (2r x 3c):
@veca = {[$r0c0,$r0c1], [$r1c0,$r1c1], [$r2c0,$r2c1]};
Fill in a single row from another 1-D array:
$veca[$rownum] = [ @row ];
Fetch a single row:
@row = @{$veca[$rownum]};
or
@row = @$veca[$rownum];
Fetch a single member:
$val = ${$veca[$rownum]}->[$colnum];
or
$val = $veca[$rownum]->[$colnum];
or
$val = $veca[$rownum][$colnum];
Number of rows (length of the first dimension):
@n_rows = $#veca + 1;
Number of columns in a row:
@n_cols = $#{$veca[$rownum]};

Hash Operations:
Create an empty hash:
%myhash = { };
Create a filled hash:
%myhash = ($key1, $val1, $key2, $val2, ...);
or
%myhash = ($key1 => $val1, $key2 => $val2, ...);
Fetch a hash value from a known key:
$the_val = $myhash{$the_key};
Fetch a several hash values at once
@val_list = @myhash{$key1, $key2, $key3, ...};
Create a reference to an anonymous filled hash:
$hashref = {$key1 => $val1, $key2 => $val2, ...};
for example
$record[$rec_num] = {name => $val1, rank => $val2, sernum => $val3};
then
$this_rank = $record[$this_rec_num]->{'rank'};
Test if a particular key exists in a hash:
$is_there = exists ($myhash{$the_key});
Test if a particular key has its corresponding value defined (not undef):
$has_value = defined ($myhash{$the_key});
Get a list of all keys:
@keylist = keys (%myhash);
Get a list of all values (same order as keys):
@valuelist = values (%myhash);
Remove a key/value pair but save it elsewhere for later:
%removed = delete ($myhash{$delkey});