in reply to question regarding slices

There is no promise that dot and dot-dot are always the first two entries returned, although it is rare to see them misordered (usually from a disk fsck repair).

The best solution is to leave your check in your loop as you already have. Stop optimizing for things that will break eventually.

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

Replies are listed 'Best First'.
Re: •Re: question regarding slices
by ptkdb (Monk) on Nov 07, 2003 at 13:19 UTC
    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
      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.

Re: •Re: question regarding slices
by Grygonos (Chaplain) on Nov 06, 2003 at 20:11 UTC

    thanks merlyn.. had no clue that they wouldn't always be the first two.. I'll leave as is

    edit:Here's the final version for anyone interested. I'm printing the items using TextPad because I like its formatting. I realize its proprietary but I just wrote it for personal use. I could have use the win 32 print shell command but its formatting was ugly so i decided to use textpad. It could easily adapted to provide max compatibility