Perl values Function

These sites are FOR SALE: miscperlinfo.com, mpihowto.com!
If you are interested, please use my Contact page.




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.


Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!


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).


Using the Perl values function with arrays

You 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



A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!


Check these how-to tutorial eBooks (PDF format):


Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        delete
        each
        exists
        grep
        hex
        index
        join
        keys
        lc
        lcfirst
        length
        map
        oct
        ord
        pack
        pop
        push
        q
        qq
        qw
        reverse
        rindex
        scalar
        shift
        sort
        splice
        split
        sprintf
        substr
        tr
        uc
        ucfirst
        undef
        unshift
        unpack
        values (more)

return from Perl values Function to Perl Basics



Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!