FAQ
How can I create a Hash of Hashes from two lists. Is it possible?

I want the effective functionality to be served like this

$ChildHash["Joe"]["21A"]="Sally"

i.e Joe at 21A has a child called Sally. List1 here will be the name of Parents, List2 here will contain the house number.

Soham



From cricket scores to your friends. Try the Yahoo! India Homepage! http://in.yahoo.com/trynew

Search Discussions

  • Shawn H Corey at Sep 29, 2009 at 9:56 am

    Soham Das wrote:
    How can I create a Hash of Hashes from two lists. Is it possible?

    I want the effective functionality to be served like this

    $ChildHash["Joe"]["21A"]="Sally"

    i.e Joe at 21A has a child called Sally. List1 here will be the name of Parents, List2 here will contain the house number.
    Hashes use {}, arrays use []

    $ChildHash{"Joe"}{"21A"} = "Sally";


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Programming is as much about organization and communication
    as it is about coding.

    I like Perl; it's the only language where you can bless your
    thingy.
  • Jeff Peng at Sep 29, 2009 at 11:40 am

    2009/9/29 Shawn H Corey <shawnhcorey@gmail.com>:
    Soham Das wrote:
    How can I create a Hash of Hashes from two lists. Is it possible?

    I want the effective functionality to be served like this

    $ChildHash["Joe"]["21A"]="Sally"

    i.e Joe at 21A has a child called Sally. List1 here will be the name of
    Parents, List2 here will contain the house number.
    Hashes use {}, arrays use []

    That's in Python? :-)
    Perl's both hash and array use ().
    But anonymous hash and array use {} and [].
  • Chas. Owens at Sep 29, 2009 at 12:36 pm

    On Tue, Sep 29, 2009 at 07:40, Jeff Peng wrote:
    2009/9/29 Shawn H Corey <shawnhcorey@gmail.com>:
    Soham Das wrote:
    How can I create a Hash of Hashes from two lists. Is it possible?

    I want the effective functionality to be served like this

    $ChildHash["Joe"]["21A"]="Sally"

    i.e Joe at 21A has a child called Sally. List1 here will be the name of
    Parents, List2 here will contain the house number.
    Hashes use {}, arrays use []

    That's in Python? :-)
    Perl's both hash and array use ().
    But anonymous hash and array use {} and [].
    snip

    To clarfy,

    Perl uses (LIST) to initialize hashs and arrays. One way to create a
    list is (X).

    my @a = (1, 2, 3);
    my %h = (a => 1, b => 2, c => 3);

    Perl uses X[Y] to get the value at the Yth index in X when X is an
    array or a list.

    my $x = $a[1]; #$x is now 2

    Perl uses X{Y} to get the value associated with Y when X is a hash.

    $x = $h{c}; #$x is now 3

    Perl uses [LIST] to create an anonymous array ref.

    my $aref = [1, 2, 3];
    $x = $aref->[0]; #$x is now 1

    Perl uses {LIST} to create an anonymous hash ref.

    my $href = {a => 1, b => 2, c => 3};
    $x = $href->{b}; #$x is now 2

    See [perldoc perlop][1] or my [perlopref][2] document for more information.

    [1] : http://perldoc.perl.org/functions/perlop.html
    [2] : http://github.com/cowens/perlopref

    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.
  • Shawn H Corey at Sep 29, 2009 at 12:40 pm

    Jeff Peng wrote:
    That's in Python? :-)
    Wouldn't know, don't do Python.
    Perl's both hash and array use ().
    But anonymous hash and array use {} and [].
    Hashes use {}, arrays use [], lists use ().

    When you set an element of a hash, you use {} to surround its key:

    $hash{$key} = $value;

    When you set an element of an array, you use [] to surround its index:

    $array[$index] = $datum;

    When you set both to a list, surround the list with ():

    %hash = ( 1, 2, 3, );
    @array = ( 'a', 'b', 'c', );

    See `perldoc perldata` for more details.
    http://perldoc.perl.org/perldata.html


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Programming is as much about organization and communication
    as it is about coding.

    I like Perl; it's the only language where you can bless your
    thingy.
  • Thomas Bätzler at Sep 29, 2009 at 10:16 am

    Soham Das asked:
    How can I create a Hash of Hashes from two lists. Is it possible?

    I want the effective functionality to be served like this

    $ChildHash["Joe"]["21A"]="Sally"

    i.e Joe at 21A has a child called Sally. List1 here will be the name of
    Parents, List2 here will contain the house number.
    Please keep in mind: square brackets are for arrays/lists. Curly brackets are for hashes.

    In any case, wouldn't it be smarter to organize your data differently?

    I.e.:

    %parent = ( 'Joe' => { 'address' => '21A', children => ['Dick','Sally'] } );

    To add another child to an existing parent you'd then say

    push @{$parent{'Joe'}{'children'}}, 'Jane';

    To add a new parent:

    @{$parent{'Sven'}}{'address','children'} = ( '9b', ['Bjorn'] );

    HTH,
    Thomas
  • Soham Das at Sep 29, 2009 at 10:36 am
    Yes,its much more powerful, the way you said, but in my case it won't be necessary or important.

    Here I guess, I gave a wrong example where the data can be changed.

    Lets assume, the hash of hash being a record of something which has already happened and hence we know the final value, not something which is right now happening, i.e changeable.

    In my case, its like

    $Position{$Scrip}{$Date}= #some value

    That is, my position in a previous date $Date, in the stock $scrip, was some integer. Thanks for the correction, regarding the brackets. I stand corrected and it seems I have made a lot of such mistakes apparent in the previous two three mails.

    Soham




    ________________________________
    From: Thomas Bätzler <t.baetzler@bringe.com>
    To: beginners@perl.org
    Cc: Soham Das <sohamdas@yahoo.co.in>
    Sent: Tuesday, 29 September, 2009 3:46:28 PM
    Subject: AW: Hash of Hashes

    Soham Das <sohamdas@yahoo.co.in> asked:
    How can I create a Hash of Hashes from two lists. Is it possible?

    I want the effective functionality to be served like this

    $ChildHash["Joe"]["21A"]="Sally"

    i.e Joe at 21A has a child called Sally. List1 here will be the name of
    Parents, List2 here will contain the house number.
    Please keep in mind: square brackets are for arrays/lists. Curly brackets are for hashes.

    In any case, wouldn't it be smarter to organize your data differently?

    I.e.:

    %parent = ( 'Joe' => { 'address' => '21A', children => ['Dick','Sally'] } );

    To add another child to an existing parent you'd then say

    push @{$parent{'Joe'}{'children'}}, 'Jane';

    To add a new parent:

    @{$parent{'Sven'}}{'address','children'} = ( '9b', ['Bjorn'] );

    HTH,
    Thomas



    Connect more, do more and share more with Yahoo! India Mail. Learn more. http://in.overview.mail.yahoo.com/
  • Uri Guttman at Sep 29, 2009 at 3:48 pm
    "SD" == Soham Das writes:

    SD> Lets assume, the hash of hash being a record of something which
    SD> has already happened and hence we know the final value, not
    SD> something which is right now happening, i.e changeable.

    you keep swapping hash and array concepts, words and symbols. please
    learn to keep them separate or your perl life will be hell. hashes have
    no concept of 'final value' as an array would. maybe you mean the hash
    is finalized and won't be changed anymore. if so then say that.

    SD> In my case, its like

    SD> $Position{$Scrip}{$Date}= #some value

    SD> That is, my position in a previous date $Date, in the stock
    SD> $scrip, was some integer. Thanks for the correction, regarding the
    SD> brackets. I stand corrected and it seems I have made a lot of such
    SD> mistakes apparent in the previous two three mails.

    hashes have no positions, just keys. again. try to use standard
    terminology or you won't convey any proper meaning here. programming
    requires this to be accurate. and yes, you have been making a bunch of
    hash/array mistakes and you must fix that in your head. they are similar
    in syntax styles in some ways (e.g. [] vs {}) but very different in
    semantics and terminology.

    uri
  • Shawn H Corey at Sep 29, 2009 at 4:09 pm

    Uri Guttman wrote:
    "SD" == Soham Das <sohamdas@yahoo.co.in> writes:

    SD> Lets assume, the hash of hash being a record of something which
    SD> has already happened and hence we know the final value, not
    SD> something which is right now happening, i.e changeable.

    you keep swapping hash and array concepts, words and symbols. please
    learn to keep them separate or your perl life will be hell. hashes have
    no concept of 'final value' as an array would. maybe you mean the hash
    is finalized and won't be changed anymore. if so then say that.

    SD> In my case, its like

    SD> $Position{$Scrip}{$Date}= #some value

    SD> That is, my position in a previous date $Date, in the stock
    SD> $scrip, was some integer. Thanks for the correction, regarding the
    SD> brackets. I stand corrected and it seems I have made a lot of such
    SD> mistakes apparent in the previous two three mails.

    hashes have no positions, just keys. again. try to use standard
    terminology or you won't convey any proper meaning here. programming
    requires this to be accurate. and yes, you have been making a bunch of
    hash/array mistakes and you must fix that in your head. they are similar
    in syntax styles in some ways (e.g. [] vs {}) but very different in
    semantics and terminology.

    uri
    It would also help to post the snippet of code that is causing you
    problems. Include some data it is to work on (not real data, you don't
    want to post real data on a public mailing list; create some typical but
    fake data) and the output you want. There is no need to post the actual
    output; that can be generated by running the code.


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Programming is as much about organization and communication
    as it is about coding.

    I like Perl; it's the only language where you can bless your
    thingy.
  • Soham Das at Sep 30, 2009 at 5:00 am
    ----- Forwarded Message ----
    From: Soham Das <sohamdas@yahoo.co.in>
    To: Uri Guttman <uri@StemSystems.com>
    Sent: Wednesday, 30 September, 2009 10:29:12 AM
    Subject: Re: AW: Hash of Hashes







    ________________________________
    From: Uri Guttman <uri@StemSystems.com>
    To: Soham Das <sohamdas@yahoo.co.in>
    Cc: Thomas Bätzler <t.baetzler@bringe.com>; beginners@perl.org
    Sent: Tuesday, 29 September, 2009 9:17:57 PM
    Subject: Re: AW: Hash of Hashes
    "SD" == Soham Das <sohamdas@yahoo.co.in> writes:

    SD> Lets assume, the hash of hash being a record of something which
    SD> has already happened and hence we know the final value, not
    SD> something which is right now happening, i.e changeable.
    you keep swapping hash and array concepts, words and symbols. please
    learn to keep them separate or your perl life will be hell. hashes have
    no concept of 'final value' as an array would. maybe you mean the hash
    is finalized and won't be changed anymore. if so then say that.
    The hash is finalised and wont be changing anymore.

    SD> In my case, its like

    SD> $Position{$Scrip}{$Date}= #some value

    SD> That is, my position in a previous date $Date, in the stock
    SD> $scrip, was some integer. Thanks for the correction, regarding the
    SD> brackets. I stand corrected and it seems I have made a lot of such
    SD> mistakes apparent in the previous two three mails.
    hashes have no positions, just keys. again. try to use standard
    terminology or you won't convey any proper meaning here. programming
    requires this to be accurate. and yes, you have been making a bunch of
    hash/array mistakes and you must fix that in your head. they are similar
    in syntax styles in some ways (e.g. [] vs {}) but very different in
    semantics and terminology.

    Now what I wrote, was not describing what that statement is supposed to do, but the english transliteration of that statement. i.e the hash position will convey me the record of my positions.
    Hence my position in a particular scrip on a particular date (which will act as keys) will give me some value, which will stand for the number of shares I was holding.

    I hope I am not ambiguous this time.
    uri

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/



    ________________________________
    Now, send attachments up to 25MB with Yahoo! India Mail. Learn how.


    Yahoo! India has a new look. Take a sneak peek http://in.yahoo.com/trynew
  • Uri Guttman at Sep 30, 2009 at 5:22 am

    "SD" == Soham Das writes:
    hashes have no positions, just keys. again. try to use standard
    terminology or you won't convey any proper meaning here. programming
    requires this to be accurate. and yes, you have been making a bunch of
    hash/array mistakes and you must fix that in your head. they are similar
    in syntax styles in some ways (e.g. [] vs {}) but very different in
    semantics and terminology.

    SD> Now what I wrote, was not describing what that statement is
    SD> supposed to do, but the english transliteration of that
    SD> statement. i.e the hash position will convey me the record of my
    SD> positions. Hence my position in a particular scrip on a
    SD> particular date (which will act as keys) will give me some value,
    SD> which will stand for the number of shares I was holding.

    you are still inventing terms and not using standard ones. there is no
    hash position. i think you mean slot or entry which are commonly used
    for where a hash stores the value associated with a key. but that has
    nothing to do with a position in a script nor a date. it will help you
    enormously if you use the common terms for perl things. hashes have keys
    and values. values are stored in slots or entries (arrays or
    hashes). array slots are accessed by an integer index (or sequentially
    in a loop) and hash slots are accessed by a string key.

    read perldoc perldsc, perllol for more on perl data structures and how
    to create and access them.

    uri

    --
    Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 29, '09 at 9:43a
activeSep 30, '09 at 5:22a
posts11
users6
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase