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

Problem: Use of null value:
Problem2: Use of uninitialized value:
Background: Earlier I have joined $number, $email, $address1 and $address2 to $dbm{$name}. I'm trying to now pull back all entries in the database by $name. Ie, I want it to show results like:
Aaron Anderson
111-1111
200 Rocky Lane
Bedrock

Is there a syntax error with my script? I think the problem might be my 'my'ing of them cause maybe doing that removes their contents? I'm not sure but that's the best guess I have.

Thanks for your help everyone.

elsif ($option eq 'r') { my ($number, $email, $address1, $address2) = split "\0", $dbm{$na +me}; foreach (sort keys(%dbm)) { print "$dbm{$_}\n";
} }

"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

Replies are listed 'Best First'.
Re: Splitting a hash
by dws (Chancellor) on Mar 11, 2003 at 04:48 UTC
    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.

      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.

      ++++ 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.

        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

Re: Splitting a hash
by pg (Canon) on Mar 11, 2003 at 05:23 UTC
    Now you got your answer from dws and tall_man.

    Also, I would like to recommend that, in the future, try to use one variable for each piece of data, each piece of information, avoid using this kind of concatnated combo ;-) string.

      I needed to keep all data for each person separately and I had no other ideas on how to do that other than using join and split when the time comes. The only other suggestion that was given to me was to use MLDBM but I really didn't like having to install a module just for a small script like this..then learn how to use it. If there is an easier way without modules to do what I accomplished I'd be interested in hearing it but for now I'd like to stick with modules/dbm's that came with my version of perl.

      "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