http://qs321.pair.com?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

Replies are listed 'Best First'.
Re: Fast string construction
by johngg (Canon) on Apr 07, 2011 at 13:59 UTC

    I wonder if you could use vec instead. I don't think you would need to initialise the string then as it would, I think, be automatically extended at need. I think it also stores bits in the same order as pack q{N}, ...

    knoppix@Microknoppix:~$ perl -E ' > vec( $str, 1, 32 ) = 0xfffff; > say unpack q{b*}, $str; > vec( $str, 3, 32 ) = 0xf; > say unpack q{b*}, $str;' 0000000000000000000000000000000000000000111100001111111111111111 0000000000000000000000000000000000000000111100001111111111111111000000 +0000000000000000000000000000000000000000000000000011110000 knoppix@Microknoppix:~$ perl -E ' > vec( $str, 0, 32 ) = 0xfffff; > say unpack q{b*}, $str; > say unpack q{b*}, pack q{N}, 0xfffff;' 00000000111100001111111111111111 00000000111100001111111111111111 knoppix@Microknoppix:~$

    I hope this is useful.

    Cheers,

    JohnGG

Re: Fast string construction
by Eliya (Vicar) on Apr 07, 2011 at 13:15 UTC

    You could also use the x operator (repetition):

    $bin = "\0" x (4 * ($last_val+1));

    A benchmark would have to show whether it's more efficient, though...

Re: Fast string construction
by ikegami (Patriarch) on Apr 07, 2011 at 16:06 UTC
    Does it actually have to be a string, or does it just have to be efficient? You may want to consider Judy::L

      (Blink...)

      That’s what I love about this site, and about Perl.   It’s a never-ending educational experience.