http://www.perlmonks.org?node_id=898040

citromatik has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I am storing some data in a long string of "ints":

open my $fh, "<", $file or die $!; while (<$fh>){ my ($key,$val) = split /\t/; substr($bin,$key*4,4,pack("N",$val)); }

Since the $keys in the file are not sorted, I need to initialize substr with enough values (to avoid a "substr outside of string" error). Currently I am doing it like follows:

substr($bin,$_*4,4,pack ("N",0)) for (0..$last_val);

Is there a more efficient way (maybe in one go instead of looping) of doing this?

Thanks in advance

citromatik