[ubuntu-florida] Perl, REGEX, modifiers and arrays

j.e.aneiros jesus.aneiros at gmail.com
Tue Sep 21 20:13:20 BST 2010


Suppose you have a file like this:

 1* 2* 3 4 5 6
10 20 30* 40 50* 60
100 200 300 400* 500 600
1000 2000* 3000 4000 5000* 6000*

You need to extract the group of numbers per line that have a * associated,
the result should be like this:

1 2
30 50
400
2000 5000 6000

Could you find a language that you obtain the same result with less lines of
code than Perl?

#!/usr/bin/perl

use strict;
use warnings;

while (<>) {

chomp;

my @Data = /(\d+)\*/g;

print "@Data\n";
}

__END__


You can do it even with a one-liner:

janeiros at harlie:/media/disk$ perl -ne '@Data = /(\d+)\*/g; print "@Data\n"'
data.txt
1 2
30 50
400
2000 5000 6000



More information about the Ubuntu-us-fl mailing list