Hashes are an advanced form of array. One of the limitations of an array is that the information contained within it can be difficult to get to. For example, imagine that you have a list of people and their ages.
The oddly-named hash is a special type of an array called an associative array. Each entry is a pair of values. They're denoted with a percentage mark (%) and consist of pairs of keys and values. Examples:
# Simple assignment
@AoH = ({'key1' => 'value1', 'key2' => 'value2},
{'newhashkey1' => 'value1', 'key2' => 'value2},
{'anotherhashkey1' => 'value1', 'key2' => 'value2});
# Adding anonymous hash to the array
push(@AoH, {'key' => 'val', 'xkey' => 'xval'});
$AoH[2] = {'key' => 'val', 'xkey' => 'xval'};
# Adding single key/value pair in one of the hashes
$AoH[1]{'NewKey'} = 'NewValue';
# Accessing the structure
for (my $i = 0; $i <= $#AoH; $i++) {
foreach my $key (keys %{$AoH[$i]}) {
print $AoH[$i]{$key}, "\n"; } }
There's a rough parallel between Perl's Hashes and HashMaps in Java, and between Perl's lists and ArrayLists in Java.
You'll be setting up your structures using objects, constructors and the like in Java - at first it will seem slow and longwinded but it will get easier with time, and you should find yourself able to maintain the code much more easily once you've put the initial effort in. In other words - though I have given to a comparison don't try to do a straight conversion because Java is different.
You could also use a Hashtable in place of a HashMap, and a Vector in place of an ArrayList - in both cases, these alternatives are recommended only if you want Java compatability or you're writing code to be threadsafe. A HashSet is something different - it has keys but no values, and you would use if for the type os situation where, in Perl, you set up a has and just put a "1" into the value to make it true.
Hashes are an advanced form of array. One of the limitations of an array is that the information contained within it can be difficult to get to. For example, imagine that you have a list of people and their ages.
The oddly-named hash is a special type of an array called an associative array. Each entry is a pair of values. They're denoted with a percentage mark (%) and consist of pairs of keys and values. Examples:
# Simple assignment @AoH = ({'key1' => 'value1', 'key2' => 'value2}, {'newhashkey1' => 'value1', 'key2' => 'value2}, {'anotherhashkey1' => 'value1', 'key2' => 'value2}); # Adding anonymous hash to the array push(@AoH, {'key' => 'val', 'xkey' => 'xval'}); $AoH[2] = {'key' => 'val', 'xkey' => 'xval'}; # Adding single key/value pair in one of the hashes $AoH[1]{'NewKey'} = 'NewValue'; # Accessing the structure for (my $i = 0; $i <= $#AoH; $i++) { foreach my $key (keys %{$AoH[$i]}) { print $AoH[$i]{$key}, "\n"; } }There's a rough parallel between Perl's Hashes and HashMaps in Java, and between Perl's lists and ArrayLists in Java.
You'll be setting up your structures using objects, constructors and the like in Java - at first it will seem slow and longwinded but it will get easier with time, and you should find yourself able to maintain the code much more easily once you've put the initial effort in. In other words - though I have given to a comparison don't try to do a straight conversion because Java is different.
You could also use a Hashtable in place of a HashMap, and a Vector in place of an ArrayList - in both cases, these alternatives are recommended only if you want Java compatability or you're writing code to be threadsafe. A HashSet is something different - it has keys but no values, and you would use if for the type os situation where, in Perl, you set up a has and just put a "1" into the value to make it true.