in reply to Re: random pairs
in thread random pairs

Let's say I used your bit of code and edited it a bit to match my needs. Currently I have this:

my %test; my @predetermined; my @results; @predetermined = (0..$pop-1); for my $element (@predetermined) { my $pair; { $pair = int(rand($pop)); redo if $test{$element.'-'.$pair} or $test{$pair.'-'.$elem +ent} or $pair == $element; } $test{$element.','.$pair} = $test{$pair.','.$element} = 1; push @ {$pop{$element{interactions}}}, $pair; push @results, $element.'-'.$pair; foreach (@results) { ($element.','.$pair) = split; $element{$_} = $element; } } sort {$a <=> $b} @results; say for (@results);

For the line of code below,

push @ {$pop{$element{interactions}}}, $pair;

the terminal keeps returning this.

Use of uninitialized value $element{"interactions"} in hash element at disease_outbreak_array.pl line 87, <> line 1.

I have this hash that contains hashes of hashes that follow this format:

my %pop = ( 0 => {status => "0", interactions => []}, 1 => {status => "0", interactions => []},

I am attempting to store all the values that one person decides to interact with inside the "interactions" list within my hash of hashes, but it keeps giving me the uninitialized error. TIA

Replies are listed 'Best First'.
Re^3: random pairs
by CountZero (Bishop) on Jul 16, 2012 at 22:30 UTC
    Perl "warnings" is trying to tell you that $element{"interactions"} is uninitialized (undef). That probably means you "forgot" to give it a value previously. Check the program- and data-flow and see where you went wrong.

    Probably @{$pop{$element{interactions}}} is not doing what you expect. I think perhaps you mean something more like @{$pop{$element}{'interactions'}}

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      I keep getting errors about $element and $pairs being uninitialized. I was wondering what you had set for the value of those two elements for your loop. I want to store $pairs into an array by the value of $element but I can't.
        Difficult to say why you get these warnings.

        By now I have lost track of how your program looks and I cannot reproduce these warnings in my script.

        I guess the problem is with this code:

        push @results, $element.'-'.$pair; foreach (@results) { ($element.','.$pair) = split; $element{$_} = $element; } }
        You are splitting the elements of @results on whitespace and there is no whitespace in these elements, so split is not doing what you think it does.

        Also, ($element.','.$pair) looks really strange. What are you trying to do with it? I have never seen his on the left side of an assignment.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics