efficient string copy without using refs 
Author Message
 efficient string copy without using refs


> Sent: Friday, September 01, 2000 18:17

> Subject: Q: efficient string copy without using refs

> I am thinking about how to copy a large scalar data value efficiently into
> a hash (i.e. with minimal extra memory usage).  One way is to use a ref to
> the large string , but I would like to access the value later as a simple
> scalar, not as a ref.

> An example (this code is just to illustrate what I mean)

>    my %hash;

>    if (my ($item1,$item2) = m/(item1_re)(large_data_re)/ )
>    {  # item 2 is big
>       my $key=some_function_of($item1);
>       $hash{$key}=$item2;  # but I don't want to make a copy of
>                            # item2.
>    }

> I could use
>            $hash{$item1}= \$item2;

> but then I always have to remember to deref the item later, which is not a
> real big deal, except that it makes this one hash an exception to
> virtually all the others, so its easy to forget.

> I seems like there should be some way to make the hash at that key "take
> over" the ownership of the string item2.  If this was a simple variable
> (and also not a my) then I think I could say *var2 = \$item2, but the
> value has to go into a hash, and its usually a my variable to boot.

> Suggestions on the best (any?) ways to do this would be appreciated.

How about a two-step capture:

        if (my ($item1) = m/(item1_re)/) {
           $hash{some_function_of($item1)} = (/item1_re(large_data_re)/)[0];

Larry Rosler
Hewlett-Packard Laboratories

Thu, 20 Feb 2003 02:17:33 GMT  
 [ 1 post ] 

 Relevant Pages 

1. HELP! 2 bde application using the same database

2. TFileStream.Create >>> Limit size???

3. Q: efficient string copy without using refs

4. s// using lexicals and refs in an eval'ed string

5. Detach dBase Index without DBD ?

6. Array refs without temp-vars and strange behaviour of my

7. Using File::Copy and Sockets to copy public files from http addresses

8. Warning using copy from File::Copy

9. Binary -font file for Pascal

10. DBGrid: How do I make a sum, of all the numbers occuring in a column?

11. help: push array refs into hash, sort by refs in hash

12. strings and symbol refs and strict - oh my!


Powered by phpBB® Forum Software