String Proccessing


Conditional Constructs
Scalar Operators
Regular Expressions
Functions

  1. Conditional Constructs

            String Test     Meaning
                eq          Equal to
                ne          Not equal
                gt          Greater then
                le          Greater then or equal to
                lt          Less than
                le          Less than or equal to
                cmp         Not equal to, with signed return
            

    Example:
            while (<>) {               #Print lines that weren't blank.
                chop;
                if ($_ ne "") {
                    print $_,"/n";
                }
            }
            

  2. Scalar Operators

    Example:
                $/ = "";            #Enable paragraph mode.
                $* = 1;             #Enable multi-line patterns.
            # Now read each paragraph and split into words. Record each instance
            # of a word in the %wordcount associative array.
                while <> {
                    s/-\n//g;                            #Dehyphenate hyphenations.
                    tr/A-Z/a-z;                          #Canonicalize to lowercase.
                    &words = split(/\W*\s+\W*/, $_);
                    foreach $word (@words) {
                        $wordcount($word)++;              #Increment the entry.
                    }
                }             
            

  3. Regular Expressions


    Example:
            s/^([^ ]*) *([^ ]*)/$2 $1/;     #swap first two words
            /{\w*)\s*=\s*\1/;               #match "foo = foo"
            /.(80,};                        #match line at least 80 chars
            /^(\d+\.?\d*|\.\d+)$/;          #match valid Perl number
            if (/Time: (..):(..):(..)/) {   #pull fields out of line
                $hours = $1;
                $minutes = $2;
                $seconds = $3;
            }
            
            
  4. Functions


    • chop
      This function chops off the last character of a string and returns the character chopped.

              chop(LIST)
              chop(VARIABLE)
              chop VARIABLE
              chop
      
      Example:
              while  {
                  shop;       # avoid \n on last field
                  @array = split (/:/);
                  ....
              }
      
              
    • crypt
      This function encrypts a string.

      crypt(PLAINTEXT,SALT)
      Example:
              if ( crypt($quess, $pass) eq $pass) {
                  # guess is correct
              }
              
              
    • defined
      This function returns a Boolean value saying whether the value EXPR has a real value or not.

              defined(EXPR)
              defined EXPR
      Example:
              print if defined $switch{'D'}; #Test a scalar value from associative array
      
              
    • grep
      This function evaluates EXPR for each element of LIST and returns the array value consisting of those elements for which the expression evaluated to true.

      grep(EXPR,LIST)
      Example:
              @foo = grep(!/^/#/, @bar);  #@bar containes lines of code. Weed out comment lines.
              
              
    • index
      This function returns the position of the first occurrence of SUBSTR in STR.

              index(STR,SUBSTR,POSITION)
              index(STR,SUBSTR)
      Example:
              $pos = $1;
              while (( $pos = index($string, $lookfor, $pos)) >= $[) {
                  print "Found at $pos\n";        #look for occurence of $lookfor from posistion
                                                  #$[ in a $string and print it
                  $pos ++;
              }
      
              
    • join
      This function joins the separate strings of LIST into a single string with fields separated by the value of EXPR, and returns the string.

      join(EXPR,LIST)
      Example:
              S_ = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);        
              
    • length
      This function returns the length in characters of the value of EXPR.

              length(EXPR)
              length EXPR
              

    • print
      This function prints a string or a comma-separated lisr of strings. The function returns 1 if successful, 0 otherwise.

              print(FILEHANDLE LIST)
              print(LIST)
              print FILEHANDLE LIST
              print LIST
              print
      
              
    • printf
      This function prints a formatted string to a FILEHANDLE or, if ommited, the currently selected output filehandle.

              printf(FILEHANDLE FORMAT,LIST)
              printf(FORMAT,LIST)
              printf FILEHANDLE FORMAT,LIST
              printf FORMAT,LIST
      
              
    • reverse
      This function returns a string consisting of the characters of the first element of LIST in reverse character order.

              reverse(LIST)
              reverse LIST
      Example:
              %barfoo = reverse %foobar;
      
              
    • s
      This function searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of the substitutions made.
                     g   option indicates that all occurences of the pattern
                                           are to be replaced;
      
                      i   option indicates that matching is to be done in a 
                                           case-senesitive manner.   
      
                      e   option indicates that replacement string is to be 
                                           evaluted as an expression rather than just as a 
                                           double-quoted string.
              


      s/PATTERN/REPLACEMENT/[g][i][e][o]
      Example:
              for (;;) {                      #The substitute takes each line and replaces it
                  print "$_\$";               #it with the value of $1 after processing it
                  last unless $_ = ;   #through backquotes(running a /bin/sh on it)
                  s`^([^#)(#.*)?\n$`$1`;
              }
          
              
    • splice
      This function removes the elements designated by OFFSET and LENGTH from array and replaces them with the elements of LIST, if any. The function returns the elements removed from array.

              splice(ARRAY,OFFSET,LENGTH,LIST)
              splice(ARRAY,OFFSET,LENGTH)
              splice(ARRAY,OFFSET)
      
      Example:
              sub aeq {                                   #compare two array values
                      local(&a) = splice(@_,0,shift);
                      local(@b) = splice(@_,0,shift);
                      return 0 unless @a == @b;           # same length?
                      while (@a) {
                          return 0 if pop(@a) ne pop(@b);
                      }
                      return 1;
              }                                            #assuming array lengths before arrays
             
              
    • split
      This function splits a string into an array of strings, and returns the array value. The PATTERN matches the delimiters that separate the desired array elements.

              split(/PATTERN/,EXP,LIMIT)
              split(/PATTERN/,EXP)
              split(/PATTERN/
              split
      
      Example:
              ($login, $passwd, $remainder ) = split(/:/,$_,3);
              
              
    • sprintf
      This function returns formatted by the usual printfconventions.

      sprintf(FORMAT,LIST)
    • substr
      This function extracts a substring out of EXPR and returns it.

              substr(EXPR,OFFSET,LENGTH)
              substr(EXPR,OFFSET)
      
      Example:
              substr($_,-1,1) = "Curly";  # replace the last character of $_ with "Curly"
      
              
    • tr
                      d      option deletes all characters within SEARCHLIST
                                              that are not found in REPLACEMENTLIST.
      
                      s      option causes any substitutions that would result
                                              in multiple identical REPLACEMENTLIST characters
                                              to be output consecutively to be replaced with just 
                                              a single occurrence of that character.        
       
                       c     option complements the SEARCHLIST; any caharacters
                                              mentioned in SEARCHLIST are removed from string and
                                              the resulting string is used in place of SEARCHLIST.
              

              tr/SEARCHLIST/REPLACEMENTLIST/[c][d][s]
              y/SEARCHLIST/REPLACEMENTLIST/[c][d][s]
      
      Example:
              ($HOST = $host) =~ tr/a-z/A-Z/;     #Translate while copying document
              
              

    • undef
      This function undefines the value of EXPR, which must be an lvalue.

              undef(EXPR)
              undef EXPR
              undef
              

Back to content