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

Hi,

How do i split the single array element to many array elements in single array. below i mentioned my code. i want split the each line and will pushed into array

foreach my $arr(grep /\s+/, @array) { push (my @newarray, $arr); }

input array

var1 = "OC192-11,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"

"OC192-11,OC192:PSD,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"

"OC192-12,OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"

"OC192-12,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" ';

output array:

var1 = '"OC192-11,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"',

var2 = '"OC192-11,OC192:PSD,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"',

var3 = '"OC192-12,OC192:CVS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1"',

var1 = '"OC192-12,OC192:ESS,0,COMPL,NEND,RCV,1-DAY,02-11,00-00,1" '

Replies are listed 'Best First'.
Re: array conversion
by davido (Cardinal) on Jun 08, 2011 at 10:10 UTC
      Maybe the '#!/usr/bin/perl' part? Or am I being too optimistic? ;)

      Elda Taluta; Sarks Sark; Ark Arks
      My deviantART gallery

Re: array conversion
by moritz (Cardinal) on Jun 08, 2011 at 09:44 UTC
Re: array conversion
by GrandFather (Saint) on Jun 08, 2011 at 09:53 UTC

    For the moment forget about "how", but instead tell us about the "why". Your code does not match your descriptions and the input and output arrays appear to be identical. Paint us a bigger picture to show us what you are trying to achieve - not just in terms of an input array and output array, but what you want the full script to achieve.

    True laziness is hard work
Re: array conversion
by Eliya (Vicar) on Jun 08, 2011 at 10:01 UTC

    BTW,

    push (my @newarray, ... );

    can (or should) more clearly be written as

    my @newarray = ... ;

    because the push suggests that you're intending to add values to an already existing array. The my, OTOH, creates a new array every time you execute the statement, so the effect is the same as a simple assigment...

      i want split the each line and will pushed into array.

Re: array conversion
by locked_user sundialsvc4 (Abbot) on Jun 08, 2011 at 12:41 UTC

    Well, we’ve always heard of “the XY problem,” and here we see where those kinds of problems come from.

    When you are struggling to learn a language (or, struggling to use it without taking the time to learn it first...?), it does not pay to be a bulldog.   You will consistently find that, having grabbed on to that first toy and worked very hard to shred it completely into tatters, you have, in the end, accomplished nothing useful at all.   Oh, “you wrote a program,” all right, and eventually you even got it to work!   Unfortunately, the same task could have been accomplished with a “one-liner” in Perl or even awk.   If you didn’t first take the time to study the problem and to determine what is the best all-around way to solve it, you are spinning a little wheel in a little cage.

    Folks who take that approach “wash out” very quickly, as they are a waste of time and money for their employers, anywhere in the world.   Remember, this is engineering.

      Might I take this thought further and speculate that many who do wash out have the problem of "a small toolbox"? When all you know how to use is a hammer, you try and refactor each problem into a nail.