in reply to random pairs

Easy:
use Modern::Perl; my %test; my @predetermined; my @results; push @predetermined, int(rand(100)) for 1 .. 100; for my $element (@predetermined) { my $pair; { $pair = int(rand(100)); redo if $test{$element.'-'.$pair} or $test{$pair.'-'.$element} + or $pair == $element; } $test{$element.'-'.$pair} = $test{$pair.'-'.$element} = 1; push @results, $element.'-'.$pair; } say for sort(@results);
Update: Added test for both elements being equal.

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

Replies are listed 'Best First'.
Re^2: random pairs
by Anonymous Monk on Jul 16, 2012 at 20:00 UTC

    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

      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.