Perl: get only all values from hash

Lets talk about how to get only the values from a given hash. Its easy. Just use the values() function from Perl. It will return you all the values as an array. You can then perform a loop to access the values from array index.
#!/usr/bin/perl

%my_hash = ('one' => 'value_1',
'two' => 'value_2',
'three' => 'value_3');

foreach my $value (values %my_hash){
print "$value\n";
}


Once again, you can perform revers() or sort() on your values after retrieve. For example,

foreach my $value (sort values %my_hash){
print "$value\n";
}


Similarly you can paste the values into an array directly too. Here is the code to get the values into any array from your hash.

@array = values(%my_hash);
$\ = $/;
print for(@array);

That's it dudes. Have a nice day once again.

Comments

Good one friend.
You can dump the hash as:
#!/usr/bin/perl
use Data::Dumper;

my %file_attachments = (
'test1.zip' => { 'price' => '10.00', 'desc' => 'the 1st test'},
'test2.zip' => { 'price' => '12.00', 'desc' => 'the 2nd test'},
'test3.zip' => { 'price' => '13.00', 'desc' => 'the 3rd test'},
'test4.zip' => { 'price' => '14.00', 'desc' => 'the 4th test'}
);

#add a hash key
$file_attachments{'test2.zip'}->{'location'} = '/var/www/html/test2.zip';

print Dumper(%file_attachments);


here the output will be:
$VAR1 = 'test2.zip';
$VAR2 = {
'location' => '/var/www/html/test2.zip',
'desc' => 'the 2nd test',
'price' => '18.00'
};
$VAR3 = 'test5.zip';
$VAR4 = {
'desc' => 'the last test',
'price' => '20.00'
};
$VAR5 = 'test3.zip';
$VAR6 = {
'desc' => 'the 3rd test',
'price' => '13.00'
};
$VAR7 = 'test1.zip';
$VAR8 = {
'desc' => 'the 1st test',
'price' => '10.00'
};
$VAR9 = 'test4.zip';
$VAR10 = {
'desc' => 'the 4th test',
'price' => '14.00'
};