in reply to •Re: question regarding slices
in thread question regarding slices

How to do it with not just 1 slice but 2 slices, and at the same time avoid Merlyn's issue of . and .. not always coming at the start of the list
@l = readdir(PRINT) ; %h{@l} = (1) x @l ; delete @h{'.', '..'} ; for( keys %h ) { # do work }
This is probably not what was desired in the first place, since it adds more lines of code, but I thought this would be interesting.

Nuts and Bolts

  • @l = readdir gets the list of files
  • %h{@l} = (1) x @l converts the list into a hash, since all elements in a directory are unique
  • delete @h{'.', '..'} removes the . and .. entries from the hash at whatever position they're hiding in.
  • for( keys %h) { . and .. free looping
  • Replies are listed 'Best First'.
    •Re: Re: •Re: question regarding slices
    by merlyn (Sage) on Nov 07, 2003 at 13:47 UTC
      Besides being very likely both longer to type and longer to run, you've also made it less portable, because delete @hash{@list} wasn't added until perl 5.6, if I recall. It might have been 5.5, but it's still a "relatively recent invention" as I often say.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.