1)How to find the number of entries in a hash array?
By using
$count = keys %Hash; (or)
$count++ while each %Hash;
This preceding method will be faster than extracting the keys into a temporary array to count them.
2)How can I quote a variable to use in a regexp?
By using quotemeta
$str="$10";
$str=quotemeta($str);
or a regexp like
$pattern=~s#(\W)#\\$1#g;
If we are not sure about a pattern, enclose it in an eval to avoid fatal run-time error.
3)Exchanging values without using temporay variables
$alpha="alpha";
$beta="beta";
($beta,$alpha)=($first,$second);
Not only 2 values.. Can also exchange multiple values in perl
($alpha, $beta, $production) = qw(January March August);
# move beta to alpha,
# move production to beta,
# move alpha to production
($alpha, $beta, $production) = ($beta, $production, $alpha);
4)Case controlling
A string in uppercase needs converting to lowercase, or vice versa.Use the lc and uc functions or the \L and \U string escapes.
use locale; # needed in 5.004 or above
$big = uc($little); # "bo peep" -> "BO PEEP"
$little = lc($big); # "JOHN" -> "john"
$big = "\U$little"; # "bo peep" -> "BO PEEP"
$little = "\L$big"; # "JOHN" -> "john"
To alter just one character, use the lcfirst and ucfirst functions or the \l and \u string escapes.
$big = "\u$little"; # "bo" -> "Bo"
$little = "\l$big"; # "BoPeep" -> "boPeep"
The functions and string escapes look different, but both do the same thing. You can set the case of either the first
character or the whole string. You can even do both at once to force uppercase on initial characters and lowercase on the rest.
The use locale directive tells Perl's case-conversion functions and pattern matching engine to respect your language
environment, allowing for characters with diacritical marks, and so on..
5) To reformat paragraphs
use Text::Wrap module
Syntax goes like this
use Text::Wrap;
@OUTPUT = wrap($LEADTAB, $NEXTTAB, @PARA);
Example
# wrapdemo - show how Text::Wrap works
@input = ("Folding and splicing is the work of an editor,",
"not a mere collection of silicon",
"and",
"mobile electrons!");
use Text::Wrap qw($columns &wrap);
$columns = 20;
print "0123456789" x 2, "\n"; #To say that there are 20 columns
print wrap(" ", " ", @input), "\n";
6)Converting between ASCII characters and values
Use ord to convert a character to a number,
or use chr to convert a number to a character:
$num = ord($char);
$char = chr($num);
The %c format used in printf and sprintf also converts a number to a character:
$char = sprintf("%c", $num); # slower than chr($num)
printf("Number %d is character %c\n", $num, $num);
If $num is 101 then output will be -- Number 101 is character 'e'.
A C* template used with pack and unpack can quickly convert many characters.
@ASCII = unpack("C*", $string);
$STRING = pack("C*", @ascii);
Example:
$ascii_value = ord("e"); # now 101
$character = chr(101); # now "e"
If you already have a character, it's really represented as a string of length one, so just print it out directly using print
or the %s format in printf and sprintf.
The %c format forces printf or sprintf to convert a number into a character; it's not used for printing a character that's already in character format (that is, a string).
printf("Number %d is character %c\n", 101, 101);
The pack , unpack, chr, and ord functions are all faster than sprintf. Here are pack and unpack in action:
@ascii_character_numbers = unpack("C*", "sample");
print"@ascii_character_numbers\n";
#The above Statement will print 115 97 109 112 108 101
$string=pack("c*",@ascii_character_numbers);
print $string;
#The above statement will print "sample"
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment