![]() |
dumb newbie question (or newbie dumb question)
Hi, I have the following code which finds and prints the first 2 digits
in a string. I'm trying to make it more efficient. Here's the code: $sentence = "January 23, 1992"; if ($sentence =~ /\d\d/) { $sentence = "$&"; } print $sentence; If I know that "$sentence" will ALWAYS have a two digit date, is there a way I can get rid of the "if - then" statement and just have it print $sentence??? I tried this, and it doesn't work: $sentence = "January 23, 1992"; $sentence = /\d\d/; print $sentence; I'm just trying to make my script slightly more efficient by getting rid of that "if - then" statement. Thanks, -Jerry p.s. I really appreciate your help! If you could answer this question, too, it would be greatly appreciated: In Perl Version 5.6.1, this code worked, but in version 5.8.0, it no longer works. Any idea why? $sentence = "January 23, 1992"; $sentence =~ s/[^\d]+//g; $sentence = substr($sentence,0,2); $sentence =~ s/\b0//; print $sentence; |
Re: dumb newbie question (or newbie dumb question)
"Jerry C." <jerry_c48@cordollisonline.com> wrote:
: Subject: dumb newbie question (or newbie dumb question) In future, please choose a Subject line that describes the subject of the article, not yourself or the quality of the article. See the clpm posting guidelines at http://mail.augustmail.com/~tadmc/clpmisc.shtml . : Hi, I have the following code which finds and prints the first 2 digits : in a string. I'm trying to make it more efficient. Here's the code: : : $sentence = "January 23, 1992"; : if ($sentence =~ /\d\d/) { $sentence = "$&"; } : print $sentence; : : If I know that "$sentence" will ALWAYS have a two digit date, is there a : way I can get rid of the "if - then" statement and just have it print : $sentence??? I tried this, and it doesn't work: : : $sentence = "January 23, 1992"; : $sentence = /\d\d/; : print $sentence; Add capturing parentheses to the regex and assign the return from m// in list context to $sentence. ($sentence) = $sentence =~ /(\d\d)/; : p.s. I really appreciate your help! If you could answer this question, : too, it would be greatly appreciated: In Perl Version 5.6.1, this code : worked, but in version 5.8.0, it no longer works. Any idea why? : : $sentence = "January 23, 1992"; : $sentence =~ s/[^\d]+//g; : $sentence = substr($sentence,0,2); : $sentence =~ s/\b0//; : print $sentence; "It no longer works" does nothing to describe the problem. What should the code do that it does not? What does the code do that it should not? |
Re: dumb newbie question (or newbie dumb question)
> ($sentence) = $sentence =~ /(\d\d)/;
> > : p.s. I really appreciate your help! If you could answer this question, > : too, it would be greatly appreciated: In Perl Version 5.6.1, this code > : worked, but in version 5.8.0, it no longer works. Any idea why? > : > : $sentence = "January 23, 1992"; > : $sentence =~ s/[^\d]+//g; > : $sentence = substr($sentence,0,2); > : $sentence =~ s/\b0//; > : print $sentence; > > "It no longer works" does nothing to describe the problem. > > What should the code do that it does not? > What does the code do that it should not? Sorry, but I should have mentioned that the code is supposed to do what this does: ($sentence) = $sentence =~ /(\d\d)/; (e.g. find the first 2 digits in the string $sentence) It worked fine in Perl 5.6.1 but doesn't work for me in 5.8.0 |
Re: dumb newbie question (or newbie dumb question)
Jerry C. wrote:
> Jay Tilton wrote: >> "Jerry C." wrote: >>> >>> $sentence = "January 23, 1992"; >>> $sentence =~ s/[^\d]+//g; >>> $sentence = substr($sentence,0,2); >>> $sentence =~ s/\b0//; >>> print $sentence; >> >> "It no longer works" does nothing to describe the problem. >> >> What should the code do that it does not? >> What does the code do that it should not? > > Sorry, but I should have mentioned that the code is supposed to do > what this does: > ($sentence) = $sentence =~ /(\d\d)/; > (e.g. find the first 2 digits in the string $sentence) > It worked fine in Perl 5.6.1 but doesn't work for me in 5.8.0 For me, the code prints '23' (in 5.8.0), which I suppose it's supposed to do. You are still failing to explain the nature of the problem. - What _does_ the code do when you run it? - Which error and/or warning messages do you receive? -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl |
Re: dumb newbie question (or newbie dumb question)
[This followup was posted to comp.lang.perl.misc]
In article <3FBF82DD.9F5E448E@cordollisonline.com>, jerry_c48 @cordollisonline.com says... > Hi, I have the following code which finds and prints the first 2 digits > in a string. I'm trying to make it more efficient. Here's the code: > > $sentence = "January 23, 1992"; > if ($sentence =~ /\d\d/) { $sentence = "$&"; } > print $sentence; > > If I know that "$sentence" will ALWAYS have a two digit date, is there a > way I can get rid of the "if - then" statement and just have it print > $sentence??? I tried this, and it doesn't work: > > $sentence = "January 23, 1992"; > $sentence = /\d\d/; > print $sentence; > > I'm just trying to make my script slightly more efficient by getting rid > of that "if - then" statement. > > Thanks, > -Jerry > > p.s. I really appreciate your help! If you could answer this question, > too, it would be greatly appreciated: In Perl Version 5.6.1, this code > worked, but in version 5.8.0, it no longer works. Any idea why? > > $sentence = "January 23, 1992"; > $sentence =~ s/[^\d]+//g; > $sentence = substr($sentence,0,2); > $sentence =~ s/\b0//; > print $sentence; use English; $sentence =~ m/\d\d/; print $MATCH; |
Re: dumb newbie question (or newbie dumb question)
>>>>> "MWS" == Master Web Surfer <raisin@delete-this-trash.mts.net> writes:
>> $sentence =~ s/[^\d]+//g; >> $sentence = substr($sentence,0,2); >> $sentence =~ s/\b0//; >> print $sentence; MWS> use English; MWS> $sentence =~ m/\d\d/; MWS> print $MATCH; oh crap! no one uses English for two reasons. it is dumb and it slows down all your s/// operations since it refers to $& and brethren. there is no need to ever use $& when you can just explicitly grab the match you want. uri -- Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com --Perl Consulting, Stem Development, Systems Architecture, Design and Coding- Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org |
Re: dumb newbie question (or newbie dumb question)
Uri Guttman <uri@stemsystems.com> wrote:
>>>>>> "MWS" == Master Web Surfer <raisin@delete-this-trash.mts.net> writes: > MWS> use English; > > MWS> $sentence =~ m/\d\d/; > MWS> print $MATCH; > > oh crap! no one uses English for two reasons. it is dumb and it slows > down all your s/// operations since it refers to $& and brethren. there > is no need to ever use $& when you can just explicitly grab the match > you want. I already told him that the last time he gave the same "answer"... -- Tad McClellan SGML consulting tadmc@augustmail.com Perl programming Fort Worth, Texas |
Re: dumb newbie question (or newbie dumb question)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 Uri Guttman <uri@stemsystems.com> wrote in news:x7wu9s3zzz.fsf@mail.sysarch.com: > oh crap! no one uses English for two reasons. it is dumb and it slows > down all your s/// operations since it refers to $& and brethren. > there is no need to ever use $& when you can just explicitly grab the > match you want. $& isn't as slow as it used to be. I do agree that "use English" is dumb, though. :-) - -- Eric $_ = reverse sort $ /. r , qw p ekca lre uJ reh ts p , map $ _. $ " , qw e p h tona e and print -----BEGIN PGP SIGNATURE----- Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com> iQA/AwUBP8AcOGPeouIeTNHoEQIEIACg5777diYK2IcbEAlVM+00wy fC2WYAmwRc bAPc0z+B2ig+xTx7P1saFZPx =V6AL -----END PGP SIGNATURE----- |
Re: dumb newbie question (or newbie dumb question)
>>>>> "TM" == Tad McClellan <tadmc@augustmail.com> writes:
TM> Uri Guttman <uri@stemsystems.com> wrote: >>>>>>> "MWS" == Master Web Surfer <raisin@delete-this-trash.mts.net> writes: MWS> use English; >> MWS> $sentence =~ m/\d\d/; MWS> print $MATCH; >> >> oh crap! no one uses English for two reasons. it is dumb and it slows >> down all your s/// operations since it refers to $& and brethren. there >> is no need to ever use $& when you can just explicitly grab the match >> you want. TM> I already told him that the last time he gave the same "answer"... well then, he is no master perl hacker and i doubt master of much else either. uri -- Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com --Perl Consulting, Stem Development, Systems Architecture, Design and Coding- Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org |
| All times are GMT. The time now is 10:03 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.