Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^5: Converting python list range expressions to perl

by kcott (Archbishop)
on Dec 05, 2022 at 06:14 UTC ( [id://11148562]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Converting python list range expressions to perl
in thread Converting python list range expressions to perl

OFFSET:LENGTH vs. OFFSET:OFFSET may be purely semantics. The relationship that I showed in the table is exactly the way I coded it in my script. Can you come up with any new tests which aren't successful?

— Ken

Replies are listed 'Best First'.
Re^6: Converting python list range expressions to perl
by LanX (Saint) on Dec 05, 2022 at 14:23 UTC
    > OFFSET:LENGTH vs. OFFSET:OFFSET may be purely semantics.

    doesn't seem so.

    https://docs.python.org/3/glossary.html#term-slice

    slice An object usually containing a portion of a sequence. A slice is creat +ed using the subscript notation, [] with colons between numbers when several are given, such as in variable_nam +e[1:3:5]. The bracket (subscript) notation uses slice objects internally.

    https://docs.python.org/3/library/functions.html#slice

    class slice(stop) class slice(start, stop, step=1) Return a slice object representing the set of indices specified by ran +ge(start, stop, step). The start and step arguments default to None. ...

    something is off (-set ;) with this test suite. Unfortunately I have no Python at hand to test ...

    updates

    references and citations updated

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

      In the OP, a notation that looked like [x:y], where x or y could be blank, was given. I noted this equivalence:

      PythonPerl
      [x:y]splice ARRAY, x, y
      [:y]splice ARRAY, 0, y
      [x:]splice ARRAY, x
      [:]splice ARRAY, 0

      I coded that and it passed all tests. I took all given tests, and their results, on face value. One exception is [:], which I added myself and was extrapolated from information already provided.

      If the original data was wrong in some way, then I'm working from a false premise. If the tests are insufficient, further tests could show that the seen equivalence doesn't hold for other values.

      I'm not trying to sell something here. I don't care what the OP uses for his "poetry" project — as already stated: "What you choose is entirely up to you.".

      If you think that there's something wrong with the source, please take it up with the OP, not me.

      — Ken

        > please take it up with the OP, not me.

        I'm not "taking it up with you".

        This is a public thread, other readers may get interested to dig deeper.

        edit

        I was just writing up my findings literally "for the record", before they get lost.

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

          A reply falls below the community's threshold of quality. You may see it by logging in.
Re^6: Converting python list range expressions to perl
by ibm1620 (Hermit) on Dec 05, 2022 at 21:36 UTC
    Here's one:

    [\@test_array, '[3:4]', 'd'],
    In my OP I mentioned needing help from "bilingual" (i.e. Perl and Python) people; otherwise I'd have included a description of Python's slice notation. I'm sorry if this was negligence on my part, but I assumed that anyone answering would be familiar with Python's oddball slice semantics. [a:b] means "the characters starting at offset a and ending at offset b-1". Unless a or b is negative in which case .. hell, I don't remember ;-)

    The code I posted correctly slices arrays using Pythonic parameters, although it doesn't attempt to handle the optional third argument, a step value. And I'm happy to say that the translation effort has succeeded and Perl is now churning out gibberish sonnets at a much faster rate than its Python equivalent.

    Update:

    It correctly slices arrays if the arguments aren't nonsensical -- like being out of range, which Python forgives. It needs a few more sanity tests to handle those cases.

      > Unless a or b is negative in which case .. hell, I don't remember ;-)

      AFAIR from a previous discussion is Perl using closed intervals while Python using half open ones.

      That means the end point is excluded in Python.

      One advantage is that [:X] and [X:] are splitting an interval including X exactly into two dijoint parts, without needing to decrement the first end boundary.

      I.O.W always $end-- when translating to Perl.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

        Personally I prefer offset/length-type expressions, as in substr(), to begin/end expressions of Perl slices and Python ranges, especially when it comes to the way negative values work. I'm sure it's just a matter of familiarity, though.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11148562]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found