[LA.pm] String handling like C array

Peter Benjamin pete at peterbenjamin.com
Tue Feb 7 10:24:34 PST 2006


Perl is not a parser for an entire file, like to compile
it into an object or executable, but it can do the job.

Tokenizing in perl can be done with a CPAN.perl.org module.
You'll have to find it yourself.  Or roll a quick one yourself.

#  Add your "delimiters here:
$RE[0] = "your_first_pattern_here";
$RE[1] = "your_second_pattern_here";
$RE[2] = ...

$REs = join ( "|", @RE );
$REs = "(?=$REs)";                # might be ?:

#    Tokens will include your delimiters for the switch
my ( @tokens ) = split ( $REs, $record );

#   I like to know which token is being handled by number.
#   This loop can be replaced with a primitive loop where
#   $nt can be incremented inside the loop to jump over
#   values that were handled in the cases.
foreach $nt ( 0..$#tokens ) {
  $token = $tokens($nr);

  switch $token {
  case "1"
    code
    break;
  case "2"
    code
    break;
  case "3"
    code
    break;
  case "4"
    code
    break;
  }
}



More information about the Losangeles-pm mailing list