in reply to Quantum::Superpositions prob

tommycahir,
I think you were just banking on something that Quantum::Superpositions doesn't promise. The eigenstates function returns a list of states in the superposition, but does not say in order. I peeked under the hood and discovered that it generates a unique list by using a hash and we all know that hash order is not array order. There are ways to still use the module and maintain order - but it requires more work.

Hope this helps - L~R

Replies are listed 'Best First'.
Re: Re: Quantum::Superpositions prob
by tommycahir (Acolyte) on Mar 25, 2004 at 15:10 UTC
    how would i go about this, is there much work involved.
    What i really am aiming to do is
    1.to have an array of routers visited and an array of routers to_visit,
    2.then then remove the routers visited from the to_visit array
    3.continue to visit each one of these routers in turn finding out what other routers they connected to and adding them to the to_visit array
    4.repeat from step 2 till no more left in the to_visit array.
      tommychahir,
      Of course this can be done without Quantum::Superpositions, but since this sounded like fun:
      #! /usr/bin/perl use strict; use warnings; use Quantum::Superpositions; my @array_1 = (1 .. 4); my @array_2 = qw{1 2 2 3 3 3 4 4 4 4 5 6 7}; my @array_3 = eigenstates( any(@array_2) != all(@array_1) ); my @array_4 = Quantum::Superpositions::eigenstates( any(@array_2) != a +ll(@array_1) ); print "Ordered:\n"; print "$_\n" for @array_3; print "Hash:\n"; print "$_\n" for @array_4; { no warnings 'redefine'; *eigenstates = *main::eigenstates; sub eigenstates ($) { my @uniq; my %seen; for ( Quantum::Superpositions::collapse( $_[0] ) ) { push @uniq , $_ if ! $seen{$_}; $seen{$_}++; } return @uniq; } }
      Cheers - L~R
      tommycahir,
      Ok, since I have already provided a way to do it using Quantum::Superpositions, I should point out that there certainly are simpler solutions. Since your goal is just to visit all routers, order really should not matter:
      my @visited = (1 .. 5); my %unvisited = map { $_ => undef } 1 .. 9; while ( %unvisited ) { delete @{unvisited}{@visited}; for ( keys %unvisited ) { visit( $_, \%unvisited ); # Adds routers to %unvisited push @visited , $_; } }
      Now if for some reason order is important to you, you can reverse the array/hash for the stack.
      my %visited = map { $_ => undef } (1 .. 4); my @unvisited = (1 .. 9); while ( @unvisited ) { my $router = shift @unvisited; next if exists $visited{$router}; visit ( $router, \@unvisited ); # Adds routers to @unvisited $visited{$router} = undef; }
      I hope these solutions are helpful.

      Cheers - L~R

        L~R
        tanx for all the help i think i have it going now. order is important as i want to see what router is connected to what.
        the code i actually used was very similar to what u just given..
        once again your fountain of knowledge amazes me
        thanking you
        tommy