The Perl
How to convert a UNIX Date and Time to a readable 9 digit tele phone OR use it as an Unique ID.
1312744885 to 131-274-4885
This gets the time in UNIX format.
&print_questions_id ($question_id);
&print_questions;
This prints the same number like a telephone number in the US - ( also like a genuine ID )
--------------- Sub Routine --------
sub print_questions_id () {
my ( $id ) = @_;
for ( 1..120 ) {
printf ( " " );
}
#printf (" <font size=\"5\"> ID: $id </font> <br>" );
#my $newid = sprintf ("%6.6s", $id );
#printf (" <font size=\"5\"> ID: $newid </font> <br>" );
my $right = substr ($id, 6);
my $middle = substr ($id, 4, 3 );
my $left = substr ($id, 0, 3);
printf (" <font size=\"5\"> ID: $left-$middle-$right </font> <br>" );
}
Example: Changing a String's Value
Frequently, I find that I need to change part of a string's value, usually somewhere in the middle of the string. When this need arises, I turn to the substr() function. Normally, the substr() function returns a sub-string based on three parameters: the string to use, the position to start at, and the length of the string to return.
$firstVar = substr("0123BBB789", 4, 3); print("firstVar = $firstVar\n");
This program prints:
firstVar = BBB
The substr() function starts at the fifth position and returns the next three characters. The returned string can be printed like in the above example, as an array element, for string concatention, or any of a hundred other options.
Things become more interesting when you put the substr() function on the left-hand side of the assignment statement. Then, you actually can assign a value to the string that substr() returns.
$firstVar = "0123BBB789"; substr($firstVar, 4, 3) = "AAA"; print("firstVar = $firstVar\n");
This program prints:
firstVar = 0123AAA789
--------------------------------------------------------
Special Characters Inside the Square Brackets
As we've already seen, a hyphen is used to indicate all characters in the colating sequence between the character on the hyphen's left and the character on its right.An uparrow (^) at immediately following the opening square bracket means "Anything but these characters", and effectively negates the character class. For instance, to match anything that is not a vowel, do this:
if($string =~ /[^AEIOUYaeiouy]/){print "This string contains a non-vowel"} Contrast to this: if($string !~ /[AEIOUYaeiouy]/){print "This string contains no vowels at all"}Best Uses of Character Classes
Print all people whose name begins with A through E if($string =~ m/^[A-E]/){print "$string\n"} If character classes are giving you quirky results, consider using groups!
Matching: Putting it All Together
Print everyone whose last name is Clinton, Bush or Reagan. Each element of list is first name, blank, last name, and possibly more blanks and more info after the last name. Study this til you understand it. if($string =~ m/^\S+\s+(Clinton|Bush|Reagan)/i){print "$string\n"}; Print every line with a valid phone number. if($string =~ m/[\)\s\-]\d{3}-\d{4}[\s\.\,\?]/)
{print "Phone line: $string\n"};
-------------------------------------------------------------
Between the %
and the format letter, you may specify several additional attributes controlling the interpretation of the format. In order, these are:
- format parameter index
An explicit format parameter index, such as
2$
. By default sprintf will format the next unused argument in the list, but this allows you to take the arguments out of order: - flags
one or more of:
- space prefix non-negative number with a space
- + prefix non-negative number with a plus sign
- - left-justify within the field
- 0 use zeros, not spaces, to right-justify
- # ensure the leading "0" for any octal,
- prefix non-zero hexadecimal with "0x" or "0X",
- prefix non-zero binary with "0b" or "0B"
For example:
- printf '<% d>', 12; # prints "< 12>"
- printf '<%+d>', 12; # prints "<+12>"
- printf '<%6s>', 12; # prints "< 12>"
- printf '<%-6s>', 12; # prints "<12 >"
- printf '<%06s>', 12; # prints "<000012>"
- printf '<%#o>', 12; # prints "<014>"
- printf '<%#x>', 12; # prints "<0xc>"
- printf '<%#X>', 12; # prints "<0XC>"
- printf '<%#b>', 12; # prints "<0b1100>"
- printf '<%#B>', 12; # prints "<0B1100>"
When a space and a plus sign are given as the flags at once, a plus sign is used to prefix a positive number.
- printf '<%+ d>', 12; # prints "<+12>"
No comments:
Post a Comment