1. How chomp can be screwed up
########################
#! /usr/bin/perl -w
$/="something";
chomp ($name =);
#chomp $name;
print "$name\n";
########################
Now what happens is chomp won't remove the newline. It will remove
'something'!!!
This is because $/ by default is set to "\n" and we have set it to
'something'.
Good thing to remember.
#! /usr/bin/perl -w
$/="something";
chomp ($name =
#chomp $name;
print "$name\n";
########################
Now what happens is chomp won't remove the newline. It will remove
'something'!!!
This is because $/ by default is set to "\n" 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,"\n";
print "@abc\n";
########################
OUTPUT
abcd
a b c d
#! /usr/bin/perl -w
@abc= ("a","b","c","d");
print @abc,"\n";
print "@abc\n";
########################
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 "\n***1***\n";
for ($x=0;$x<=$#abc;$x++) { print "$abc[$x]"; } print "\n***2***\n"; for $x (0..$#abc){ print "$abc[$x]"; } print "\n***3***\n"; foreach $alphabet (@abc){ print "$alphabet"; } print "\n***4***\n"; foreach (@abc){ print "$_"; } print "\n***5***\n"; foreach (@abc){ print ; }
#! /usr/bin/perl -w
@abc= ("a","b","c","d");
print "\n***1***\n";
for ($x=0;$x<=$#abc;$x++) { print "$abc[$x]"; } print "\n***2***\n"; for $x (0..$#abc){ print "$abc[$x]"; } print "\n***3***\n"; foreach $alphabet (@abc){ print "$alphabet"; } print "\n***4***\n"; foreach (@abc){ print "$_"; } print "\n***5***\n"; foreach (@abc){ print ; }
print "\n***6***\n";
print @abc[0..$#abc],"\n";
print @abc[0..$#abc],"\n";
4. Using 'Last'
#######################
#! /usr/bin/perl -w
@alphabets = ('a','b','c','d','e','f','g','h','i');
foreach $letter (@alphabets) {
print "$letter\n";
last if $letter=~/e/;
}
#######################
@alphabets = ('a','b','c','d','e','f','g','h','i');
foreach $letter (@alphabets) {
print "$letter\n";
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\n";
print "Alphabets: @alphabets\n";
}
#######################
6. HASH########################
foreach $x (sort hash_val_descending (keys(%hash_val))) {
print OUTFILE "$x \, $hash_val{$x} \n";
}
print "\nOUTPUT FILE: merged.csv\n";
close OUTFILE;
close INFILE;
}
sub hash_val_descending {
$hash_val{$b} <=> $hash_val{$a};