Perl Tutorials -Part 22 – Taking elements out of arrays

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


#qw function is an easy way of initiate array
@greetings = ('hi','hello','howdy');
#same as the above
@greetings = qw (hi hello howdy);
print $greetings[0];
print "\n\n";

#slice function remove elements from array
@weather = ('Heavy rain', 'Sun showers', 'Blue sky', 'Drifing fog');
#remove 1 element after the second element
print splice (@weather, 1, 1);
print "\n";
print $weather[1];
print "\n\n";

@weather = ('Heavy rain', 'Sun showers', 'Blue sky', 'Drifing fog');
#remove 2 elements after the second element
print splice (@weather, 1, 2);
print "\n";
print $weather[1];
print "\n";
Output:
1
2
3
4
5
6
7
hi

Sun showers
Blue sky

Sun showersBlue sky
Drifing fog
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: