Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Ignore a range of numbers ina List

by AnomalousMonk (Archbishop)
on Jun 25, 2017 at 06:36 UTC ( [id://1193482]=note: print w/replies, xml ) Need Help??


in reply to Re: Ignore a range of numbers ina List
in thread Ignore a range of numbers ina List

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $aref = [ 6, 7, 1, 6, 8, 9, 7, 2, 6, 98, 99, 7, 3, 7, 4, 6, 5, ]; dd $aref; ;; my @copy; $_ == 6 .. $_ == 7 or push @copy, $_ for @$aref; ;; dd \@copy; " [6, 7, 1, 6, 8, 9, 7, 2, 6, 98, 99, 7, 3, 7, 4, 6, 5] [1, 2, 3, 7, 4]

What's supposed to happen if a "start" code for an exclusion sequence is seen, but no corresponding "stop" code is ever seen? My assumption was that no exclusion would happen. I expected
    [6, 7, 1, 6, 8, 9, 7, 2, 6, 98, 99, 7, 3, 7, 4, 6, 5]
to yield
    [1, 2, 3, 7, 4, 6, 5]


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Ignore a range of numbers ina List
by CountZero (Bishop) on Jun 25, 2017 at 08:09 UTC
    Building upon your very elegant solution, but changing it somewhat so it now adds to the valid output a sequence at the end that starts with a 6 but never gets ended by a 7.

    use Modern::PBP::Perl; my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7, 1, 2, 6, 9, 8, 6, 5]; my @copy; my @stack; (($_ == 6 .. $_ == 7) and push @stack, $_) or (push @copy, $_ and @sta +ck=()) for @$aref; push @copy, @stack; use Data::Dumper; print Dumper \@copy;

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      ... your ... solution ...

      Not mine (unfortunately), but tybalt89's here :)


      Give a man a fish:  <%-{-{-{-<

Re^3: Ignore a range of numbers ina List
by shmem (Chancellor) on Jun 25, 2017 at 07:19 UTC
    What's supposed to happen if a "start" code for an exclusion sequence is seen, but no corresponding "stop" code is ever seen?

    If the RHS of the flip-flop operator never gets true, then the flip-flop just stays as it is. Hence after 7,4 the 6 turns the flip-flop on, and 6,5 aren't pushed onto @copy.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      Hence after 7,4 the 6 turns the flip-flop on, and 6,5 aren't pushed onto @copy.

      I understand that, but what I was confused about was whether pr33 wanted the 6, 5 to appear in the output or not. pr33 seems to have answered this and related questions here. (At last! A quasi-test-based response :)


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-23 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found