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

Hello Monks,

Is there a shorter method of doing the below? ($arrayKey is just a name list)

if(!$myarray[$arrayKey[$i]]) { $myarray[$arrayKey[$i]]=() }

Thank-you kindly, *bow*

Update:

1st reply asked what I think this does... *blush*

2nd reply leads me to believe that its redundant usage in Perl, so i'll just start running code and see if it fails without it

Overall, the above method is redundant. I'm use to defining everything before hand, but perl obviously allows me to freely create my arrays

Thank-you very much for the information and the support :) *bow*

Replies are listed 'Best First'.
Re: If Not Replace
by ikegami (Patriarch) on Sep 10, 2011 at 15:00 UTC
    Nothing ("()") in scalar context evaluates to undef, so
    if (!$myarray[$arrayKey[$i]]) { $myarray[$arrayKey[$i]]=() }
    is really
    if (!$myarray[$arrayKey[$i]]) { $myarray[$arrayKey[$i]] = undef; }

    If $myarray[$arrayKey[$i]] is non-existent, 0, the empty string, "0" or one of the few other things that are false, undef will be assigned to it.

    Unless you are trying to micro-optimise memory usage (undef takes less memory than the string "0"), it sounds like a rather useless thing to do.

    Maybe the author was trying to stretch the size of the array, but that's probably a useless thing to do too. As the statement demonstrates, using and assigning to non-existent elements is not a problem.

Re: If Not Replace
by derby (Abbot) on Sep 10, 2011 at 14:18 UTC

    You probably do not need this at all. You can rely on autovivification when setting the value and defined when accessing.

    -derby
      There’s no autoviv involved here. Autovivication is the implicit allocation of references of the proper type when undefined lvalues are dereferenced. There is no dereferencing here.

        Ah ... sorry I was wrongly thinking perl's auto extending of arrays is the same thing as autoviv.

        -derby
Re: If Not Replace
by Anonymous Monk on Sep 10, 2011 at 14:31 UTC

    Update: After being asked what I think this does... Its suppose to define(create) a table within the parent if it doesn't already exist. I can't assume that the table will be properly defined ...

    Are you using strict? Read this if you want to cut your development time in half!

    $ perl -e " use strict; use warnings; my %f; $f[0]=3; " Global symbol "@f" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

    You should not write  %myarray because that is a hash, and $myarray[0 ] is refering to @myarray, whereas $myarray{0} is refering to %myarray;

    perlintro

      You should not write %myarray because that is a hash, and $myarray0 is refering to @myarray, whereas $myarray{0} is refering to %myarray

      I was following examples from 'http://oreilly.com/catalog/advperl/excerpt/ch01.html' but I no doubt got them wrong.

      Thank-you muchly for guiding me re the above variable information. Associative arrays are what I believe i'm use to using in LUA, the hash naming just confuses me atm *bow*

        That's a good book, but having read it (and having re-read the excerpt you pointed to in the link above), I am not seeing quite how you came up with what you did. But that's not important. The important thing is that you've asked a few good questions, and have had thick enough skin to stick around for the answers. I ++ed both of your nodes in this thread for that reason, particularly after seeing the updates. So many times we see someone "fly by" with a question that seems like it's coming from left field, and then after we answer or request further clarification, never see the individual again. Kudos for sticking with it.

        I recommend putting the Advanced Perl Programming book off for now. It's a great book but it doesn't deal with advanced Perl syntax so much as advanced topics addressed by Perl. It does get into symbol table tricks to some degree, but for the most part it's about implementing solutions with Perl. Mastering Perl is in a similar category; another great book that sort of takes on random topics related to implementing real-world solutions with Perl.

        But Learning Perl and Intermediate Perl are books dedicated to Perl itself; Perl syntax, Perl data types, Perl objects, Perl modules, Perl operators, etc.


        Dave

Re: If Not Replace
by Anonymous Monk on Sep 10, 2011 at 14:07 UTC
    What do you think that code does?