Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Hash to Scalar?

by Massyn (Hermit)
on Dec 09, 2002 at 12:03 UTC ( [id://218497]=perlquestion: print w/replies, xml ) Need Help??

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

#!/fellow/monks.pl

Is there an easy way to convert a hash to a scalar, and back again? I'm busy writing a mean script to "encode" and "decode" a hash into a scalar variable. The idea is to have a hash table for a number of users, so I need to be able to convert a hash to a scalar and vice versa.

I would like to store and retrieve a hash within a mySQL database via DBI for each user who has a entry in the table. Thus I need to convert the hash to a scalar.

Thanks!

#!/massyn.pl

You never know important a backup is until the day you need it.

Replies are listed 'Best First'.
Re: Hash to Scalar?
by fruiture (Curate) on Dec 09, 2002 at 12:09 UTC

    You can serailize any(*) datastructure using Storable (Standard). Have a look into the manpage.

    *: any common structure

    --
    http://fruiture.de
Re: Hash to Scalar?
by bronto (Priest) on Dec 09, 2002 at 12:17 UTC

    There are plenty of modules that do the job, with FreezeThaw and Storable being the most popular choices (it seems...).

    If, for any reason, you need an handcrafted solution, it depends on the data you have. Long time ago I used to serialize hashes using \cF (control-F) as field separator (i.e.: key/value separator) and \cR as record separator (i.e.: separator between key/value couples).

    Obviously, this will break if you have binary data.

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->make($love) ;
    }

Re: Hash to Scalar?
by kabel (Chaplain) on Dec 09, 2002 at 12:23 UTC
    the other way is to reflect the structure of the hash each user has in the sql table itself.

    you might try the Class::DBI module. there is a nice article about it on perl.com.

    HTH
Re: Hash to Scalar?
by ph0enix (Friar) on Dec 09, 2002 at 13:43 UTC

    You can use Storable

    use Storable qw(nfreeze thaw); $scalar = nfreeze(\%hash); %hash_copy = %{ thaw($scalar) };

    If you need to have hash with keys - usenames and values as data structure (not scalar) you can use tie... for binding to sql database use eg. Tie::RDBM module. Example from docs:

    use Tie::RDBM; tie %h,Tie::RDBM,'mysql:test',{table=>'Demo',create=>1,autocommit=>0}; $h{'key1'} = 'Some data here'; $h{'key2'} = 42; $h{'key3'} = { complex=>['data','structure','here'],works=>'true' }; $h{'key4'} = new Foobar('Objects work too'); print $h{'key3'}->{complex}->[0]; tied(%h)->commit; untie %h;
Re: Hash to Scalar?
by Theseus (Pilgrim) on Dec 09, 2002 at 14:24 UTC
    Don't forget Data::Dumper! My favorite module. =)
Re: Hash to Scalar?
by tall_man (Parson) on Dec 09, 2002 at 20:35 UTC
    I have used Data::DumpXML for this before. Since it's for a shared database the extra portability of the XML syntax may pay off later (if other applications have to read it).
Re: Hash to Scalar?
by grantm (Parson) on Dec 09, 2002 at 20:32 UTC
    The idea is to have a hash table for a number of users, so I need to be able to convert a hash to a scalar and vice versa.

    Do you actually need to convert to a scalar? Could references solve your problem (ie: an array of hashes or a hash of hashes)? Check out perlreftut if you're not familiar with references. Ignore me if you are :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-18 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found