Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Convert undef to empty string in a hash

by ibm1620 (Monk)
on Jan 15, 2015 at 23:48 UTC ( #1113412=perlquestion: print w/replies, xml ) Need Help??

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

Is there a better (quicker) way to do the following:

$foo{$_} = $foo{$_} // '' for keys %foo;
In other words, to change all undef values in a hash to the empty string?

Replies are listed 'Best First'.
Re: Convert undef to empty string in a hash
by GrandFather (Saint) on Jan 16, 2015 at 00:03 UTC

    Quicker is really unlikely to be an issue. "Better" is much harder to define. You may consider one of:

    $_ //= '' for values %foo; $foo{$_} //= '' for keys %foo;

    "better". I'd tend toward the first because it's maybe a little more obvious that it's the values in the hash that you are processing.

    For the most part when thinking about "better", think about "is the intent of this code obvious?". Make the code clear first, then if you really need to, make it faster. Almost always you don't need to do the "make it faster" bit.

    If you do think you need to make it faster, first write unit tests then profile the code before changing anything. Unit tests to make sure you don't break the code while changing it. Profile so you don't waste your time "fixing" stuff that isn't broken.

    Perl is the programming world's equivalent of English
Re: Convert undef to empty string in a hash
by eyepopslikeamosquito (Bishop) on Jan 16, 2015 at 07:23 UTC

    $foo{$_} = $foo{$_} // '' for keys %foo;
    Both of GrandFather's solutions are "better" than yours because your solution violates DRY. That is, this class of coding error:
    $Foo{$_} = $foo{$_} // '' for keys %foo; # oops I meant $foo not $F +oo
    simply cannot occur with good ol' Gramps' solutions.

    Of GrandFather's two solutions, from the perspective of DRY, this one is better:

    $_ //= '' for values %foo;
    because the name foo is mentioned only once.

      our solution violates DRY...from the perspective of DRY, this one is better:

      Yeah, that isn't what DRY is about

        Yeah, that isn't what DRY is about
        Well, that is a matter of interpretation and you did not provide any citations to support your interpretation.

        From Don't Repeat Yourself (O'Reilly)

        Every line of code that goes into an application must be maintained, and is a potential source of future bugs. Duplication needlessly bloats the codebase, resulting in more opportunities for bugs
        I contend that:
        a_very_long_name = a_very_long_name + 42;
        is better written as:
        a_very_long_name += 42;
        because (the completely unnecessary) duplication of a_very_long_name is eliminated thus eliminating a "potential source of future bugs" and reducing "opportunities for bugs".

        Of course, DRY is much broader than that. Yet that doesn't mean my interpretation of it here is wrong. Indeed, the wikipedia DRY page gives a very broad interpretation:

        a principle of software development, aimed at reducing repetition of information of all kinds

        A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1113412]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2021-05-07 18:26 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (93 votes). Check out past polls.