|
Posted by monkeys paw on January 29, 2008, 1:50 pm
Please log in for more thread options
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');
|
|
Posted by xhoster on January 29, 2008, 1:58 pm
Please log in for more thread options
> 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.
|
|
Posted by Jim Gibson on January 29, 2008, 2:13 pm
Please log in for more thread options
> 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
|
|
Posted by Jürgen Exner on January 29, 2008, 2:39 pm
Please log in for more thread options >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
|
| Similar Threads | Posted | | Using JOIN: Converting scalar to array | August 23, 2005, 7:18 pm |
| assigning array to a scalar variable | March 14, 2008, 4:39 am |
| How to pass an array and scalar as arguments to a subroutine/ | June 28, 2006, 12:16 pm |
| I need to extract an array from a scalar regex-wise ? | May 9, 2008, 2:22 pm |
| substr forces scalar context with array argument | November 29, 2005, 7:18 am |
| Mail::Box::Manager message body scalar or array? | October 15, 2006, 8:33 pm |
| Dear gurus how can I extract an ARRAY from a scalar regex-wise | May 9, 2008, 2:18 pm |
| Replace scalar in another scalar | January 27, 2005, 3:21 pm |
| How does a scalar know what it is? | July 24, 2005, 2:16 am |
| I Broke a scalar | August 25, 2004, 10:43 pm |
|