Perl | values() Function

values() Function in Perl returns the list of all the values stored in a Hash. In a scalar context it returns the number of elements stored in the Hash.

Note: Values returned from the value() Function may not always be in the same order.

Syntax: values Hash

Returns: list of values in the list context and number of values in the scalar context

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl -w
  
# Hash containing Keys and values
%sample_hash = ('Geeks' => 'A',
                'for' => 'B',
                'Geek' => 10,
                'World' => 20);
  
# values() in list context returns 
# values stored in the sample_hash
@values = values(%sample_hash);
print("Values in the Hash are: "
       join("-", @values), "\n");
  
# values() in scalar context returns
# the number of values stored in sample_hash
$values = values( %sample_hash);
print "Number of values in Hash are: $values";

chevron_right


Output:

Values in the Hash are  A-B-10-20
Number of values in Hash are: 4

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl -w
  
# Hash containing Keys and values
%sample_hash = (1 => 'Welcome',
                2 => 'to',
                3 => 'Geeks',
                4 => 'World');
  
# values() in list context returns 
# values stored in the sample_hash
@values = values( %sample_hash);
print("Values in the Hash are ",
      join("-", @values), "\n");
  
# values() in scalar context returns
# the number of values stored in sample_hash
$values = values(%sample_hash);
print "Number of values in Hash are: $values";

chevron_right


Output:

Values in the Hash are  Welcome-World-to-Geeks
Number of values in Hash are: 4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.