Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Tutorial suggestion: split and join

by davido (Archbishop)
on Aug 28, 2003 at 22:23 UTC ( #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
Node Status?
node history
Node Type: perlmeditation [id://287544]
Approved by enoch
Front-paged by broquaint
help
Chatterbox?
[shmem]: yes, at least for the result in @foo
[shmem]: but the results of both operations are different.
[shmem]: result := the discarded result (what's leftmost of the statement)
[shmem]: push returns the number of elements in @foo, the assignment returns all the elements in the (renewed) @foo
[Lady_Aleena]: Sanity check please? push @line, ref($list_addition ) eq 'ARRAY' ? @$list_addition : $list_addiction; # so I don't have the change existing uses of the sub.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-04-27 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (503 votes). Check out past polls.