Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Understanding Split and Join

by davido (Archbishop)
on Dec 28, 2006 at 07:05 UTC ( #591988=perltutorial: 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
Node Status?
node history
Node Type: perltutorial [id://591988]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-12-28 18:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls