preface 
 
  Look_up Engine  
 
  email how to  
 
  counter  
 
  regex  
 
  search engine  
 
  home  
 
  ask us  
 

 

Perl scripting


Applications


OO Perl


Perl & Databases


Search a word:
   



© The scientific sentence. 2010

Perl Other operators
Other operators

1. The Array @_

The values passed to a subroutine are stored in the default array @_. To get these values, we use, shift function The expression my ($number1) = shift; means: get the first argument from @_ and assign it to $number1 Example: sub Add1 { my ($number1) = shift; # get first argument from @_ and assign it to $number1 my ($number2) = shift; # assign the second argument of @_ to $number2 my $sum = $number1 + $number2; return $sum; } sub Add2 { my ($number1,$number2) = @_; my $sum = $number1 + $number2; return $sum; } sub Add3 { return ($_[0] + $_[1]); } $number1 = 34.00; $number2 = 12,12; $result1 = Add1($number1 ,$number2); $result2 = Add2($number1 ,$number2); $result3 = Add3($number1 ,$number2); print " $result1 \n $result2 \n $result3"; outputs 46 46 46

2. Conditional Operator

(Is something TRUE) ? IF YES do SOMETHING : OTHERWISE other THING ; The syntax is: my $RESULT = $BOOLEAN1 ? $VALUE1 : $VALUE2; Example: $number1 = -98.6754; $number2 = ($number1 =~m{^-}) ? abs($number1) : $number1 + 10; Will ouput 98.6754

3. References

Like in C language, A reference points to the variable to which it refers. It contains the address of this variable. In Perl, we can set a reference to a variable by placing a backslash "\" in front of this variable: Example: my $sentence = 'Best regards .. '; my $sentence_ref = \$sentence; \$sentence is the reference of the variable $sentence. $\$sentence will return the value of $sentenece, that is: Best regards ..

4. \Q...\E escape sequence

All the characters between the \Q and the \E are interpreted as literal characters.

5. Output record separator $/

The default input record separator $/ (or$\) is set to the newline \n. To empty is, we will set it as "undifined". undef $/; The output record separator is mainly used for the print operator "\n". Default is undef; then set to the newline character "\n". Perl uses this variable to identify where to break lines in a file. For example if it sets a colon (:) to be used as the delimiter $/ = ".";. Perl will break the file each time it encounters the period(.). Example: Here is the contents of the file test.txt: On the desk is a prepositional phrase. It consists of preposition and an object of a preposition. The object of preposition is a noun. Astronauts walked on the moon in 1969. Place is mentioned before time. Here is the script: #!c:/Perl/bin # Per reads file into array open (FILE, "ajaja.txt"); @lines = ; # Now, iterate over the file and print each line foreach $line (@lines) { print "- " . $line; } # print the related number of lines in the file: $count = @lines; print "\n"; print "$count lines in this file!\n"; outputs: - On the desk is a prepositional phrase. It - consists of preposition and an - object of a preposition. - The object of preposition is a noun. - Astronauts walked on the moon in 1969. - Place is mentioned before time. 6 lines in this file! with: undef $/; outputs: - On the desk is a prepositional phrase. It consists of preposition and an object of a preposition. The object of preposition is a noun. Astronauts walked on the moon in 1969. Place is mentioned before time. 1 lines in this file! with: $/ = "of"; outputs: - On the desk is a prepositional phrase. It consists of- preposition and an object of- a preposition. The object of- preposition is a noun. Astronauts walked on the moon in 1969. Place is mentioned before time. 4 lines in this file!

6. String Comparisons

$string =~ m/some_text/; Returns true if the string $string contains a substring "some_textt", false otherwise. If we need only those strings where "some_text" appears at the very beginning, we write: $string =~ m/^some_text/; If we want to know whether "some_text" is the very last text in the string, we write: $string =~ m/some_text$/; Now, if we want the comparison to be true only if $string contains nothinng but "some_text" , write: $string =~ m/^some_text$/; Now what if we want the comparison to be case insensitive, we add the letter i after the ending delimiter: $string =~ m/^some_text$/i; Remark: The ^ operator indicates "beginning of string". The $ operator indicates "end of string". m: stands for "match" =~ binding Operator, it binds a scalar expression to a pattern match, s, or tr ,...
  
Google
Web
ScientificSentence
 




chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2007. All rights reserved.