print (@array, "\n");
This prints the following on your screen:
123
To get around this problem, put the array you want to print into a string:
print ("@array\n");
$scalar = @array;
($scalar) = @array;
In the first statement, the length of the list in @array is
assigned to $scalar. In the second statement, the first
element of @array is assigned to $scalar
while ($count <= @array) {
@slice = @array[1,2,3];
Although single elements of an array are referenced
using the $ character, array slices are referenced using
@:
$var = $array[0];
@subarray = @array[0,1];
The array-slice notation provides an easy way to swap elements in a list. The following
is an example:
@array[1,2] = @array[2,1];
This statement swaps the second and third elements of @array. As with the overlapping
array slices you saw earlier, the Perl interpreter copies @array[2,1] into a temporary locn.
@array =
unless (open(OUTFILE, ">outfile")) { // opposite to IF.
7: die ("cannot open output file outfile\n");
8: }
the print statement sends output to the standard output file, which means
that it sends the output to the file associated with STDOUT
print STDOUT ("Here is a line of output.\n");
***** it reads a line of input from file1 because the file variable STDIN represents file1.
Similarly, specifying > on the command file redirects the standard output file from the
screen to the specified file
myperlprog
FILES:
-e exists, -r file permission, -w
$list = <>;
is called myprog and is called using the command
$ myprog file1 file2 file3
In this case, the first occurrence of the <> operator reads the first line of input from
file1. Successive occurrences of <> read more lines from file1. When file1 is exhausted,
<> reads the first line from file2, and so on. When the last file, file3, is exhausted, <>
returns an empty string, which indicates that all the input has been read.
In Perl, the <> operator actually contains a hidden reference to the array @ARGV. Here's
how it works:
1. When the Perl interpreter sees the <> for the first time, it opens the file whose
name is stored in $ARGV[0].
2. After opening the file, the Perl interpreter executes the following library
function:
shift(@ARGV);
This library function gets rid of the first element of @ARGV and moves every other
element over one. This means that element x of @ARGV becomes element x-1.
3. The <> operator then reads all of the lines of the file opened in step 1.
4. When the <> operator exhausts an input file, the Perl interpreter goes back to
step 1 and repeats the cycle again
PATTERNS:
===============
(0[1-9]|[12]\d|3[01]),
===> 0[1-9] =>01,02,03....09
|[12] => 1/2
\d =>any single digit(0..9)
RE=> 0 to 31..
Perl 5 makes life a little easier by supplying the x option. This tells the Perl interpreter
to ignore white space in a pattern unless it is preceded by a backslash.
( 0[13578] | 1[02] )\2( 0[1-9] | [12]\d|3[01] )
\2 ==> Separator.....l \2, which matches the character that separates the day, month, and year
m!/u/jqpublic/perl/prog1! ===> Gud .... instead of /\u\/j....
m can be used if / is not used pattern match....
here ! we used... we can use anything other than / wid m...
\d Any digit [0-9]
\D Anything other than a digit [^0-9]
\w Any word character [_0-9a-zA-Z]
\W Anything not a word
character
[^_0-9a-zA-Z]
\s White space [ \r\t\n\f]
\S Anything other than white
space
[^ \r\t\
g Match all possible patterns
i Ignore case
m Treat string as multiple
lines
o Only evaluate once
s Treat string as single line
x Ignore white space in
pattern
/\d{2} ([\W]) \d{2} \1 \d{2}/x
s/(\d+)/($1)/g ==> puts Paranthesis..
s/\bno\b/NO/gi => no to NO,No to NO,nO to NO.
$string =~ /abc/$var/o; ===> o --->only once.
$string = "This is a\ntwo-line string.";
$string =~ s/a.*o/one/s;
# $string now contains "This is one-line string."
If the m option is specified, ^ and $ match the beginning and end of any line.
$string = "The The first line\nThe The second line";
$string =~ s/^The//gm;
For example, the following substitution
replaces all occurrences of the words no, No, NO, and nO with NO. (Recall that the \b
escape character specifies a word boundary.)
$string =~ s/\Btwo\B/one/ s; // s----> treat as single line
Replacement Using an Expression
The e option treats the replacement string as an expression, which it evaluates before
replacing. For example, consider the following:
$string = "0abc1";
$string =~ s/[a-zA-Z]+/$& x 2/e // replacemt =>evaluated b4 replacing.
$string = "This is a\n two -lintwoe string.";
$string =~ s/\Btwo\B/That x 2/es; ==> Replaces only lintwoe ... linthatthate.
$string =~ s/\btwo\b/That x 2/es; ==>replaces only two.
if ($string =~/(\d)+\.(\d)+.(\d)+.(\d)+/ ) // IP Address :-)
[a-zA-Z][a-zA-Z0-9_.]+@[a-zA-Z]+\.com|net|org|edu // E-mail Addr.
/([\da-z]+)([:;])\1\2\1/ [] =>pattern memory \1,\2,\3... so on..
~ tr/0-9/ /s all occurences of number wid space..
/[a-z]+/i
/(?i)[a-z]+/
In both cases, the pattern matches one or more alphabetic characters; the i option
indicates that case is to be ignored when matching.
The syntax for embedded pattern options is
(?option)
where option is one of the options shown in Table 7.7.
Table 7.7. Options for embedded patterns.
Option Description
i Ignore case in pattern
m Treat pattern as multiple lines
s Treat pattern as single line
x Ignore white space in pattern
If you want to enclose a subpattern in parentheses without storing it in
memory, use the ?: extended pattern-matching feature. For example, consider this
pattern:
/(?:a|b|c)(d|e)f\1/
Perl 5 enables you to use the ?= feature to define a boundary condition that must be
matched in order for the pattern to match. For example, the following pattern matches
abc only if it is followed by def:
/abc(?=def)/
Similarly, the ?! feature defines a negative look-ahead condition, which is a boundary
condition that must not be present if the pattern is to match
@list = (1, 2, 3, 4, 5);
foreach $temp (@list) {
if ($temp == 2) {
$temp = 20;
}
}
In this loop, when $temp is equal to 2, $temp is reset to 20. Therefore, the list stored in
the array variable @list becomes (1, 20, 3, 4, 5).
Use this feature with caution, because it is not obvious that the value of @list has
changed.
Perl enables you to tell the Perl interpreter to restart an iteration of a loop using the
redo statement.
Like last and next, the syntax for the redo statement is simple:
// redo possible in for loop but not in =============>>>> do ..while loop/until LOOPs.
next LABEL;
redo LABEL;
you can define statements that are to be executed whenever the end of a
while loop or an until loop is reached. To carry out this task, specify a continue
statement after the loop.
$i = 1;
while ($i <= 10) {
print ("$i\n");
}
continue {
$i++;
}
No comments:
Post a Comment