raveguy2k has asked for the wisdom of the Perl Monks concerning the following question:

i'm switching between an array and hash in my program and whenever I delete a key in a hash, It switches my numbers all around, I think this is because when I output everything in the array, it outputs like this:

name
number
blank line

so when I switch from an array back to a hash, I think it couples the blank line with whatever is underneath it, so I need to find a way to remove the blank line from my @array and/or hash. Any ideas?

Replies are listed 'Best First'.
Re: Removing extra blank lines
by Joost (Canon) on Jun 04, 2002 at 15:27 UTC
    Please show some code, because it is quite hard to understand what your problem is this way :-)

    Anyway, as I'm going to attack this problem blind, remember this:

    • Hashes are unordered
      So you cannot rely on any ordering of key => value sets in a hash - except that the ordering stays the same provided the hash does not change
    • Hashes are unordered pairs
      This means that you cannot delete a KEY in a hash without deleting it's VALUE. It also means that you get in trouble when converting an array with an uneven number of fields into a hash.
    • Hash keys are unordered pairs with unique keys
      You cannot have two keys that are the same in a hash.
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      except that the ordering stays the same provided the hash does not change
      I wouldn't even rely on this. The current implementation does it this way, but there's no telling what could happen in the future. Hashes have no order whatsoever, and you shouldn't rely on the any side effects to get an order.

      I could see perl going to some type of lazy, hash rebalancing structure. In this model, when cpu usage is low, it reoptimizes the hash. If this were the case, then the order might change, even though you do alter any elements in the hash.

        It might change - but I suspect this will not happen before Perl6 arrives. The docs (or the camel book, I forget) explicitly say that the ONLY thing you can count on as far as hash-ordering goes, is that the ordering of keys, values and each on a hash will be identical. - The changes you suggest will break that.
        -- Joost downtime n. The period during which a system is error-free and immune from user input.