Extracting Sub-Strings from Text Strings

   



Switching Word Order in Text Strings

Perl has nine positional variables (similar to sed) that can be used for backward referencing matched sub-strings, as illustrated by the code in Listing C.16.

Listing C.16 wordSwitch1.pl

start example
my($currentLine) = "one two"; if( $currentLine =~ /(\w+)\s+(\w+)/ ) {    print "original words: $1 $2\n";    print "switched words: $2 $1\n"; }
end example

Remarks

You can launch the Perl script wordSwitch1.pl in Listing C.16 from the command line as follows,

perl -w wordSwitch1.pl

and the output is as follows:

original words: one two switched words: two one



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net