Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

[SOLVED]Splitting a string and pushing it into an array

by baxy77bax (Deacon)
on Jun 25, 2014 at 20:14 UTC ( [id://1091244]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, a very basic question but I am admitting I have no idea how to solve it and i need it right now. I have a string :
my $string = "/3212/2344,4334,XYZ&54,32/4333%3343%12";
I need to push this string into an array using any of /&%, symbols as record separators. but i need those elements to be in my array as well . so :
my @array = ('/','3212','/,'2344',',','4334','XYZ','&','54',',','32', +'/','4333','%','3343','%','12'");
should by my outcome.

Thank you in advance

UPDATE:

Thank you, I knew it was something simple but i somehow missed taht part

Replies are listed 'Best First'.
Re: Splitting a string and pushing it into an array
by choroba (Cardinal) on Jun 25, 2014 at 20:25 UTC
    Your expected output is wrong (a quote missing, an extra double quote, and a missing comma).

    For a solution, look at split: if the separator pattern uses capturing, it will be included in the result:

    #!/usr/bin/perl use warnings; use strict; use Test::More tests => 1; my $string = "/3212/2344,4334,XYZ&54,32/4333%3343%12"; my @array = do { no warnings 'qw'; qw(/ 3212 / 2344 , 4334 , XYZ & 54 , 32 / 4333 % 3343 % 12); }; my @ar = split m{([/&%,])}, $string; shift @ar if $string =~ m{^[/&%,]}; # Leading separators might cause p +roblems. is_deeply(\@ar, \@array, 'ok');
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-23 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found