Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Tutorial suggestion: split and join

by davido (Cardinal)
on Aug 28, 2003 at 22:23 UTC ( [id://287544]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
        $line = "Bart  Lisa Maggie Marge Homer";
        @simpsons = split ( /\s/, $line ); 
            # Splits line and uses single whitespaces 
            # as the delimiter.
    
  2. or download this
        @simpsons = split ( /\s+/, $line ); 
        #Now splits on one-or-more whitespaces.
    
  3. or download this
        $string = "Just humilityanother humilityPerl humilityhacker.";
        @japh = split ( /humility/, $string );
    
  4. or download this
        $string = "alpha-bravo-charlie-delta-echo-foxtrot";
        @list = split ( /(-)/, $string );
    
  5. or download this
        $string = "Monk";
        @letters = split ( //, $string );
    
  6. or download this
        @mydata = ( "Simpson:Homer:1-800-000-0000:40:M",
                    "Simpson:Marge:1-800-111-1111:38:F",
    ...
            ( $last, $first, $phone, $age ) = split ( /:/ ); 
            print "You may call $age year old $first $last at $phone.\n";
        }
    
  7. or download this
        ( $last, $first, $everything_else) = split ( /:/, $_, 3 );
    
  8. or download this
        $string = "Hello world!";
        @letters = split ( /\s*/, $string );
    
  9. or download this
        my @list = split /\s+/, $string;
        my @list = $string =~ /(\S+)/g;
    
  10. or download this
        my @bignumbers = $string =~ /(\d{4,})/g;
    
  11. or download this
        $string = join ( ':', $last, $first, $phone, $age, $sex );
    
  12. or download this
        $string = join ( ':', @array );
    
  13. or download this
        $string = join ( '', @array );
    
  14. or download this
        $string = join ( '*', "My", "Name", "Is", "Dave" );
    
  15. or download this
        $string = join ( 'humility', ( qw/My name is Dave/ ) );
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://287544]
Approved by enoch
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found