Thread /String+/ ersetzen mit String2 für alle /String+/ (11 answers)
Opened by PerlProfi at 2006-12-03 19:54

topeg
 2006-12-03 21:00
#72216 #72216
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Ich würde es aus dem stehgreif so machen:
Code: (dl )
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
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/perl
use strict;
use warnings;

my $str=
'$r->vor;
$r->vor;
$r->vor;
$r->drehe_rechts;
$r->vor;
$r->vor;
$r->drehe_links;
$r->vor;
$r->vor;
$r->vor;
$r->vor;
$r->vor;
drehe_um;
$r->vor;
';

my $cnt=0;
my @arr=();
for my $i (split(/(?<=;\n)/,$str))
{
push(@arr,$i) && next unless(@arr);
if($arr[-1] eq $i && $i=~/\$r->vor;\n/)
{ $cnt++; }
else
{
if($cnt)
{
$arr[-1]='vor('.($cnt+1).");\n";
$cnt=0;
}
push(@arr,$i);
}
}

print join ('',@arr);


Wobei es sicher noch eleganter geht. :-)

Edit:
Es gibt einen Einzeiler :-)
Code: (dl )
$str=$`.'vor('.scalar(@{[split(/;\n/,$&)]}).");\n".$' while($str=~/(?:\$r->vor;\n){2,}/s);
\n\n

<!--EDIT|topeg|1165173498-->

View full thread /String+/ ersetzen mit String2 für alle /String+/