Regular expressions can be used to break up strings. This is what split does. The join function on the other hand
takes a list of strings and combines them together again.
Suppose we had a list of records of the form
Name|Phone Number|Address
We could open a file while which contained those records and do something like this:
The function join can be used to reconstruct split up values into one string again. The syntax for calling this function is join($glue, @array) or join($glue,$var1,$var2....) The glue is simply the string that goes between two strings to hold them together. Here are a few examples:
$line="Bart Lisa Maggie Marge Homer"; @simpsons=split(/\s/, $line); #splits $line and uses a piece of whites +pace as a delimiter. #@simpsons now contains ("Bart","","Lisa","Maggie","Marge","Homer"); #notice there is an extra space between Bart and Lisa so we get an emp +ty element in the array there. #lets try a better delimiter that will eliminate that from happening @simpsons=split(/\s+/ $line); #now splits $line on 1 or more whitespac +e characters #@simpsons now containts ("Bart","Lisa","Maggie","Marge","Homer");
Suppose we had a list of records of the form
Name|Phone Number|Address
We could open a file while which contained those records and do something like this:
open FILE, "data.txt"; while(<FILE>) chomp; ($name,$phone,$address)=split(/\|/); #splits the default variable $ +_ on | #notice we have to put \| sinc +e | is a metacharacter #that represents or. Otherwise + we'd be matching #empty string or empty string #then we place the results in +variables instead of a list #the parentheses around the va +riable names need #to be there for this to work +properly print "Name: $name\n"; #Now we print out the informat +ion in a more readable form print "Phone Number: $phone\n"; print "Address: $address\n\n"; } close FILE;
The function join can be used to reconstruct split up values into one string again. The syntax for calling this function is join($glue, @array) or join($glue,$var1,$var2....) The glue is simply the string that goes between two strings to hold them together. Here are a few examples:
$string=join(" ",@simpsons); #string now equals "Bart Lisa Maggie Marge Homer"; $name="Bob"; $phone="555-5555"; $address="42 Tulip Lane, Holland MI, 49423"; $string=join("|",$name,$phone,$address); #$string is now equal to "Bob|555-5555|42 Tulip Lane, Holland MI, 4942 +3"
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: split and join
by jeroenes (Priest) on Jan 08, 2001 at 18:46 UTC | |
RE: split and join
by Anonymous Monk on May 10, 2000 at 23:36 UTC | |
by misty (Hermit) on May 11, 2000 at 22:54 UTC | |
Re: split and join
by Anonymous Monk on Dec 18, 2001 at 07:11 UTC | |
Error in the example code
by Anonymous Monk on Sep 10, 2001 at 05:39 UTC | |
by davorg (Chancellor) on Nov 27, 2001 at 15:40 UTC | |
by Anonymous Monk on Nov 27, 2001 at 15:26 UTC | |
Re: split and join
by Anonymous Monk on Jan 04, 2002 at 03:52 UTC | |
by Anonymous Monk on Sep 25, 2002 at 02:28 UTC | |
Re: split and join
by nobettername (Initiate) on Nov 10, 2005 at 08:06 UTC | |
by ysth (Canon) on Nov 10, 2005 at 09:36 UTC | |
Re: split and join
by Anonymous Monk on Apr 22, 2002 at 22:38 UTC | |
by Albannach (Monsignor) on Apr 22, 2002 at 23:42 UTC | |
by Zaxo (Archbishop) on Apr 22, 2002 at 23:35 UTC |
Back to
Tutorials