How's ruby compare to it older brother python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carl Banks

    #76
    Re: Is Perl *that* good?

    imbosol@aerojoc key.com (Carl Banks) wrote in message news:<60dfb6f6. 0404281459.2167 5af4@posting.go ogle.com>...[color=blue]
    > In file x.pl:
    >
    > sub muck() {
    > $_ = "sucker\n";
    > }
    >
    > while(<>) {
    > if (/^abc/) {
    > muck();
    > print $_;
    > }
    > }
    >
    > Command line:
    >
    > echo abcdefg | perl x.pl
    >
    > Output:
    >
    > sucker
    >
    >
    > I had actually thought it restored $_.
    > It never ceases to amaze me how bad Perl is.[/color]

    Wait a minute, Perl does restore the regexp implicits.

    In file x.pl:

    sub muck() {
    $_ = sucker;
    /sucker/;
    }

    while(<>) {
    if (/^abc/) {
    muck();
    print $&;
    print "\n";
    }
    }

    Command line:

    echo abcdefg | perl x.pl

    Output:

    abc


    Perl has failed to amaze me at just how bad
    it is this one time. :)


    --
    CARL BANKS

    Comment

    • Carl Banks

      #77
      Re: Is Perl *that* good?

      Peter Otten wrote:[color=blue]
      > Wallclimber wrote:
      >[color=green]
      >> I have to agree with the original poster. My *only* complaint about
      >> Python are not regex search, but regex search and replace operations.
      >> Perl : s =~ s/(\w+)\s*=\s*(\d +)/$2 <= $1/g;
      >> Python : regex.sub(s, "(\w+)\s*=\s*)\ d+)", (lambda m: m.group(2)+" <=
      >> "+m.group(1 )))
      >>
      >> I didn't try this out so there might be some syntax problems in there,
      >> but you get the idea. Is there a 'nicer' way to do this in python?[/color]
      >
      > Your example suggests that the Python equivalent for $1 is \1:
      >[color=green][color=darkred]
      >>>> re.sub(r"(\w+)\ s*=\s*(\d+)", r"\2 <= \1", "a=1 b = 3 c = d")[/color][/color]
      > '1 <= a 3 <= b c = d'[/color]

      You can do that? LSNED.


      --
      CARL BANKS http://www.aerojockey.com/software
      "If you believe in yourself, drink your school, stay on drugs, and
      don't do milk, you can get work."
      -- Parody of Mr. T from a Robert Smigel Cartoon

      Comment

      • Martin Maney

        #78
        Re: Is Perl *that* good?

        Peter Hansen <peter@engcorp. com> wrote:[color=blue]
        > This is actually the first time in recent years that I can recall
        > that someone has complained about having to make up a new variable
        > (which is as good as free) to hold the result of a match. If[/color]

        That's an odd way of characterizing it, since this is one of the common
        forms of the FAQ more often discussed in terms of
        assignment-as-operator, which used to come up about every week or so
        back when I had the time to read more than an eclectic sampling of the
        traffic here. The throwaway wrapper class is probably the least bad
        solution - at least it can be used in thread-safe manner (1), unlike one
        simply awful hack that's in the cookbook.


        (1) use it as a wrapper that's instaniated locally and not shared,
        which is the closest analog of how the code would go if operator= were
        available, IMO&E. The bloody awful cookbook recipie is the one that
        gains a little more convenience at the cost of a single globally shared
        hidden variable.

        --
        Self-pity can make one weep, as can onions. -- Fodor

        Comment

        Working...