File: sub-lines2words.nqp

package info (click to toggle)
nqp 2024.09%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,972 kB
  • sloc: java: 28,087; perl: 3,479; ansic: 451; makefile: 202; javascript: 68; sh: 1
file content (27 lines) | stat: -rwxr-xr-x 470 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env nqp

# create a line => words splitting function
my $line := ' #   one  two ';

say("\$line: '$line'");

my @w := words($line);

say("'$line' => words");
for @w {
    say("  word: $_");
}

sub words($line) {
    my @arr := nqp::split(' ', $line);
    my @words := [];
    for @arr {
        my $s := $_;
        # remove any whitespace
        $s := subst($s, /\s+/, '', :global);
        next if !$s;
        @words.push($s);
    }

    return @words;
}