Thursday, December 24, 2009

Re: Dupicate and increment lines

epanda wrote:

> Hi,
>
> Goal : multiply some parts of lines which contain factors
>
> foo(2)bar(9)
>
> Should become
>
> foo1bar1
> foo1bar2
> foo1bar3
> foo1bar4
> foo1bar5
> foo1bar6
> foo1bar7
> foo1bar8
> foo1bar9
> foo2bar1
> foo2bar2
> foo2bar3
> foo2bar4
> foo2bar5
> foo2bar6
> foo2bar7
> foo2bar8
> foo2bar9

Hi, here is a solution in Perl:

#!/usr/bin/perl -wn
sub expand {
my ($p, $_) = @_;
if (/\((\d+)\)/) {
my ($w, $c, $r) = ($`, $1, $');
for (1 .. $c) {
print "$p$w$_\n" unless ($r);
expand("$p$w$_", $r);
}
} else {
print "$p$_\n";
}
}
chomp;
expand '', $_;


Examples:

$ cat test.txt
foo(2)bar(9)
foobar(3)aaa

$ ./epanda.pl test.txt
foo1bar1
foo1bar2
foo1bar3
foo1bar4
foo1bar5
foo1bar6
foo1bar7
foo1bar8
foo1bar9
foo2bar1
foo2bar2
foo2bar3
foo2bar4
foo2bar5
foo2bar6
foo2bar7
foo2bar8
foo2bar9
foobar1aaa
foobar2aaa
foobar3aaa

-- Dominique

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment