in reply to NEXT statement in for loop

I thought it will take first value of $pic{R0}, which is 5 but it is taking $pic{R0} second value.

To emphasize something that Corion pointed out in his answer: Hashes are unordered. There is no "first" or "second" value. If you need things in order, you should probably put them in an array, not a hash.

my %pic = ( R0 => [ 5, 4 ], R2 => [ 3, 2 ] );

Replies are listed 'Best First'.
Re^2: NEXT statement in for loop
by Anonymous Monk on Jun 06, 2016 at 05:53 UTC

    But, you can't define array in hash, right ?. I don't think this will work

    my %pic = ( R0 => [ 5, 4 ], R2 => [ 3, 2 ] );

        Yes, tried and worked

        Pasting the final code

        my @r = qw/h1 h2/ ; my $rh = JT->new( host => $r[0] ); my $rh = JT->new( host => $r[1] ); my $output; my $key; my $value; my @routers =($r[0], $r[1]); my @router = ( "R0", "R1", "R2" ); my %fpc = ( R0 => [5,4], R1 => [3,2], R2 => [1,0], ); my %pic = ( R0 => [5,4], R1 => [3,2], R2 => [1,0], ); ); @router = ( "R0", "R1", "R2" ); for my $rh (@routers) { my $r = shift(@router); print "value of r is $r\n"; my $count = 0; for my $f (@{ $fpc{$r} }) { # for my $p (@{ $pic{$r} }) { my $p = (@{ $pic{$r} })[$count]; print "request chassis pic offline fpc-slot $f pic-slot $p +\n"; $count++; #} } }

        . Thanks all