Schrift
[thread]8357[/thread]

RegEx-en und dann zu String evaluieren (Seite 2)



<< |< 1 2 >| >> 14 Einträge, 2 Seiten
Ronnie
 2006-09-25 22:50
#70234 #70234
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
[quote=Ronnie,25.09.2006, 16:39]
Code: (dl )
(not $condition or $a =~ s/foo/bar/) and print $a;
[/quote]
Ein Fehler ist noch drin. Sieht ihn jemand?! ;)
Linuxer
 2006-09-26 13:16
#70235 #70235
User since
2006-01-27
3891 Artikel
HausmeisterIn

user image
[quote=Ronnie,25.09.2006, 20:50][quote=Ronnie,25.09.2006, 16:39]
Code: (dl )
(not $condition or $a =~ s/foo/bar/) and print $a;
[/quote]
Ein Fehler ist noch drin. Sieht ihn jemand?! ;)[/quote]

Was macht Dein Code, wenn $a kein "foo" enthält?
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/perl
# vim: set ts=4 sw=4 et sta:
use strict;
use warnings;

my ( $cond, $aa );

for $cond ( 0 .. 1 ) {

$aa = 'fix';
( not $cond or $aa =~ s/foo/bar/ ) and print "$cond: $aa\n";

}
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!
pq
 2006-09-26 13:49
#70236 #70236
User since
2003-08-04
12209 Artikel
Admin1
[Homepage]
user image
[quote=docsnyder,25.09.2006, 16:57]
Jedenfalls erschien in der Ausgabe "1" (nicht "1bar").
[/quote]
hui, dann hat dein perl einen bug?
Code: (dl )
1
2
3
4
5
$ perl -wle'                
$condition = 1;
$a = "foo";
print $condition ? ($a=~s/foo/bar/, $a) : $a;'
1bar
\n\n

<!--EDIT|pq|1159264216-->
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
Ronnie
 2006-09-26 18:23
#70237 #70237
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
[quote=Linuxer,26.09.2006, 11:16]
Was macht Dein Code, wenn $a kein "foo" enthält?[/quote]
nix, das ist ja der Fehler ;)
lässt sich aber einfach beheben:
Code: (dl )
(not $condition or $a =~ s/foo/bar/ or 1) and print $a;

Die Lösungen mit dem ternären Operator sind da etwas schicker, aber ein wenig boolsche Logik finde ich immer nett.\n\n

<!--EDIT|Ronnie|1159281275-->
<< |< 1 2 >| >> 14 Einträge, 2 Seiten



View all threads created 2006-09-25 16:12.