The Perl values function can be used either with hashes or arrays, returning a list with the values. The syntax forms are:
values %hash
values @array
As with other built-in functions, the parentheses are optional but you can use them if you want to avoid any ambiguity.This function is the counterpart of the keys function which returns a list with the indices of an array or the keys of a hash.
Using the Perl values function with hashes
The values of the hash will be returned in a random order which it has nothing to do with the order you added the pair elements in the hash.
You can use the list of values for further processing (for example you may have a hash of arrays where the values of the hash are references to anonymous arrays and you need to dereference them to get the elements of the anonymous arrays) or simple to print the elements returned by the function.
To go through the list elements returned by the Perl values function you can use any loop statement or a function like grep or map.
The following example will print the values of a hash:
#!/usr/local/bin/perl
use strict;
use warnings;
my %hash = ( 1 => "left", 2 => "bottom",
3 => "right", 4 => "up" );
# print the hash values using the foreach loop
print("The arrows keys are: ");
print "$_ " foreach (values %hash);
print "\n";
# it prints: The arrows keys are: up left right bottom
You can use also the keys function to get the hash values by replacing the correspondent code line from the example above with the following one:print "$hash{$_} " foreach (keys %hash);
You can get the same result using the each function instead:my ($k, $v);
print "$v " while (($k, $v) = each %hash);
Please note the random order in which you get the values of the hash. But it doesn’t matter if you use values, keys or each, the order of the values is the same for all these functions.As you know, in a hash the keys are unique, but generally the values aren’t. You can use the reverse function twice to get the unique values of a hash, like in the following example:
#!/usr/local/bin/perl
use strict;
use warnings;
my %hash = ( 1 => "left", 2 => "bottom", 5 => "left",
3 => "right", 4 => "up", 6 => "up");
%hash = reverse %hash;
%hash = reverse %hash;
print("The values are: ", join(' ', values %hash), "\n");
You’ll get as result:The values are: up left right bottom
In a scalar context, the Perl values function returns the number of the hash elements. For example, if you run the following code lines:#!/usr/local/bin/perl
use strict;
use warnings;
my %hash = ( 1 => "left", 2 => "bottom", 5 => "left" );
my $size = values %hash;
print "$size\n";
# it prints: 3
the $size variable will be set to 3 (the number of pair elements of he hash).
Check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl built-in functions in your scripts.
Using the Perl values function with arraysYou can use the Perl values function with arrays in a similar manner that we’ve done with hashes.
You have a commented example below:
#!/usr/local/bin/perl
use strict;
use warnings;
my @array = ( 'blue', 'yellow', 'red', 'green', 'black', 'white');
# list context
print("Values are: ", join(' ', values @array), "\n");
# it prints: Values are: blue yellow red green black white
# scalar context
my $size = values @array;
print "$size\n";
# it prints: 6
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :