#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use Clone qw(clone); my $ranges = { 1282 => [25, 40, 5, undef], 1283 => [300, 400, 25, 1282], 1284 => [20, 30, 5, undef], 1285 => [250, 260, 10, 1284], 1286 => [25, 30, 5, undef], 1287 => ["0.003", "0.005", "0.001", 1286]}; my $ranges_clone = clone($ranges); for my $range (keys %$ranges) { my $related_r = @{ $ranges->{$range} }[3]; if ( defined $related_r ) { my @merge_range; @merge_range = ( @{$ranges->{$range}}[0..2], @{$ranges->{$related_r}}[0..2] ); delete $ranges_clone->{$related_r}; $ranges_clone->{$range} = \@merge_range; } } print "EDITED RANGES:\n"; print Dumper $ranges_clone;