1. How chomp can be screwed up
#! /usr/bin/perl -w
$/=”something”;
chomp ($name =
print “$name”;
########################
Now what happens is chomp won”t remove the newline. It will remove
“something”!!! This is because $/ by default is set to “
” and we have set it to
“something”.
Good thing to remember.
2. Something about Lists
#! /usr/bin/perl -w
@abc= (”a”,”b”,”c”,”d”);
print @abc,”";
print “@abc”;
########################
OUTPUT
abcd
a b c d
3. Printing arrays in so many ways (Look at ***5***)
#! /usr/bin/perl -w
@abc= (”a”,”b”,”c”,”d”);
print “***1***”;
for ($x=0;$x<=$#abc;$x++) {
print “$abc[$x]“;
}
print “***2***”;
for $x (0..$#abc){
print “$abc[$x]“;
}
print “***3***”;
foreach $alphabet (@abc){
print “$alphabet”;
}
print “***4***”;
foreach (@abc){
print “$_”;
}
print “***5***”;
foreach (@abc){
print ;
}
print @abc[0..$#abc],”";
#######################
4. Using “Last”
#! /usr/bin/perl -w
foreach $letter (@alphabets) {
print “$letter”;
last if $letter=~/e/;
}
#######################
5. Splice
#######################
#! /usr/bin/perl@vowels = (”a”,”e”,”i”,”o”);
@alphabets = (”a”,”b”,”c”,”d”);
&display;splice (@vowels,0,0, @alphabets);
&display;sub display{
print “Vowels: @vowels”;
print “Alphabets: @alphabets”;
}
#######################
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.