Perl Tutorials -Part 23 – More array manipulation

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


#splice function replace elements in array
@weather = ('Heavy rain', 'Sun showers', 'Blue sky', 'Drifing fog');
#replace element 'Sun showers', 'Blue sky' with element 'Thunder'.
print splice (@weather, 1, 2, ('Thunder'));
print "\n";
print $weather[1];
print "\n\n";

#splice function insert elements in array
@weather = ('Heavy rain', 'Sun showers', 'Blue sky', 'Drifing fog');
#insert 'Thunder', 'snow storms' after second element 'Sun Showers'
print splice (@weather, 1, 0, ('Thunder','snow storms'));
print "\n";
print $weather[1];
print "\n\n";

#splice function sort elements in array
@weather = ('Heavy rain', 'Sun showers', 'Blue sky', 'Drifing fog');
print sort (@weather);
print "\n";
#notice the original array didn't change
print @weather;

print "\nreverse\n";
#function reverse sort the array in reverse alphabatic order;
print reverse(@weather);
print "\n\n";
Output:
1
2
3
4
5
6
7
8
9
10
11
Sun showersBlue sky
Thunder

Thunder

Blue skyDrifing fogHeavy rainSun showers
Heavy rainSun showersBlue skyDrifing fog
reverse
Drifing fogBlue skySun showersHeavy rain
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: