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

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: how to initialize array to null values
by ikegami (Patriarch) on Mar 23, 2009 at 13:28 UTC

    What's wrong with the solutions we gave when you last brought up this problem.

    How can we initialize array to null values??

    my @array3; will create an empty array.

    @array3 = (); will empty an existing array.

    infact something like..HASH(0x82e3594), HASH(0x82e35a0). What could be the problem

    The problem is that the data isn't what you say it is. You seem to have references to hashes rather than strings.

Re: how to initialize array to null values
by locked_user sundialsvc4 (Abbot) on Mar 23, 2009 at 13:37 UTC

    I suggest that you are trying to impose “C-like” thinking to this problem, when there might be a much cleaner way to do it...

    Fuhgeddabout storage allocation. Fuhgeddabout “fixed contiguous blocks of storage of some certain size.” Instead, imagine that what you call “a multi-dimensional array which will contain a lot of nulls” is simply an opaque storage-container (class) which accepts a list as the key which it uses to look for a particular value within itself.

    Such a storage-container can, conceptually, be built using a Perl hash. The list is converted to a key which is used to determine if a matching key in the hash. If so, the value is returned; if not, undef. Null values do not ever need to be stored, and if you know that the container will contain a preponderance of nulls (that is to say, the structure is sparse), you don't waste any storage on them.

    Why is this “important?” Why am I advocating using an “inefficient” storage-method like hashing? Answer: because memory is virtual. Large fixed-size arrays have a very large “footprint,” and that means they either acquire large working-sets (as seen by the virtual memory manager) or they start getting penalized buried-in-the-ground by page faults.

Re: how to initialize array to null values
by GrandFather (Saint) on Mar 24, 2009 at 03:33 UTC

    This looks very much like something where you should be using a hash rather than parallel arrays. Consider:

    use strict; use warnings; my %tasks = ( read => [qw(book novel magazine)], eat => [qw(apple banana)], play => [qw(football tennis)], ); for my $task (sort keys %tasks) { my @objects = @{$tasks{$task}}; print "I can $task a "; print join ', ', @objects[0 .. $#objects - 1]; print " and a $objects[-1]" if @objects > 1; print ".\n"; }

    Prints:

    I can eat a apple and a banana. I can play a football and a tennis. I can read a book, novel and a magazine.

    which shows a few language handling problems, but ought to demonstrate how such code hangs together.


    True laziness is hard work
Re: how to initialize array to null values
by ig (Vicar) on Mar 23, 2009 at 23:48 UTC
    What could be the problem. Please guide me.

    The problem is not in the code that you have posted - it is in the code that initializes @array1. If you want help to find the error in that code you should post it.