Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

how to initialize array to null values

by sharan (Acolyte)
on Mar 23, 2009 at 13:22 UTC ( #752580=perlquestion: print w/ replies, xml ) Need Help??
sharan has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Hi all, I am trying to implement a co-relation problem in perl. I have two arrays i.e. array1 and array2 which contain following values.

array1 array2 read book eat apple book novel apple banana play football tennis football novel mazagine
I want my output as:
Hi all, I am trying to implement a co-relation problem in perl. I have two ar +rays i.e. array1 and array2 which contain following values. <c> array1 array2 array3 array4 read book novel magazine eat apple banana play football tennis
i tried implementing it with simple for loops as:
#Step no.1 for($i=0;$i<$arraylength;$i++) { for($j=0;$j<$arraylength;$j++) { if($array2[$i]eq $array1[$j]) { $array3[$i] = $array2[$j]; } } } #Step no.2 for($i=0;$i<$arraylength;$i++) { for($j=$i+1;$j<$arraylength;$j++) { if($array2[$i]eq $array2[$j]) { $array3[$i] = $array1[$j]; } } } #Step no.3 for($i=0;$i<$arraylength;$i++) { for($j=0;$j<$arraylength;$j++) { if($array2[$i]eq $array1[$j]) { $array4[$i] = $array3[$j]; } } }
But its not showing the proper output. It shows some random output in array3 and array4.

I guess there is some problem in the step no.2. It shows error stating that 'array3' cannot be null.

How can we initialize array to null values??

my @array3=(); #is it correct??

When the second step executes.. it compares the two same value but the value stored in array3 from array1 is not a string infact something like..HASH(0x82e3594), HASH(0x82e35a0). What could be the problem. Please guide me. Thanking you,

Comment on how to initialize array to null values
Select or Download Code
Re: how to initialize array to null values
by ikegami (Pope) 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 sundialsvc4 (Monsignor) 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 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.

Re: how to initialize array to null values
by GrandFather (Cardinal) 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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2014-08-27 21:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls