For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > January 2008 > scalar to array?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author scalar to array?
monkeys paw

2008-01-29, 7:21 pm

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');
xhoster@gmail.com

2008-01-29, 7:21 pm

monkeys paw <user@example.net> 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.
Jim Gibson

2008-01-29, 7:21 pm

In article < MKadnS_EYNTD6ALanZ2dnUVZ_vyinZ2d@insight
bb.com>, monkeys
paw <user@example.net> 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).
<http://www.ginini.com/perlsaq.html>

>
> 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
Jürgen Exner

2008-01-29, 7:21 pm

monkeys paw <user@example.net> 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com