Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

spliting arrays

by bory (Beadle)
on Nov 05, 2003 at 07:58 UTC ( [id://304653]=perlquestion: print w/replies, xml ) Need Help??

bory has asked for the wisdom of the Perl Monks concerning the following question:

Hi!I have the following problem:
my $ident="aaa bbb ccc-ddd eee fff-ggg hhh iii-jjj kkk lll-"; my @id= split/-/,$ident;
=> @id=("aaa bbb ccc","ddd eee fff","ggg hhh iii","jjj kkk lll"); I don't what to do to print only "aaa" and then "bbb" then "ccc".... exactly i want to store each one in a different string. Thank you very much for your time.

Replies are listed 'Best First'.
Re: spliting arrays
by davido (Cardinal) on Nov 05, 2003 at 08:06 UTC
    You can split on whitespace OR hyphen by using alternation within the split regexp:

    my @id = split /-|\s/, $ident;

    The above code would solve the problem illustrated in the text of your question. Notice: I used \s to indicate "space". \s is not strictly synonomous with "space", as it also matches tab, newline, etc. You could use the actual space character in your split regex. I used \s to make it easy to read the regex.

    The subject of your question, "Splitting Arrays" doesn't match the text of your question. Is there something you left out? We're not splitting arrays here, we're splitting strings into an array.


    Dave


    "If I had my life to live over again, I'd be a plumber." -- Albert Einstein
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: spliting arrays
by allolex (Curate) on Nov 05, 2003 at 11:22 UTC

    This is my take on your problem using split twice with array of anonymous arrays.

    #!/usr/bin/perl use strict; use warnings; my @strings; while (<DATA>) { # Split up input stream using dashes my @smallarray = split '-'; # @smallarray now has 'aaa bb +b ccc', etc. # Iterate through @smallarray items and split them up using s +paces # Create an array of anonymous array references # The references are the array resulting from split, using [] push(@strings, [split /\s/]) foreach (@smallarray); }
    # Print out the whole AoA contents foreach my $arrayref (@strings) { print "$_\n" foreach @$arrayref; } print "----\n\n"; # Or just the contents of one group foreach my $item (@{$strings[0]}) { print "$item\n"; } __DATA__ aaa bbb ccc-ddd eee fff-ggg hhh iii-jjj kkk lll- __OUTPUT__ aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk lll ---- aaa bbb ccc

    --
    Allolex

    Perl and Linguistics
    http://world.std.com/~swmcd/steven/perl/linguistics.html
    http://www.linuxjournal.com/article.php?sid=3394
    http://www.wall.org/~larry/keynote/keynote.html

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://304653]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2024-04-18 08:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found