in reply to Splitting a hash

Assuming that you did indeed join the fields with a null bytes as the separator,
foreach my $key ( sort keys %dbm ) { my($number, $email, $addr1, $addr2) = split "\0", $dbm{$key}; print "$number\n$email\n$addr1\n$addr2\n"; }
should do the trick.

In your snippet, you're doing the split once, but are then ignoring the result.

Replies are listed 'Best First'.
Re: Splitting a hash
by tadman (Prior) on Mar 11, 2003 at 09:43 UTC
    Or, in the interest of not creating variables you don't use:
    foreach my $key (sort keys %dbm) { print "$_\n" foreach (split("\0", $dbm{key})); }
    The advantage to this is that if another variable is added, at least you won't have to update this code to handle it.
      Or (usually) preferrably
      foreach my $key (sort keys %dbm) { print map "$_\n", split /\0/, $dbm{$key}; }
      (You have a typo: $dbm{key} is missing a sigil.)

      Makeshifts last the longest.

        Good catch on the error, by the way. Still, in this case I'm not sure that map is the best approach. You end up creating a list with split and then reprocessing it with map and then finally printing it. The foreach variation actually omits a step, which I would think makes it just slightly better.

        That is not to say you couldn't do something like this:
        foreach my $key (sort keys %dbm) { print join("\n", split(/\0/, $dbm{$key})), "\n"; }
        This is probably slower because you create a huge scalar in the process, but still, it should work.
Re: Re: Splitting a hash
by sulfericacid (Deacon) on Mar 11, 2003 at 04:57 UTC
    ++++ for you!!! Thanks so much, that definatley did the trick. Why was my snippet ignoring the data?

    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
      Why was my snippet ignoring the data?

      The data that you wanted (the separated fields) were in the variables you split them to. You ignored them, perhaps expecting the values within the hash itself to become split. But the original values remained as they were, because split returns a new array of values. It doesn't affect the original.

        Thanks for your help. I was actually expecting the variables to be defined and used when used in a split. That does make sense now that I think about it. I made new variables ($address1, $address2...) but I only asked to print the hash $dbm{$_} leaving the only part I was looking for in the dust.

        Again, thanks for your help, I really appreciate it.

        "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

        sulfericacid

      Why was my snippet ignoring the data?

      Well let's examine your snippet:
      my ($number, $email, $address1, $address2) = split "\0", $dbm{$name}; foreach (sort keys(%dbm)) { print "$dbm{$_}\n"; }

      Your first line does a split on null characters of the hash record with a key of $name, the result is returned to several variables which aren't used again in the snippet. Then you loop around the hash, by key, and print the values.

      dws's example put the split inside the loop and then prints out the values split returned.

      --
      Life is a tale told by an idiot -- full of sound and fury, signifying nothing. William Shakespeare, Macbeth