dumb newbie question (or newbie dumb question)

Discussion in 'Perl Misc' started by Jerry C., Nov 22, 2003.

  1. Jerry C.

    Jerry C. Guest

    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;
     
    Jerry C., Nov 22, 2003
    #1
    1. Advertising

  2. Jerry C.

    Jay Tilton Guest

    "Jerry C." <> 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?
     
    Jay Tilton, Nov 22, 2003
    #2
    1. Advertising

  3. Jerry C.

    Jerry C. Guest

    > ($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
     
    Jerry C., Nov 22, 2003
    #3
  4. 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
     
    Gunnar Hjalmarsson, Nov 22, 2003
    #4
  5. [This followup was posted to comp.lang.perl.misc]

    In article <>, 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;
     
    Master Web Surfer, Nov 22, 2003
    #5
  6. Jerry C.

    Uri Guttman Guest

    >>>>> "MWS" == Master Web Surfer <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Nov 22, 2003
    #6
  7. Uri Guttman <> wrote:
    >>>>>> "MWS" == Master Web Surfer <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 22, 2003
    #7
  8. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Uri Guttman <> wrote in
    news::

    > 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-----
     
    Eric J. Roode, Nov 23, 2003
    #8
  9. Jerry C.

    Uri Guttman Guest

    >>>>> "TM" == Tad McClellan <> writes:

    TM> Uri Guttman <> wrote:
    >>>>>>> "MWS" == Master Web Surfer <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Nov 23, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    Dumb newbie question

    John, Mar 8, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    361
    Kevin Spencer
    Mar 8, 2006
  2. Mike Pence
    Replies:
    2
    Views:
    104
    Edward Faulkner
    Sep 28, 2005
  3. Steckly, Ron
    Replies:
    2
    Views:
    91
    Tim Hunter
    Dec 8, 2007
  4. Balance

    Dumb Newbie Question

    Balance, Aug 27, 2003, in forum: Javascript
    Replies:
    6
    Views:
    140
    spaminator
    Aug 29, 2003
  5. Marc Wilson

    Dumb newbie question- object references

    Marc Wilson, Nov 25, 2003, in forum: Javascript
    Replies:
    4
    Views:
    138
    Marc Wilson
    Nov 26, 2003
Loading...

Share This Page