Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Converting python list range expressions to perl

by kcott (Archbishop)
on Dec 04, 2022 at 18:45 UTC ( [id://11148553]=note: print w/replies, xml ) Need Help??


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

"Ken, thanks for your writeup, including forcing me to learn a bit about Test -- I needed that! :-)"

You're welcome. It's good to be able to start a script with use v5.36;.

"I'd rather stick with Perl's slice capability since it reads more cleanly (to me, anyway) ..."

What you choose is entirely up to you. This correlation stood out for me:

PythonPerl
[OFFSET:LENGTH]splice ARRAY, OFFSET, LENGTH
[:LENGTH]splice ARRAY, 0, LENGTH
[OFFSET:]splice ARRAY, OFFSET
[:]splice ARRAY, 0
"... and doesn't require copying and modifying the source array."

The source array, @test_array, is not modified at all. The temporary copy, @temp_array, is modified in the last statement of (my) get_array_slice_by_python_expr() function; it's then immediately discarded as it goes out of scope.

I added your three new tests, plus a fourth ([:]), to my original code:

[\@test_array, '[-3:-1]', 'ef'], [\@test_array, '[-3:-3]', ''], [\@test_array, '[3:-1]', 'def'], [\@test_array, '[:]', 'abcdefg'],

All pass:

1..8 ok 1 - Testing: [:3] ok 2 - Testing: [:-3] ok 3 - Testing: [3:] ok 4 - Testing: [-3:] ok 5 - Testing: [-3:-1] ok 6 - Testing: [-3:-3] ok 7 - Testing: [3:-1] ok 8 - Testing: [:]

— Ken

Replies are listed 'Best First'.
Re^4: Converting python list range expressions to perl
by ibm1620 (Hermit) on Dec 04, 2022 at 23:22 UTC
    I didn't mean that @test_array was modified; I meant that splice() modifies its argument array, requiring a temp copy be made. It just seemed wasteful to copy an array, and modify it, and then dispose of it, when Perl slice doesn't do so. Of course, that's pure guesswork on my part - and I haven't attempted to time it.

    In your representation of Python vs Perl slicing & splicing, I didn't understand your notation OFFSET:LENGTH for Python. Python slices are OFFSET:OFFSET. (Which is why Perl slices seemed a better fit to me.) Could you clarify?

      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

        > 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

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found