Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

$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"

In reply to split and join by root

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2022-05-26 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (94 votes). Check out past polls.

    Notices?