$VAR1 = \[ { 'desc' => 'xxx', 'ports' => [ { 'adminstate' => 'up', 'operationstate' => 'down', 'port' => '80', 'ismaster' => 'active' }, { 'adminstate' => 'up', 'operationstate' => 'down', 'port' => '81', 'ismaster' => 'active' }, { 'adminstate' => 'up', 'operationstate' => 'down', 'port' => '82', 'ismaster' => 'active' } ], 'adminstate' => 'up', 'lagid' => '45', 'operationstate' => 'down' }, ${$VAR1}->[0], ${$VAR1}->[0], ${$VAR1}->[0], ${$VAR1}->[0], #### foreach my $line ( split( /\n/, $c ) ) { if ( $line =~ m/someregex/ ) { if ( exists( $lag_detail{"ports"} ) ) { push( @{ $self->{"lags"} }, \%lag_detail ); } %lag_detail = ( "lagid" => $lag, "lagdesc" => $4, "operationstate" => $3, "adminstate" => $2 ); } } if ( $line =~ m/somemoreregex/ ) { my %port_detail = ( "port" => $1, "adminstate" => $2, "operationstate" => $4, "ismaster" => $3 ); push @{ $lag_detail{"ports"} }, \%port_detail; } }