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

Hi there ,

i have an array in the following format,

a=1,b=5,c=8,d=89, a=5,b=65,c=8,d=19, a=41,b=98,c=8,d=34, a=341,b=125,c=3,d=19, a=65,b=23,c=8,d=70,

I need to convert this into a array of hashes like,

@table_array={a=1,b=5,c=8,d=89},
{=5,b=65,c=8,d=19},
{a=41,b=98,c=8,d=34}, and so on..

I tried for create a hash map traversing the array in a for loop and push it into another array , but when i try to print the hashes it always contains the last element for all the values in the array.

Please help me out in performing the above operation.

Thanks !!

Replies are listed 'Best First'.
Re: Convert array to array of hashes
by Fletch (Bishop) on Aug 14, 2009 at 16:02 UTC

    Obviously the problems on line 23 in your code which you've helpfully provided so . . . oh. Never mind.

    Vague handwaving: presuming all the sub-hashes are the same size (in your example four key/value pairs) you could trivially do it with splice to pull off 8 elements and then push a new hashref onto your list. Your problems with only having the items from the last set means that you're probably declaring a lexical hash in the wrong enclosing scope rather than somewhere it'll be unique for each set. But again, no code so that's just a slightly educated guess.

    Update:

    #!/usr/bin/env clj (ns split-sample (:use [clojure.contrib.str-utils :only (re-split)] [clojure.contrib.seq-utils :only (flatten)] clojure.contrib.pprint)) (def string "a=1,b=5,c=8,d=89, a=5,b=65,c=8,d=19, a=41,b=98,c=8,d=34, +a=341,b=125,c=3,d=19, a=65,b=23,c=8,d=70") (println (str "string:\n" string)) (def outbuff (java.io.StringWriter.)) (binding [*out* outbuff] (pprint (for [chunk (partition 4 (partition 2 (re-split #"=|,\s*" st +ring)))] (apply hash-map (flatten chunk))))) (println (str "munged:\n" outbuff)) (System/exit 0) ; string: ; a=1,b=5,c=8,d=89, a=5,b=65,c=8,d=19, a=41,b=98,c=8,d=34, a=341,b=125 +,c=3,d=19, a=65,b=23,c=8,d=70 ; munged: ; ({"a" "1", "b" "5", "c" "8", "d" "89"} ; {"a" "5", "b" "65", "c" "8", "d" "19"} ; {"a" "41", "b" "98", "c" "8", "d" "34"} ; {"a" "341", "b" "125", "c" "3", "d" "19"} ; {"a" "65", "b" "23", "c" "8", "d" "70"})

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Thanks everyone for your help. I should have given the code snippets for you to understand the problem. Sorry abt that.

      Anyways i got the spark from line "you're probably declaring a lexical hash in the wrong enclosing scope" and got my stuff working.

      Thanks again!!!
Re: Convert array to array of hashes
by ssandv (Hermit) on Aug 14, 2009 at 16:57 UTC

    It's extremely hard to answer this question because there's nothing intuitive about your initial array or your final hashes. So when you say "like ...", we have no idea what that array of hashes is like.

    If you actually made an array of hashes like that, I believe it'd be

    @table_array=( #you forgot the parentheses, by the way {1=>5,8=>89,}, {5=>65,8=>19,}, ... )
    because assignment evaluates to the value assigned. Since I'm pretty sure that's not what you want, could you please be more clear about your starting and ending data? (Hint: use quotes where they're appropriate, and put your data in <code></code> tags.)
Re: Convert array to array of hashes
by Marshall (Canon) on Aug 15, 2009 at 01:43 UTC
    You don't have an array with this this format: a=1,b=5,c=8,d=89, a=5,b=65,c=8,d=19,a=41,b=98,c=8,d=34,a=341,b=125,c=3,d=19, a=65,b=23,c=8,d=70.

    That is not an "array". That looks like a string. maybe you have a file like this?????:

    1,5,8,89; 5 65 8 19 41 98 8 34
    Please be more specific.
Re: Convert array to array of hashes
by bichonfrise74 (Vicar) on Aug 14, 2009 at 17:37 UTC
    Assuming this is how you defined you array, then you can probably use something like this.
    #!/usr/bin/perl use strict; my @AoH; my @arrays = ( "a=1,b=5,c=8,d=89", "a=5,b=65,c=8,d=19", "a=41,b=98,c=8,d=34", "a=341,b=125,c=3,d=19", "a=65,b=23,c=8,d=70" ); push ( @AoH, [ $arrays[$_] ] ) for (0 .. $#arrays);