简体   繁体   中英

Splitting array using perl

This is my array buff and I have stored the contents of the array as follows:

$buff[0]="3\nHi how are u I am f";
$buff[1]="ine\n The world is so";
$buff[2]="beautiful.\n";
$buff[3]="I love it.";

I want to split the array and print as

3
Hi how are u I am fine
The world is so beautiful.
I love it.

How do I accomplish this? I tried using split function like this:

my @split_buff=split('\n', @buff);

foreach my $val (@split_buff) {
     print $val;
}

But I am not able to split with \\n as the delimiter, and I need to retain the delimiter as well. What's wrong in the code?

我认为您可以简单地print它。

print @buff;

Join all together and split by \\n\\s*

my @split_buff = split /\n\s*/, join "", @buff;

foreach my $val (@split_buff) {

  print "$val\n";
}

I like answer: "print @buff;". You can also try this code:

    $text = ""; # or my $text;
    foreach $line (@buff) { $text = $text . "$line"; }
    print $text;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM