my @points; # This will add one point (the first one in your arrays # above) to another type of data structure. my %this_point = {x => 0, y => 0, z => 25}; push @points, \%this_point; # Do this for as many points as you have. You can # adapt the method you use to fill your three arrays # to create this kind of data structure using the # example above. #### # Sort by z first, then sort by x for each point @points = sort { $a->{x} <=> $b->{x} } sort { $a->{z} <=> $b->{z} } @points;