1

How do I convert a multiline string to array?

my $text= " ads da
sda
s 
da
d
as

das
d a as dasd
\n

";

Note : I dont want to remove or delete newline ?

5
  • 1
    This is a poor question. Define your scope more clearly. What does the string contain? Is that a literal '\n'? Do you want to ignore empty lines? Commented Nov 19, 2010 at 14:43
  • 1
    i dont want to remove new line and empty lines ... Commented Nov 19, 2010 at 14:48
  • 1
    so what do you want to split on? Commented Nov 19, 2010 at 15:38
  • 1
    i never said that i want split . I need to convert string to array Commented Nov 19, 2010 at 16:35
  • 3
    What do you mean by "convert"? Show what the array you want looks like for your example case. Commented Nov 19, 2010 at 17:23

5 Answers 5

25

As it stands, the question could be worded more clearly.

my @text = split "\n", $text;
Sign up to request clarification or add additional context in comments.

3 Comments

Downvoter(s) : Care to explain what's wrong with this answer?
this is the best solution for a list of strings with fixed width text fields
split "\n" doesn't catch as many lines as split /^/ on a string of html content I got via curl. I guess it is because the string contains mixed CR and LF characters.
16

You could split on the beginnings of the lines by using the ^ metacharacter and the m regexp modifier (letting ^ match the beginning of the line instead of just the beginning of the string):

split /^/m, $text

Actually, you can leave out the m since split puts it in for you in this case. From perldoc -f split: "A PATTERN of "/^/" is treated as if it were "/^/m", since it isn’t much use otherwise."

Using your value for $text, this code:

use Data::Dumper;
$Data::Dumper::Useqq=1;
print Data::Dumper->Dump([[split /^/, $text]], ["*text"]);

Prints this:

@text = (
          " ads da\n",
          "sda\n",
          "s \n",
          "da\n",
          "d\n",
          "as\n",
          "\n",
          "das\n",
          "d a as dasd\n",
          "\n",
          "\n",
          "\n"
        );

Comments

2

Keeping in mind that the first argument to split is a pattern:

#!/usr/bin/perl

use strict; use warnings;
use YAML;

my $text = " ads da
sda
s
da
d
as

das
d a as dasd
\n

";

print Dump [ split /(\n)/, $text ];

Output:

---
- ' ads da'
- "\n"
- sda
- "\n"
- s
- "\n"
- da
- "\n"
- d
- "\n"
- as
- "\n"
- ''
- "\n"
- das
- "\n"
- d a as dasd
- "\n"
- ''
- "\n"
- ''
- "\n"
- ''
- "\n"

Comments

1

I had fun putting this one together: Voila! Your string is now an array without split-ting it:

use strict qw<subs vars>;
use warnings;

@{" ads da
sda
s 
da
d
as

das
d a as dasd
\n
"} = 1..3
;

Comments

1

My sense is you are focusing on the wrong problem.

Instead of trying to convert a scalar multi-line string constant into a list, maybe your question should be "How do I have a multi-line string initiated into a Perl list or array?"

Look at Perl's List value constructors in Perldata.

Of particular applicability to your question is how to use a heredoc to initiate an array with a multi-line string:

#!/usr/bin/perl
use strict; use warnings;
use YAML;

my @text= <<END =~ m/(^.*\n)/mg;
 ads da
sda
s 
da
d
as

das
d a as dasd
\n

END

print Dump \@text;

Prints:

---
- " ads da\n"
- "sda\n"
- "s \n"
- "da\n"
- "d\n"
- "as\n"
- "\n"
- "das\n"
- "d a as dasd\n"
- "\n"
- "\n"
- "\n"

Use the idioms Luke!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.