my ($c, $start, $end) = (2, 0, 0); foreach $elemetns(keys %hash) { $start = $end; $end += length $elemetns; ... my $r = $document->Range($start, $end); ... } #### ... my @elements = ( 'This is a test line', 'This is second test Line', 'This is the third line', ); ... my ($c, $start, $end) = (2, 0, 0); for my $element (@elements) { $start = $end; $end += length $element; ... my $r = $document->Range($start, $end); ... }