dumb newbie question (or newbie dumb question)

J

Jerry C.

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;
 
J

Jay Tilton

: 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?
 
J

Jerry C.

($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
 
G

Gunnar Hjalmarsson

Jerry said:
Jay said:
Jerry C. said:
$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?
 
M

Master Web Surfer

[This followup was posted to comp.lang.perl.misc]

jerry_c48 said:
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;
 
U

Uri Guttman

MWS" == Master Web Surfer said:
$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
 
T

Tad McClellan

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"...
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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+00wyfC2WYAmwRc
bAPc0z+B2ig+xTx7P1saFZPx
=V6AL
-----END PGP SIGNATURE-----
 
U

Uri Guttman

MWS> use English;MWS> $sentence =~ m/\d\d/;
MWS> print $MATCH;

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top