Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Inverse slices

by johngg (Canon)
on Nov 01, 2024 at 11:59 UTC ( [id://11162557]=note: print w/replies, xml ) Need Help??


in reply to Inverse slices

This uses a hash for the index rather than an array and push with a ternary.

johngg@aleatico:~$ perl -Mstrict -Mwarnings -MData::Dumper -E 'say q{} +; my @arr = ( q{a} .. q{m} ); my %idx = map { $_ => 1 } 2, 7 .. 9, 12; my @slc; my @inv; push @{ $idx{ $_ } ? \ @slc : \ @inv }, $arr[ $_ ] for 0 .. $#arr; print Data::Dumper->Dumpxs( [ \ @arr, \ @slc, \ @inv ], [ qw{ *arr *slc *inv } ] );' @arr = ( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm' ); @slc = ( 'c', 'h', 'i', 'j', 'm' ); @inv = ( 'a', 'b', 'd', 'e', 'f', 'g', 'k', 'l' );

I hope this is of interest.

Update: Removed erroneous space in Dumpxs call which was mucking up the output.

Cheers,

JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2025-11-10 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (66 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.