#! perl -slw use strict; use Data::Dump qw[ pp ]; $Data::Dump::WIDTH = 200; use List::Util qw[ min ]; my %pos; while( ) { my( $c, $x1, $y1, $x2, $y2 ) = split ' '; $pos{ int( $y1 / 12 ) }{ int( $x1 / 10.5 ) } = $c; } my $firstY = min( keys %pos ); my @firstXs = sort{ $a <=> $b } keys %{ $pos{ $firstY } }; for my $y ( grep $_ != $firstY, keys %pos ) { $pos{ $y }{ $_ } //= ' ' for @firstXs; } pp \%pos; __DATA__ ... from the OP