[Pdx-pm] Different data structures for Hash of array, same code (mostly)

Daniel Herrington dherrington at robertmarktech.com
Wed Nov 18 12:39:49 PST 2009


Hans, that deref was the problem, thanks. Verbosity tends to be a 
holdover from when I was less secure in my Pearl coding and wanted to 
know exactly what I did. I refactor towards verbosity when I run into 
problems, which in this case didn't actually help solve anything. I was 
actually using the following when i went to test and noticed the issue:

push (@{$newBoxNameH{$newBoxName}}, $newJobName);

This replaced that whole code snippet in B.

Shlomi, thanks for the tips. The Code in snippet A was from over a year 
ago, before I started getting serious with Pearl and learned about how 
to handle complex data structure and dereferencing. I know I need to go 
back and refactor the sub that A came from, and one day as God is my 
witness I will ;) As always, time is the issue.

The variable names aren't meant to be Hungarian, just simply how I 
started with Pearl and identifying an array from a hash, etc. Now it's 
just laziness as those names pop into my head when thinking of new 
variables. I do like my variables to be somewhat related to the data 
they contain, but it's an evolving standard in my head.

Again, thanks all for the help and sorry for the double post.

Dan H.

Hans Dieter Pearcey wrote:
> Excerpts from Daniel Herrington's message of Wed Nov 18 10:12:07 -0500 2009:
>>        my @arJobNamesB = $ref_arJobNamesB;
> I bet you meant '@$ref_arJobNamesB'.
> Your code would probably be easier to skim for this kind of thing if it were
> less verbose.  For example:
>>            if ($newBoxNameH{$newBoxName}) {
>>                my $ref_arJobNames = $newBoxNameH{$newBoxName};
>>                my @arJobNames = @$ref_arJobNames;
>>                push (@arJobNames,$newJobName);
>>                $newBoxNameH{$newBoxName} = \@arJobNames;
>>            } else {
>>                my @arJobNames;
>>                push (@arJobNames,$newJobName);
>>                $newBoxNameH{$newBoxName} = \@arJobNames;
>>            }
>   my @foo;
>   push @foo, $x;
>   $thing = \@foo;
> can be much more easily written as
>   $thing = [ $x ];
> In fact, the whole conditional I quoted above could be one line.
>   push @{ $newBoxNameH{$newBoxName} }, $newJobName;
> Perl will automatically turn the nonexistent hash element into an array
> reference for you.  If this is too magical for your taste, default it to an
> empty arrayref first:
>   $newBoxNameH{$newBoxName} ||= [];
> hdp.
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/pdx-pm-list/attachments/20091118/8751b362/attachment.html>

More information about the Pdx-pm-list mailing list