RE: Replace Leading Spaces (fwd)
I realized that my previous post had the wrong comparison. Here's one that's short and to the point: re1 => q($str = ' 259.00 '; $str =~ s/\s(?=\s*\S)/0/g;), re2 => q($str = ' 259.00 '; $str =~ s/ (?= *\d)/0/g;), Benchmark: timing 100 iterations of re1, re2... re1: 3 wallclock secs ( 4.08 usr + 0.00 sys = 4.08 CPU) @ 245218.24/s (n=100) re2: 4 wallclock secs ( 4.69 usr + 0.00 sys = 4.69 CPU) @ 213310.58/s (n=100) Rate re2 re1 re2 213311/s -- -13% re1 245218/s 15% -- Using a space is about 15% slower than using \s. Weird, huh? - Mark. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces (fwd)
> I think I can explain it. When (5) sees the .*\d, the .* > grabs all the characters, then the RE engine backs up until > it "releases" a digit to match the \d. (1a), on the other > hand, just grabs spaces with \s*; it isn't allowed to grab > anything else. That wasn't it... Surprisingly, replacing the space character with "\s" made the difference(!) re1 => q($str = ' 259.00 '; $str =~ s/\s(?=\s*\S)/0/g;), re2 => q($str = ' 259.00 '; $str =~ s/ (?=.*\d)/0/g;), re2b => q($str = ' 259.00 '; $str =~ s/\s(?=\s*\d)/0/g;), Rate re2 re2b re1 re2 183891/s -- -23% -26% re2b 239751/s 30% -- -4% re1 249066/s 35% 4% -- Now, can someone explain *that*??? - Mark. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces (fwd)
Thomas, Mark - BLS CTR wrote, on Tuesday, April 11, 2006 10:28 AM : $Bill Luebkert wrote: : > Rate RE2 RE5 RE3 RE4 RE1 RE1a : > RE2 136761/s -- -58% -61% -64% -74% -74% : > RE5 326584/s 139% -- -6% -14% -37% -37% : > RE3 347705/s 154% 6% -- -9% -33% -33% : > RE4 381098/s 179% 17% 10% -- -26% -26% : > RE1 516529/s 278% 58% 49% 36% -- -0% : > RE1a 516529/s 278% 58% 49% 36% 0% -- : > : > 1 $str =~ s/\s(?=\s*\S)/0/og; : > 1a $str =~ s/\s(?=\s*\S)/0/g; : > 2 1 while ($str =~ s/\s(?=(\d|\.))/0/); : > 3 $str =~ s/^(\s+)(?=\d)/'0' x (length $1)/e; : > 4 $str =~ s/^(\s+)/sprintf "%s", q[0]x length($1)/eg; : > 5 $str =~ s/ (?=.*\d)/0/g; : : What surprises me is the substantial difference between the : lookahead expressions, RE1a and RE5. Can anyone explain why : there's such a difference? I think I can explain it. When (5) sees the .*\d, the .* grabs all the characters, then the RE engine backs up until it "releases" a digit to match the \d. (1a), on the other hand, just grabs spaces with \s*; it isn't allowed to grab anything else. Perhaps a (5a), having .*?\d, thus making the engine stop at every character to see if it's a digit before adding it to the .*?, would bring it more into line with (1a) speed-wise. Joe Joseph Discenza, Senior Programmer/Analyst mailto:[EMAIL PROTECTED] Carleton Inc. http://www.carletoninc.com 574.243.6040 ext. 300 Fax: 574.243.6060 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Replace Leading Spaces (fwd)
Thomas, Mark - BLS CTR wrote: > What surprises me is the substantial difference between the lookahead > expressions, RE1a and RE5. Can anyone explain why there's such a > difference? I may have had a cut-n-paste problem there - here's the subs : sub re0 { $str = ' 5999'; } sub re1 { $str = ' 5999'; $str =~ s/\s(?=\s*\S)/0/go; } sub re1a { $str = ' 5999'; $str =~ s/\s(?=\s*\S)/0/g; } sub re2 { $str = ' 5999'; $str =~ s/ (?=.*\d)/0/g; } sub re3 { $str = ' 5999'; 1 while ($str =~ s/\s(?=(\d|\.))/0/); } sub re4 { $str = ' 5999'; $str =~ s/^(\s+)(?=\d)/'0' x (length $1)/e; } sub re5 { $str = ' 5999'; $str =~ s/^(\s+)/sprintf "%s",q[0]x length($1)/eg; } And two runs of results of 100 (re0 is just for a baseline) : Rate RE3 RE5 RE4 RE2 RE1a RE1 RE0 RE3 133887/s-- -53% -55% -63% -69% -69% -94% RE5 283206/s 112%-- -5% -21% -34% -34% -87% RE4 299043/s 123%6%-- -16% -30% -31% -86% RE2 357526/s 167% 26% 20%-- -17% -17% -83% RE1a 429738/s 221% 52% 44% 20%-- -1% -80% RE1 432339/s 223% 53% 45% 21%1%-- -80% RE0 2132196/s 1493% 653% 613% 496% 396% 393%-- Rate RE3 RE5 RE4 RE2 RE1a RE1 RE0 RE3 131961/s-- -53% -56% -63% -70% -70% -94% RE5 282008/s 114%-- -5% -22% -36% -36% -87% RE4 297619/s 126%6%-- -17% -32% -33% -86% RE2 359583/s 172% 28% 21%-- -18% -19% -83% RE1a 438404/s 232% 55% 47% 22%-- -1% -79% RE1 441306/s 234% 56% 48% 23%1%-- -79% RE0 2132196/s 1516% 656% 616% 493% 386% 383%-- ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces (fwd)
$Bill Luebkert wrote: > Rate RE2 RE5 RE3 RE4 RE1 RE1a > RE2 136761/s -- -58% -61% -64% -74% -74% > RE5 326584/s 139% -- -6% -14% -37% -37% > RE3 347705/s 154% 6% -- -9% -33% -33% > RE4 381098/s 179% 17% 10% -- -26% -26% > RE1 516529/s 278% 58% 49% 36% -- -0% > RE1a 516529/s 278% 58% 49% 36% 0% -- > > 1 $str =~ s/\s(?=\s*\S)/0/og; > 1a $str =~ s/\s(?=\s*\S)/0/g; > 2 1 while ($str =~ s/\s(?=(\d|\.))/0/); > 3 $str =~ s/^(\s+)(?=\d)/'0' x (length $1)/e; > 4 $str =~ s/^(\s+)/sprintf "%s", q[0]x length($1)/eg; > 5 $str =~ s/ (?=.*\d)/0/g; What surprises me is the substantial difference between the lookahead expressions, RE1a and RE5. Can anyone explain why there's such a difference? - Mark. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Replace Leading Spaces (fwd)
Glenn Linderman wrote: > On approximately 4/7/2006 12:11 PM, came the following characters from > the keyboard of Nelson R. Pardee: > >>I've included timings for 1 iterations for each of the proposed >>solutions. >> >>0.056398 s/\s(?=\s*\S)/0/og >>0.254457 while (s/\s(?=(\d|\.))/0/ {) >>0.094268 s/^(\s+)(?=\d)/'0'x(length $1)/e >>0.026934 (see below) Strip front space, take length diff, replace with n x "0" >>0.095046 s/^(\s+)/sprintf "%s", q[0]x length($1)/eg >>0.086842 s/ (?=.*\d)/0/g >> >>Surprisingly, the more manual process is the fastest. This latest positive >>lookahead is a bit slower than the first one, I'm not sure why. > > > I'm not sure either, nor of your measurement technique, but I do notice > that the top lookahead uses flags /og and the bottom one only uses /g. > Maybe that contributes to the difference in timing? Shouldn't matter if no $vars involved. Rate RE2 RE5 RE3 RE4 RE1 RE1a RE2 136761/s -- -58% -61% -64% -74% -74% RE5 326584/s 139% -- -6% -14% -37% -37% RE3 347705/s 154% 6% -- -9% -33% -33% RE4 381098/s 179% 17% 10% -- -26% -26% RE1 516529/s 278% 58% 49% 36% -- -0% RE1a 516529/s 278% 58% 49% 36% 0% -- 1 $str =~ s/\s(?=\s*\S)/0/og; 1a $str =~ s/\s(?=\s*\S)/0/g; 2 1 while ($str =~ s/\s(?=(\d|\.))/0/); 3 $str =~ s/^(\s+)(?=\d)/'0' x (length $1)/e; 4 $str =~ s/^(\s+)/sprintf "%s", q[0]x length($1)/eg; 5 $str =~ s/ (?=.*\d)/0/g; Each RE was preceded with re-initing the string. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
> > Nelson, > > Please add Mark Thomas' solution to your timings to see how > it compares > to the others: > I'd be curious to see how Wags' sprintf compares as well: s/^(\s+)/sprintf "%s", q[0]x length($1)/eg; ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Replace Leading Spaces (fwd)
On Fri, 7 Apr 2006, Glenn Linderman wrote: > On approximately 4/7/2006 12:11 PM, came the following characters from > the keyboard of Nelson R. Pardee: > > I've included timings for 1 iterations for each of the proposed > > solutions. > > > > 0.056398 s/\s(?=\s*\S)/0/og > > 0.254457 while (s/\s(?=(\d|\.))/0/ {) > > 0.094268 s/^(\s+)(?=\d)/'0'x(length $1)/e > > 0.026934 (see below) Strip front space, take length diff, replace with n x > > "0" > > 0.095046 s/^(\s+)/sprintf "%s", q[0]x length($1)/eg > > 0.086842 s/ (?=.*\d)/0/g > > > > Surprisingly, the more manual process is the fastest. This latest positive > > lookahead is a bit slower than the first one, I'm not sure why. > > I'm not sure either, nor of your measurement technique, but I do notice > that the top lookahead uses flags /og and the bottom one only uses /g. > Maybe that contributes to the difference in timing? I checked /o- for this regex it doesn't make much difference, although it can indeed make a difference. Here's the code snippet for timing- I replace the interior of the for loop for each variation. prd uses Time::Hires gettimeofday to track time used. The prd arguments are just stuff to print out. $_=$val="259.00 "; $c=1; &prd(__LINE__." 1"); for $i(0..$c){ $_=$val; s/\s(?=\s*\S)/0/g; } print "$_ "; &prd(__LINE__.' s/\s(?=\s*\S)/0/og'); ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
May not have hit your inbox yet... 0.056398 s/\s(?=\s*\S)/0/og 0.254457 while (s/\s(?=(\d|\.))/0/ {) 0.094268 s/^(\s+)(?=\d)/'0'x(length $1)/e 0.026934 (see below) Strip front space, take length diff, replace with n x "0" 0.095046 s/^(\s+)/sprintf "%s", q[0]x length($1)/eg 0.086842 s/ (?=.*\d)/0/g Surprisingly, the more manual process is the fastest. This latest positive lookahead is a bit slower than the first one, I'm not sure why. -- ($trimmed_string = $_) =~ s/^ *//; # remove leading spaces $transformed_string = ('0' x ((length $_) - length $trimmed_string)) . $trimmed_string; ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Replace Leading Spaces
Ken Kriesel wrote: Why not the more concise $string =~ s/^(\s+)/'0'x(length $1)/e; Thanks, that is exactly the same as Paul's solution. Minus the spaces around the 'x'. -- Lyle Kopnicky Software Project Engineer Veicon Technology, Inc. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
Why not the more concise $string =~ s/^(\s+)/'0'x(length $1)/e; as in my $string = ' 259.00 '; print "<$string>\n"; #$string =~ s/^(\s+)(?=\d)/'0'x(length $1)/e; $string =~ s/^(\s+)/'0'x(length $1)/e; print "<$string>\n"; which outputs: < 259.00 > <0259.00 > showing same spacing fore & aft. ^\s+ alone will match any leading white-space, up to where a non-white-space character \S is encountered. Characters that can have more than one cell width, like \t, could require special handling if present. Ken At 10:56 AM 4/7/2006, Paul Sobey wrote: >> My $string = ' 259.00 '; >> >> Note that I don't want to change the trailing space character. The >> resulting string would look like: >> >> '0259.00 ' >> >> The total length of the string would remain the same after the replace >> operation. >> >> I'm just having a total brain-fade on this one. > >$string =~ s/^(\s+)/'0' x length($1)/e > >Does that do what you want? > >P. > >* >Gloucester Research Limited believes the information >provided herein is reliable. While every care has been >taken to ensure accuracy, the information is furnished >to the recipients with no warranty as to the completeness >and accuracy of its contents and on condition that any >errors or omissions shall not be made the basis for any >claim, demand or cause for action. > >The information in this email is intended only for the >named recipient. If you are not the intended recipient >please notify us immediately and do not copy, distribute >or take action based on this e-mail. > >Gloucester Research Limited, 5th Floor, Whittington House, >19-30 Alfred Place, London WC1E 7EA >* > >___ >Perl-Win32-Users mailing list >Perl-Win32-Users@listserv.ActiveState.com >To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
Oops, I see I misattributed 2 lines. my $string = ' 259.00 '; print "<$string>\n"; #$string =~ s/^(\s+)(?=\d)/'0'x(length $1)/e; #Mike Arms posted $string =~ s/^(\s+)/'0'x(length $1)/e; #Paul Sobey; quicker than above line print "<$string>\n"; which outputs: < 259.00 > <0259.00 > Ken At 10:56 AM 4/7/2006, Paul Sobey wrote: >> My $string = ' 259.00 '; >> >> Note that I don't want to change the trailing space character. The >> resulting string would look like: >> >> '0259.00 ' >> >> The total length of the string would remain the same after the replace >> operation. >> >> I'm just having a total brain-fade on this one. > >$string =~ s/^(\s+)/'0' x length($1)/e > >Does that do what you want? > >P. > >* >Gloucester Research Limited believes the information >provided herein is reliable. While every care has been >taken to ensure accuracy, the information is furnished >to the recipients with no warranty as to the completeness >and accuracy of its contents and on condition that any >errors or omissions shall not be made the basis for any >claim, demand or cause for action. > >The information in this email is intended only for the >named recipient. If you are not the intended recipient >please notify us immediately and do not copy, distribute >or take action based on this e-mail. > >Gloucester Research Limited, 5th Floor, Whittington House, >19-30 Alfred Place, London WC1E 7EA >* > >___ >Perl-Win32-Users mailing list >Perl-Win32-Users@listserv.ActiveState.com >To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces (fwd)
I've included timings for 1 iterations for each of the proposed solutions. 0.056398 s/\s(?=\s*\S)/0/og 0.254457 while (s/\s(?=(\d|\.))/0/ {) 0.094268 s/^(\s+)(?=\d)/'0'x(length $1)/e 0.026934 (see below) Strip front space, take length diff, replace with n x "0" 0.095046 s/^(\s+)/sprintf "%s", q[0]x length($1)/eg 0.086842 s/ (?=.*\d)/0/g Surprisingly, the more manual process is the fastest. This latest positive lookahead is a bit slower than the first one, I'm not sure why. -- ($trimmed_string = $_) =~ s/^ *//; # remove leading spaces $transformed_string = ('0' x ((length $_) - length $trimmed_string)) . $trimmed_string; ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
Dirk Bremer - Senior Systems Engineer - ESS/AMS - NISC Lake St. Louis MO - USA Central Time Zone 636-755-2652 fax 636-755-2503 [EMAIL PROTECTED] www.nisc.coop > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On > Behalf Of Nelson R. Pardee > Sent: Friday, April 07, 2006 13:20 > To: Active State Perl > Subject: RE: Replace Leading Spaces > > Try # 2: > The first is my new one using a positive lookahead assertion. > I've included timings for 1 iterations for each of the proposed > solutions. > > 0.056398 s/\s(?=\s*\S)/0/og > 0.254457 while (s/\s(?=(\d|\.))/0/ {) > 0.094268 s/^(\s+)(?=\d)/'0'x(length $1)/e > Nelson, Please add Mark Thomas' solution to your timings to see how it compares to the others: s/ (?=.*\d)/0/g Dirk Bremer - Senior Systems Engineer - ESS/AMS - NISC Lake St. Louis MO - USA Central Time Zone 636-755-2652 fax 636-755-2503 [EMAIL PROTECTED] www.nisc.coop ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
> Using a regex, I want to replace each leading space-character > with a corresponding zero-character on a one-to-one basis. > For an example > string: > > My $string = ' 259.00 '; > > Note that I don't want to change the trailing space > character. The resulting string would look like: > > '0259.00 ' How about s/ (?=.*\d)/0/g; Translation: any space with a digit to its right gets replaced with a zero. -- Mark Thomas Internet Systems Architect ___ BAE SYSTEMS Information Technology 2525 Network Place Herndon, VA 20171 USA ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
> -Original Message- > From: Thomas, Mark - BLS CTR [mailto:[EMAIL PROTECTED] > Sent: Friday, April 07, 2006 13:15 > To: Dirk Bremer; Perl-Win32-Users@listserv.ActiveState.com > Subject: RE: Replace Leading Spaces > > > Using a regex, I want to replace each leading space-character > > with a corresponding zero-character on a one-to-one basis. > > For an example > > string: > > > > My $string = ' 259.00 '; > > > > Note that I don't want to change the trailing space > > character. The resulting string would look like: > > > > '0259.00 ' > > How about > > s/ (?=.*\d)/0/g; > > Translation: any space with a digit to its right gets replaced with a > zero. > > -- > Mark Thomas Mark, I also like your solution, thanks! Dirk Bremer - Senior Systems Engineer - ESS/AMS - NISC Lake St. Louis MO - USA Central Time Zone 636-755-2652 fax 636-755-2503 [EMAIL PROTECTED] www.nisc.coop ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
Try # 2: The first is my new one using a positive lookahead assertion. I've included timings for 1 iterations for each of the proposed solutions. 0.056398 s/\s(?=\s*\S)/0/og 0.254457 while (s/\s(?=(\d|\.))/0/ {) 0.094268 s/^(\s+)(?=\d)/'0'x(length $1)/e On Fri, 7 Apr 2006, Dirk Bremer wrote: > > Using a regex, I want to replace each leading space-character with a > > corresponding zero-character on a one-to-one basis. For an example > > string: > > > > My $string = ' 259.00 '; > > > > Note that I don't want to change the trailing space character. The > > resulting string would look like: > > > > '0259.00 ' > > > > The total length of the string would remain the same after the replace > > operation. --Nelson R. Pardee, Support Analyst, Information Technology & Services-- --Syracuse University, 211 Machinery Hall, Syracuse, NY 13244-1260-- --(315) 443-1079 [EMAIL PROTECTED] -- ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Replace Leading Spaces
Dirk Bremer wrote: All right, in the mean time, I have come up with the following: while (s/\s(?=(\d|\.))/0/) {} This works nicely, but I' wondering if it can be accomplished without looping and perhaps more efficiently as well. Your thoughts? I think that's kind of confusing. I like Paul's suggestion. It's short, simple, I look at it and can see what it means right away. Here's another way, that should be pretty efficient: my $string = ' 259.00 '; (my $trimmed_string = $string) ~= s/^ *//; # remove leading spaces my $transformed_string = '0' x (length $string - length $trimmed_string) . $trimmed_string; -- Lyle Kopnicky Software Project Engineer Veicon Technology, Inc. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Replace Leading Spaces
On Fri, 7 Apr 2006, Nelson R. Pardee wrote: > Don't know if this is the most efficient, but it seems to work for me... > s/^(0?\s)/0/g; Another brain fade!. This doesn't work. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Replace Leading Spaces
Don't know if this is the most efficient, but it seems to work for me... s/^(0?\s)/0/g; On Fri, 7 Apr 2006, Dirk Bremer wrote: > Using a regex, I want to replace each leading space-character with a > corresponding zero-character on a one-to-one basis. For an example > string: > > My $string = ' 259.00 '; > > Note that I don't want to change the trailing space character. The > resulting string would look like: > > '0259.00 ' > > The total length of the string would remain the same after the replace > operation. --Nelson R. Pardee, Support Analyst, Information Technology & Services-- --Syracuse University, 211 Machinery Hall, Syracuse, NY 13244-1260-- --(315) 443-1079 [EMAIL PROTECTED] -- ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
[EMAIL PROTECTED] wrote: > Using a regex, I want to replace each leading space-character with a > corresponding zero-character on a one-to-one basis. For an example > string: > > My $string = ' 259.00 '; > > Note that I don't want to change the trailing space character. The > resulting string would look like: > > '0259.00 ' > > The total length of the string would remain the same after the replace > operation. > > I'm just having a total brain-fade on this one. It was brain fade here also, but finally found one way: #!perl use strict; use warnings; $_ = ' 259.00 '; print $_ . "\n"; s/^(\s+)/sprintf "%s", q[0]x length($1)/eg; print $_ . "\n"; Output: 259.00 00259.00 hth Wags ;) > > Dirk Bremer - Senior Systems Engineer - ESS/AMS - NISC Lake St. Louis > MO - USA Central Time Zone > 636-755-2652 fax 636-755-2503 > > [EMAIL PROTECTED] > www.nisc.coop > > ___ > Perl-Win32-Users mailing list > Perl-Win32-Users@listserv.ActiveState.com > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs *** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. *** ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
Dirk Bremer [Dirk.Bremer AT nisc.coop] wrote: > Using a regex, I want to replace each leading space-character with a > corresponding zero-character on a one-to-one basis. For an example > string: > > my $string = ' 259.00 '; > > Note that I don't want to change the trailing space character. The > resulting string would look like: > > '0259.00 ' > > The total length of the string would remain the same after the > replace operation. Hi, Dirk, You mean something like this? $string =~ s/^(\s+)(?=\d)/'0'x(length $1)/e; The power of the 'e' modifier on the substitution function. :-) -- Mike Arms ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
> -Original Message- > From: Arms, Mike [mailto:[EMAIL PROTECTED] > Sent: Friday, April 07, 2006 11:37 > To: Perl-Win32-Users@listserv.ActiveState.com > Cc: Dirk Bremer > Subject: RE: Replace Leading Spaces > > Dirk Bremer [Dirk.Bremer AT nisc.coop] wrote: > > Using a regex, I want to replace each leading space-character with a > > corresponding zero-character on a one-to-one basis. For an example > > string: > > > > my $string = ' 259.00 '; > > > > Note that I don't want to change the trailing space character. The > > resulting string would look like: > > > > '0259.00 ' > > > > The total length of the string would remain the same after the > > replace operation. > > Hi, Dirk, > > You mean something like this? > > $string =~ s/^(\s+)(?=\d)/'0'x(length $1)/e; > > The power of the 'e' modifier on the substitution function. :-) > > -- > Mike Arms > Now that one is pretty darn cool and something I didn't know about. Thanks Mike! Dirk Bremer - Senior Systems Engineer - ESS/AMS - NISC Lake St. Louis MO - USA Central Time Zone 636-755-2652 fax 636-755-2503 [EMAIL PROTECTED] www.nisc.coop ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Replace Leading Spaces
Using a regex, I want to replace each leading space-character with a corresponding zero-character on a one-to-one basis. For an example string: My $string = ' 259.00 '; Note that I don't want to change the trailing space character. The resulting string would look like: '0259.00 ' The total length of the string would remain the same after the replace operation. I'm just having a total brain-fade on this one. Perl sprintf or printf built-in function. 'perldoc -f sprintf' printf '<%06s>', 12; # prints "<12>" ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
> -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On > Behalf Of Dirk Bremer > Sent: Friday, April 07, 2006 09:52 > To: Perl-Win32-Users@listserv.ActiveState.com > Subject: Replace Leading Spaces > > Using a regex, I want to replace each leading space-character with a > corresponding zero-character on a one-to-one basis. For an example > string: > > My $string = ' 259.00 '; > > Note that I don't want to change the trailing space character. The > resulting string would look like: > > '0259.00 ' > > The total length of the string would remain the same after the replace > operation. > > I'm just having a total brain-fade on this one. > All right, in the mean time, I have come up with the following: while (s/\s(?=(\d|\.))/0/) {} This works nicely, but I' wondering if it can be accomplished without looping and perhaps more efficiently as well. Your thoughts? Dirk Bremer - Senior Systems Engineer - ESS/AMS - NISC Lake St. Louis MO - USA Central Time Zone 636-755-2652 fax 636-755-2503 [EMAIL PROTECTED] www.nisc.coop ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Replace Leading Spaces
> My $string = ' 259.00 '; > > Note that I don't want to change the trailing space character. The > resulting string would look like: > > '0259.00 ' > > The total length of the string would remain the same after the replace > operation. > > I'm just having a total brain-fade on this one. $string =~ s/^(\s+)/'0' x length($1)/e Does that do what you want? P. * Gloucester Research Limited believes the information provided herein is reliable. While every care has been taken to ensure accuracy, the information is furnished to the recipients with no warranty as to the completeness and accuracy of its contents and on condition that any errors or omissions shall not be made the basis for any claim, demand or cause for action. The information in this email is intended only for the named recipient. If you are not the intended recipient please notify us immediately and do not copy, distribute or take action based on this e-mail. Gloucester Research Limited, 5th Floor, Whittington House, 19-30 Alfred Place, London WC1E 7EA * ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs