[newbie]saving and reading array of associative array

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default [newbie]saving and reading array of associative array

    i'm looking for examples of saving to file and reading back an array of
    associative array, in a ruby like way.

    saying i have something like :

    one = {"Name" => "Smith", "Surname" => "John" ...}
    two = {"Name" => "Dupont", "Surname" => "Jean" ...}

    myFriends = [one, two, ...]

    i want to save into a file "myFriends.db" the array of hashes myFriends
    in such a way reading it back will recorver the array of hashes the
    easiest way...
    --
    Yvon
    Yvon Thoraval Guest

  2. Similar Questions and Discussions

    1. associative array length
      Is there any way of knowing the length of an associative array? I could build a static function that does the job (iterates over the as. array and...
    2. ASSOCIATIVE ARRAY
      How do I do to get the index of a associative array? //FMX CODE ar = new Array(); ar = 'John' //I need to get the value 'name'
    3. #25237 [Opn->WFx]: Two, associative array functions
      ID: 25237 Updated by: sniper@php.net Reported By: gavin at itmerge dot com -Status: Open +Status: ...
    4. #25237 [NEW]: Two, associative array functions
      From: gavin at itmerge dot com Operating system: Linux PHP version: 4.3.3 PHP Bug Type: Feature/Change Request Bug...
    5. function to empty associative array
      On 7 Jul 2003 12:53:14 -0700, lkrubner@geocities.com (lawrence) wrote: reset()? I'm a bigger fan of foreach, which has a reset built in. ...
  3. #2

    Default Re: [newbie]saving and reading array of associative array

    il Wed, 17 Sep 2003 11:13:26 +0200,
    [email]yvon.thoravallist@-SUPPRIMEZ-free.fr.inva[/email]lid (Yvon Thoraval) ha
    scritto::
    >i'm looking for examples of saving to file and reading back an array of
    >associative array, in a ruby like way.
    >
    >saying i have something like :
    >
    >one = {"Name" => "Smith", "Surname" => "John" ...}
    >two = {"Name" => "Dupont", "Surname" => "Jean" ...}
    >
    >myFriends = [one, two, ...]
    >
    >i want to save into a file "myFriends.db" the array of hashes myFriends
    >in such a way reading it back will recorver the array of hashes the
    >easiest way...
    ----------------------------------------------------------
    Marshal::dump
    dump( anObject [, anIO] , limit=--1 ) -> anIO
    ------------------------------------------------------------------------
    Serializes anObject and all descendent objects. If anIO is
    specified, the serialized data will be written to it, otherwise
    the
    data will be returned as a String. If limit is specified, the
    traversal of subobjects will be limited to that depth. If limit
    is
    negative, no checking of depth will be performed.

    More easy than you think ;)

    BTW you could even use PStore, included in the standard lib, it works
    similar to an hash, but gets saved on disk and supports 'transactions'
    for data integrity.
    gabriele renzi Guest

  4. #3

    Default Re: [newbie]saving and reading array of associative array

    gabriele renzi <surrender_it@remove.yahoo.it> wrote:
    >
    > ----------------------------------------------------------
    > Marshal::dump
    > dump( anObject [, anIO] , limit=--1 ) -> anIO
    > ------------------------------------------------------------------------
    > Serializes anObject and all descendent objects. If anIO is
    > specified, the serialized data will be written to it, otherwise
    > the
    > data will be returned as a String. If limit is specified, the
    > traversal of subobjects will be limited to that depth. If limit
    > is
    > negative, no checking of depth will be performed.
    >
    > More easy than you think ;)
    >
    > BTW you could even use PStore, included in the standard lib, it works
    > similar to an hash, but gets saved on disk and supports 'transactions'
    > for data integrity.
    tanxs a lot !
    --
    Yvon
    Yvon Thoraval Guest

  5. #4

    Default Re: [newbie]saving and reading array of associative array

    On 9/17/2003 6:34 AM, gabriele renzi wrote:
    >Marshal::dump
    > dump( anObject [, anIO] , limit=--1 ) -> anIO
    >
    >BTW you could even use PStore, included in the standard lib, it works
    >similar to an hash, but gets saved on disk and supports 'transactions'
    >for data integrity.
    >
    >
    You could also use YAML:

    require 'yaml'
    one = {"Name" => "Smith", "Surname" => "John"}
    two = {"Name" => "Dupont", "Surname" => "Jean"}

    myFriends = [one, two]

    File.open("myfriends.db", "w") {|file| file.write(myFriends.to_yaml)}
    myFriends = File.open("myfriends.db") {|file| YAML.load(file)}


    --
    Dean saor, dean saor an spiorad. Is seinn d'orain beo.

    [url]http://www.joeygibson.com[/url]
    [url]http://www.joeygibson.com/blog/life/Wisdom.html[/url]




    Joey Gibson Guest

  6. #5

    Default Re: [newbie]saving and reading array of associative array

    Joey Gibson <joey@joeygibson.com> wrote:
    >
    > You could also use YAML:
    >
    > require 'yaml'
    > one = {"Name" => "Smith", "Surname" => "John"}
    > two = {"Name" => "Dupont", "Surname" => "Jean"}
    >
    > myFriends = [one, two]
    >
    > File.open("myfriends.db", "w") {|file| file.write(myFriends.to_yaml)}
    > myFriends = File.open("myfriends.db") {|file| YAML.load(file)}
    fine, i should say i've other versions of what i plan to do in :

    - perlCocoa (CamelBones specific to MacOS X)
    - php + xml
    - php + mySQL

    --
    Yvon
    Yvon Thoraval Guest

  7. #6

    Default Re: [newbie]saving and reading array of associative array

    On Wed, 17 Sep 2003 21:22:13 +0900
    Joey Gibson <joey@joeygibson.com> wrote:
    > On 9/17/2003 6:34 AM, gabriele renzi wrote:
    >
    > >Marshal::dump
    > > dump( anObject [, anIO] , limit=--1 ) -> anIO
    > >
    > >BTW you could even use PStore, included in the standard lib, it works
    > >similar to an hash, but gets saved on disk and supports 'transactions'
    > >for data integrity.
    > >
    > >
    >
    > You could also use YAML:
    >
    > require 'yaml'
    > one = {"Name" => "Smith", "Surname" => "John"}
    > two = {"Name" => "Dupont", "Surname" => "Jean"}
    >
    > myFriends = [one, two]
    >
    > File.open("myfriends.db", "w") {|file| file.write(myFriends.to_yaml)}
    > myFriends = File.open("myfriends.db") {|file| YAML.load(file)}
    Yes, I was just about to mention YAML. Also note that you can use
    YAML::Store as a drop-in PStore replacement. (In fact, this example is
    the example given in pstore.rb, I just require'd yaml/store, and changed
    the db = line to YAML::Store)

    ~/prog/ruby$ cat yaml-store-example.rb
    require 'yaml/store'
    db = YAML::Store.new("/tmp/foo")
    db.transaction do
    p db.roots
    ary = db["root"] = [1,2,3,4]
    ary[0] = [1,1.5]
    end

    db.transaction do
    p db["root"]
    end
    ~/prog/ruby$ ruby yaml-store-example.rb
    []
    [[1, 1.5], 2, 3, 4]
    ~/prog/ruby$ cat /tmp/foo
    ---
    root:
    -
    - 1
    - 1.5
    - 2
    - 3
    - 4
    ~/prog/ruby$

    Jason Creighton
    Jason Creighton Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139