[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to serialize hash to a file?

hurcan solter

5/20/2008 5:47:00 AM

i have an xml file that goes like;

<objectClass name="EmitterBeam" >
<attribute name="BeamElevationCenter" dataType="Float1"/>
<attribute name="BeamIdentifier" dataType="HLAoctet"/>
</objectClass>

at some point i need the look up the data type for different
attributes.
I can get the data type using a Xpath query in the form using REXML

node = $fom_file.elements["//attribute [@name = '#{attribute_name}']"]

it works okay, the thing is ,it takes ages (which is about ten
seconds)

so I have a mind to cache the attribute-datatype pairs in a hash for
faster lookup.
So can you guys tell me a simple and elegant way to do serialize
deserialize that hash to a file?
TIA
Hurcan Solter

4 Answers

Joel VanderWerf

5/20/2008 6:23:00 AM

0

hurcan solter wrote:
> i have an xml file that goes like;
>
> <objectClass name="EmitterBeam" >
> <attribute name="BeamElevationCenter" dataType="Float1"/>
> <attribute name="BeamIdentifier" dataType="HLAoctet"/>
> </objectClass>
>
> at some point i need the look up the data type for different
> attributes.
> I can get the data type using a Xpath query in the form using REXML
>
> node = $fom_file.elements["//attribute [@name = '#{attribute_name}']"]
>
> it works okay, the thing is ,it takes ages (which is about ten
> seconds)
>
> so I have a mind to cache the attribute-datatype pairs in a hash for
> faster lookup.
> So can you guys tell me a simple and elegant way to do serialize
> deserialize that hash to a file?
> TIA
> Hurcan Solter

Pretty easy...

h = {1=>2, 3=>4}
File.open("foo", "wb") {|f| Marshal.dump(h, f)}
p File.open("foo", "rb") {|f| Marshal.load(f)} # ==> {1=>2, 3=>4}

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Robert Klemme

5/20/2008 6:36:00 AM

0

On 20.05.2008 07:46, hurcan solter wrote:
> i have an xml file that goes like;
>
> <objectClass name="EmitterBeam" >
> <attribute name="BeamElevationCenter" dataType="Float1"/>
> <attribute name="BeamIdentifier" dataType="HLAoctet"/>
> </objectClass>
>
> at some point i need the look up the data type for different
> attributes.
> I can get the data type using a Xpath query in the form using REXML
>
> node = $fom_file.elements["//attribute [@name = '#{attribute_name}']"]
>
> it works okay, the thing is ,it takes ages (which is about ten
> seconds)
>
> so I have a mind to cache the attribute-datatype pairs in a hash for
> faster lookup.
> So can you guys tell me a simple and elegant way to do serialize
> deserialize that hash to a file?
> TIA
> Hurcan Solter

irb(main):012:0> hash={1=>2}
=> {1=>2}
irb(main):013:0> hash.object_id
=> 1073423700
irb(main):014:0> File.open("foo", "wb") {|io| Marshal.dump(hash, io)}
=> #<File:foo (closed)>
irb(main):015:0> hash = File.open("foo", "rb") {|io| Marshal.load(io)}
=> {1=>2}
irb(main):016:0> hash.object_id
=> 134268870

Kind regards

robert

Robert Klemme

5/20/2008 7:23:00 AM

0

2008/5/20 Joel VanderWerf <vjoel@path.berkeley.edu>:
> hurcan solter wrote:
>>
>> i have an xml file that goes like;
>>
>> <objectClass name="EmitterBeam" >
>> <attribute name="BeamElevationCenter" dataType="Float1"/>
>> <attribute name="BeamIdentifier" dataType="HLAoctet"/>
>> </objectClass>
>>
>> at some point i need the look up the data type for different
>> attributes.
>> I can get the data type using a Xpath query in the form using REXML
>>
>> node = $fom_file.elements["//attribute [@name = '#{attribute_name}']"]
>>
>> it works okay, the thing is ,it takes ages (which is about ten
>> seconds)
>>
>> so I have a mind to cache the attribute-datatype pairs in a hash for
>> faster lookup.
>> So can you guys tell me a simple and elegant way to do serialize
>> deserialize that hash to a file?
>> TIA
>> Hurcan Solter
>
> Pretty easy...
>
> h = {1=>2, 3=>4}
> File.open("foo", "wb") {|f| Marshal.dump(h, f)}
> p File.open("foo", "rb") {|f| Marshal.load(f)} # ==> {1=>2, 3=>4}

Amazing how we nearly picked the same solution - even the file name is
identical. :-)

Cheers

robert


--
use.inject do |as, often| as.you_can - without end

Joel VanderWerf

5/20/2008 4:04:00 PM

0

Robert Klemme wrote:
> 2008/5/20 Joel VanderWerf <vjoel@path.berkeley.edu>:
...
>> h = {1=>2, 3=>4}
>> File.open("foo", "wb") {|f| Marshal.dump(h, f)}
>> p File.open("foo", "rb") {|f| Marshal.load(f)} # ==> {1=>2, 3=>4}
>
> Amazing how we nearly picked the same solution - even the file name is
> identical. :-)

Heh. Maybe we've just been here too long, and our brains have been
saturated ;)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407