G'day ahjohnson2,

The following achieves the results specifed in your OP and in your subsequent update.

Note the different data structure I've used. The order of continents is important so I've used an array of hashrefs each with a single continent key. The order of cities is also important so the values of those keys are arrayrefs.

[Also note that you've misspelled "Cairo" two different ways — this will cause errors that may be hard to track down — taking care over such details is important.]

#!/usr/bin/env perl -l use strict; use warnings; use Data::Dump; my @parent = ( { Europe => [ qw{ London Moscow } ] }, { Asia => [ qw{ Tokyo Seoul } ] }, { Africa => [ qw{ Cairo Algiers } ] }, ); my @current; reset_current(\@parent, \@current); print '*** Starting State ***'; print_current_state(\@current); print '*** Asia Down ***'; failover(Asia => \@current); print_current_state(\@current); print '*** Reset State ***'; reset_current(\@parent, \@current); print_current_state(\@current); print '*** Europe Down ***'; failover(Europe => \@current); print_current_state(\@current); print '*** Asia Down ***'; failover(Asia => \@current); print_current_state(\@current); sub failover { my ($down, $current) = @_; my @cities_to_failover = map { (keys %$_)[0] eq $down ? @{$_->{$down}} : () } @$curre +nt; for (@$current) { my ($continent) = keys %$_; next unless @{$_->{$continent}}; if ($continent eq $down) { @{$_->{$continent}} = (); } else { push @{$_->{$continent}}, shift @cities_to_failover; } } } sub print_current_state { my ($current) = @_; dd $current; } sub reset_current { my ($parent, $current) = @_; @$current = (); for (@$parent) { my ($continent) = keys %$_; push @$current, { $continent => [ @{$_->{$continent}} ] }; } }

Output:

*** Starting State *** [ { Europe => ["London", "Moscow"] }, { Asia => ["Tokyo", "Seoul"] }, { Africa => ["Cairo", "Algiers"] }, ] *** Asia Down *** [ { Europe => ["London", "Moscow", "Tokyo"] }, { Asia => [] }, { Africa => ["Cairo", "Algiers", "Seoul"] }, ] *** Reset State *** [ { Europe => ["London", "Moscow"] }, { Asia => ["Tokyo", "Seoul"] }, { Africa => ["Cairo", "Algiers"] }, ] *** Europe Down *** [ { Europe => [] }, { Asia => ["Tokyo", "Seoul", "London"] }, { Africa => ["Cairo", "Algiers", "Moscow"] }, ] *** Asia Down *** [ { Europe => [] }, { Asia => [] }, { Africa => ["Cairo", "Algiers", "Moscow", "Tokyo"] }, ]

-- Ken


In reply to Re: Scripting Failover Scenario by kcott
in thread Scripting Failover Scenario by ahjohnson2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.