[EMAIL PROTECTED] wrote:
Hi all,
again i am sending a message to this list hoping you bear with me and my
novice question.
In my script i would like to split a single scalar containing a random
passage of text into a list, which holds the words of the text.
What i found in the perl documentation is the following example [perlrequick - The split operator]:
#For example, to split a string into words, use
#
# $x = "Calvin and Hobbes";
# @word = split /\s+/, $x; # $word[0] = 'Calvin'
# $word[1] = 'and'
# $word[2] = 'Hobbes'
For my case however i would like to have also linefeeds treated as "words",
because i want to preserve them (in order not to mix up the layout of the
text when writing the list of words to an outfile).
Consequently i would think that using like in the example above
#!/usr/bin/perl
use strict;
use warnings;
my $all_of_it = <<EOF;
Word a_linefeed_follows_now
Word
EOF
# my @word = grep length, split( /(\n)|\s+/, $all_of_it );
# -or-
# my @word = grep $_ =~ /\n|\S+/, split( /(\s+)/, $all_of_it );
foreach (@word) {
print "This element is: ", ( $_ eq "\n" ? '\\n' : $_ ), "\n";
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>