Home: Perl Programming Help: Advanced:
Split Operation



santhosh_89
Novice

Jul 30, 2009, 8:45 PM


Views: 4402
Split Operation

I have a scalar variable, which needs to be split into an array.

Input :

my $test="[a,b],c,'d',[e,[f,g,h,[i,j],k,l],m,n],o,p";


Output :

@array = ( [a, b], c, 'd', [e, [f, g, h, [i, j], k, l], m, n], o, p);


KevinR
Veteran


Jul 30, 2009, 8:55 PM


Views: 4399
Re: [santhosh_89] Split Operation


Code
my $test="[a,b],c,'d',[e,[f,g,h,[i,j],k,l],m,n],o,p";  
@array = split(/,/,$test);
print "$_\n" for @array;

-------------------------------------------------


santhosh_89
Novice

Jul 30, 2009, 9:03 PM


Views: 4396
Re: [KevinR] Split Operation

Before seeing the output,don't put the answer,Please check the output.
Example the array will have the element [a,b] in first index.But you have split based on the comma(,).If you do like wise the first element will be '[a',.......


(This post was edited by santhosh_89 on Jul 30, 2009, 9:05 PM)


KevinR
Veteran


Jul 30, 2009, 9:08 PM


Views: 4394
Re: [santhosh_89] Split Operation

Your example output is so unclear no one will understand it. But I don't feel like trying to figure it out for you anyway.
-------------------------------------------------


(This post was edited by KevinR on Jul 30, 2009, 9:09 PM)


ichi
User

Jul 30, 2009, 9:41 PM


Views: 4389
Re: [santhosh_89] Split Operation


In Reply To
Before seeing the output,don't put the answer,Please check the output.
Example the array will have the element [a,b] in first index.But you have split based on the comma(,).If you do like wise the first element will be '[a',.......

kev is kind enough to help you, so be polite. you are the beggar here, so you can't choose.


Code
my $test="[a,b],c,'d',[e,[f,g,h,[i,j],k,l],m,n],o,p";   
@s = split(/,/,$test);
print join(",",splice(@s,0,2)) ."\n" ;
print join("," ,@s);

the above is not complete. I expect you to get the gist of it, and complete it yourself.


santhosh_89
Novice

Jul 31, 2009, 1:11 AM


Views: 4386
Re: [ichi] Split Operation

I am not asking the answer from beggar.


ichi
User

Jul 31, 2009, 1:45 AM


Views: 4385
Re: [santhosh_89] Split Operation

you are the one requesting help, so you are the "beggar". when you ask for help, you say "please". that's basic courtesy. Also, YOU are the one who is supposed to check your output. What kev can give you is only guidance. the rest, is up to you to explore.


(This post was edited by ichi on Jul 31, 2009, 1:47 AM)