Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: slurped scalar map

by 0xbeef (Hermit)
on Jun 20, 2006 at 21:26 UTC ( #556533=note: print w/replies, xml ) Need Help??


in reply to Re: slurped scalar map
in thread slurped scalar map

Thanks, this elaborates on your analysis as johngg mentioned. I do not have fixed record seperators but I do have records of variable length/content so I rely on offset and size. I intend to use your example based on calculating each record's end offset:

#!/usr/bin/perl -slw use strict; my $data = <<EOD; record 1 is 20 bytesrecord 2 is 20 bytesrecord 3 is longer at 30 bytes EOD my $p = 0; my @refs; # endpos would be calculated based on recsize - this is simplified: my @endpos=(20,40,70); for (@endpos) { push @refs, \substr $data, $p, $_ - $p; $p = $_; } print '[',$$_,']' for @refs;

Hope I got that right.

Niel

Replies are listed 'Best First'.
Re^3: slurped scalar map
by BrowserUk (Patriarch) on Jun 21, 2006 at 07:36 UTC

    Just be aware that even this method will only save you space where your records are longer than (from memory) 12 characters. And if you are having to store another array containing the record length, then you have to factor the size of that array into the argument as well unless you replace each record length with the lvalue ref of the record as you go. The space consumed storing the lengths will depend upon whether the numeric values are loaded and stored as IVs or PVs. Around 20 bytes/length for the former and approx. 50 for the later in addition to that used to store the lvalue ref.

    Also, it only makes sense to build an array of refs if you are going to randomly access each (or some) records more than once. Otherwise, it would be better to simply generate and use an lvalue ref for each record as you need it. The trade-off between replacing the lengths with lvalue refs, and generatng them on the fly will depend upon the number and frequency with which you re-access records through the life of the program; how the length are loaded etc.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://556533]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-05-25 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?