#!/usr/bin/perl use strict; use warnings 'all'; use Data::Dumper; # get ids my $text = '1 << 3'."\n". '5 << 2'."\n". '3 << 2'."\n". '9 << 5'."\n". '8 << 2'."\n". '7 << 5'."\n". '2 << 5'."\n". '4 << 4'; # sort the ids my @sorted = sort { $a->[1] <=> $b->[1] } map { [split(/\s*<<\s*/, $_)] } split("\n", $text); # initialize the data hash, the hash for array checking # and our actual position my($hash, $last) = ({}, {}); my $actpos = 0; # iterate through the sorted ids foreach (@sorted) { # get cid and sharednetworkid my($cid, $snid) = (@{$_}); # set start and content my($start, $content, $end) = ($actpos, 'x'x($actpos+$cid)); # check whether we have an array if (defined($last->{$snid})) { $start = $hash->{$last->{$snid}}->{END} } # set end $end = length($content) + $start; # increment actual position $actpos = $end + 1; # create hash items $hash->{$cid} = {START => $start, CONTENT => $content, END => $end}; # set last sharednetworkid $last->{$snid} = $cid; } # output $Data::Dumper::Sortkeys = 1; print Dumper($hash);