Perl Tutorials -Part 25 – Extracting data from hashes

Posted on May 27, 2010. Filed under: Perl Tutorials |


%recordArtists = 
(
        'Original Nuttah' => 'Shy FX',
        'Kloaking Device' => 'DJ Krust',
        'Cops Dont Like Us' => 'EZ Rollers'
);

#example of converting perl hash values into perl array
#values function take all elements from hash, convert into array
@artists = values (%recordArtists);
print @artists;
print "\n\n";

#example of converting perl hash keys into array
#keys function take all key strings from hash, convert into array
@artists = keys (%recordArtists);
print @artists;
print "\n\n";

#example of removing perl hash element
delete $recordArtists{'Kloaking Device'};
@artists = keys (%recordArtists);
print @artists;
print "\n\n";

Output:
1
2
3
4
5
6
EZ RollersDJ KrustShy FX

Cops Dont Like UsKloaking DeviceOriginal Nuttah

Cops Dont Like UsOriginal Nuttah
Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: