in reply to string manipulation with Regex
output:use strict; use warnings; use 5.012; my $command = '8M5I4D5M'; my $input = 'M234567MI234IM234M'; while ($command =~ m/(\d+)([MID])/g) { my $value = $1; my $code = $2; given($code) { when ('D') { print 'X' x $value; } when ('I') { $input = substr($input, $value); } when ('M') { print substr($input, 0, $value); $input = substr($input, $value); } } }
M234567MXXXXM234M
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: string manipulation with Regex
by FluffyBunny (Acolyte) on Sep 01, 2010 at 20:19 UTC | |
by Marshall (Canon) on Sep 01, 2010 at 22:48 UTC | |
by FluffyBunny (Acolyte) on Sep 02, 2010 at 15:24 UTC | |
by Marshall (Canon) on Sep 02, 2010 at 15:48 UTC | |
by bluecompassrose (Initiate) on Sep 02, 2010 at 16:06 UTC | |
by CountZero (Bishop) on Sep 01, 2010 at 21:45 UTC | |
by FluffyBunny (Acolyte) on Sep 02, 2010 at 15:19 UTC |