scalar to array?
am 29.01.2008 19:50:15 von Shion
What is the easiest way to push a scalar variable containing text into
an array?
i.e.
$text = 'here is
a line
that has
four lines';
@lines = ???
the end result is:
@lines =('here is', 'a line', 'that has', 'four lines');
Re: scalar to array?
am 29.01.2008 19:58:15 von xhoster
monkeys paw wrote:
> What is the easiest way to push a scalar variable containing text into
> an array?
> i.e.
>
> $text = 'here is
> a line
> that has
> four lines';
>
> @lines = ???
>
> the end result is:
>
> @lines =('here is', 'a line', 'that has', 'four lines');
push @lines, split /\n/, $text;
Or if didn't really want to push by rather assign:
my @lines=split /\n/, $text;
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
Re: scalar to array?
am 29.01.2008 20:13:52 von Jim Gibson
In article , monkeys
paw wrote:
> What is the easiest way to push a scalar variable containing text into
> an array?
So close! If only you had asked ' ... to split a scalar variable ...'.
Then you would have asked a "self-answering question" (SAQ).
>
> i.e.
>
> $text = 'here is
> a line
> that has
> four lines';
>
> @lines = ???
>
> the end result is:
>
> @lines =('here is', 'a line', 'that has', 'four lines');
@lines = split(/\n/,$text);
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: scalar to array?
am 29.01.2008 20:39:52 von jurgenex
monkeys paw wrote:
>What is the easiest way to push a scalar variable containing text into
>an array?
That is a SAQ: perldoc -f push
>i.e.
>
>$text = 'here is
>a line
>that has
>four lines';
>
>@lines = ???
>
>the end result is:
>
>@lines =('here is', 'a line', 'that has', 'four lines');
But this has nothing to do with pushing a single scalar into an array as you
asked for at the beginning.
Just split// the text at newline and then push() or splice() the new
elements into the array. Or you could use an array slice instead, too.
jue