spx2 has asked for the wisdom of the Perl Monks concerning the following question:
hello again dear monks it seems i have stumbled again on not knowing how to use switch statement properly. ok,so in response to these questions ive asked,i thank very much the people who answered me there http://perlmonks.org/?node_id=621046 http://perlmonks.org/?node_id=621054 http://perlmonks.org/?node_id=621013 and i have finally got a solution here it is:
use strict; use Switch; my $text="bla fred wilma fuck fred fuck wilma bla fred fredfred"; $text =~ s{(fred|wilma)} { my $replace_text; if($1 eq "fred") { $replace_text='wilma'; } else { $replace_text='fred'; }; $replace_text; }eg; print $text;
however to make the code i write more elegant i want to use switch instead of the if's statement but my code doesnt want to work. ive read perl documentation for switch already, no luck.
use strict; use Switch; my $text="bla fred wilma fuck fred fuck wilma bla fred fredfred"; $text =~ s{(fred|wilma)} { my $replace_text; switch($1) { case 'fred' {$replace_text='wilma'} case 'wilma' {$replace_text='fred'} }; $replace_text; }eg; print $text;
thank you
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex switch trouble
by GrandFather (Saint) on Jun 14, 2007 at 01:02 UTC | |
by diotalevi (Canon) on Jun 14, 2007 at 03:54 UTC | |
|
Re: regex switch trouble
by Util (Priest) on Jun 14, 2007 at 01:07 UTC | |
by spx2 (Deacon) on Jun 14, 2007 at 01:37 UTC | |
by graff (Chancellor) on Jun 14, 2007 at 02:07 UTC | |
by blazar (Canon) on Jun 24, 2007 at 10:04 UTC | |
by graff (Chancellor) on Jun 24, 2007 at 17:35 UTC | |
| |
by ikegami (Patriarch) on Jun 14, 2007 at 14:42 UTC | |
|
Re: regex switch trouble
by duncs (Beadle) on Jun 14, 2007 at 09:15 UTC | |
by blazar (Canon) on Jun 24, 2007 at 09:52 UTC |