简体   繁体   中英

Perl: transform string into array and print line number

I have the following string Today, the sky is blue. and I would like to do two things with it.

First, I'd like to turn it into the following array (or hash, not sure which one is the right one).

Today
,

the

sky 

is 

blue
.

I can do it using this grep grep -Eo '\\w+|[^\\w ]' input.txt , but could somebody please tell me how to do this using perl (in order to run this as part of a script for example).

Second thing, I'd like to add the line number to each line of my array (I know it's there somewhere but I want to actually see it), resulting in the following final output:

Today     1
,         2
          3
the       4
          5
sky       6
          7
is        8
          9
blue      10
.         11

Could somebody help me use that grep function in a perlish context and print the corresponding line number ? Thanks a lot in advance !

Here is a way to do the job:

my $text = "Today, the sky is blue.";
my @list = split/(\W)/, $text;
my $i = 1;
for (@list) {
    say $_,"\t",$i++ if $_ ne '';
}

Output:

Today   1
,   2
    3
the 4
    5
sky 6
    7
is  8
    9
blue    10
.   11

For matching regexes in Perl you have the matching operator, you will find many examples and explanations about it in the PerlRegex tutorial .

So you would reuse whatever regexes like this; here I am putting the results into an array, that contains the matches. Any postprocessing should be applied to this array. Here I am just outputting it.

One more detail concerning your regex - if I do this, using the regex you gave

echo "Today, the sky is blue." | grep -Eo '\w+|[^\w ]'

The output is:

Today
,
the
sky
is
blue
.

This is because your negated character class [^\\w ] not only excludes alphanumeric characters but spaces too.

However, according to your output you probably have [^\\w] here, so the space, which is not part of \\w is treated as match. So I am using this regex here too. By the way: I wonder, if you really want every non-word character to be a match of its own.

#!/usr/bin/perl
#

use strict;
use warnings;

while (<DATA>)
{
    my (@matches) = $_ =~ m/(\w+|[^\w])/g;

    print join("\n", @matches);
}

__DATA__
Today, the sky is blue.

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