Getting associative array from a hash

Discussion in 'Perl Misc' started by Ignoramus28164, Jul 7, 2005.

  1. I wrote a perl based algebra expression simplifier with work shown.

    http://www.algebra.com/services/rendering/simplifier.mpl

    It uses deeply nested structures of hashes holding references to
    arrays.

    Example:

    my $term = { factors => [ $factor1, $factor2], operator => '+' };

    I often need to get, say, the quantity of item in array held by the
    hash.

    I do it like this:

    my $factors = $term->{factors};

    print "Count = " . $#$factors . ".\n";

    Unfortunately, this infolves creation of a temporary variable. Can I
    avoid using a temporary?

    Also, how can I access an element by number, instead of using
    $$factors[0], can I say something like @($term->{factors})[0] or some
    such, without using a temporary variable.

    thanks

    i
     
    Ignoramus28164, Jul 7, 2005
    #1
    1. Advertising

  2. Ignoramus28164 wrote:

    > my $factors = $term->{factors};
    >
    > print "Count = " . $#$factors . ".\n";
    >
    > Unfortunately, this infolves creation of a temporary variable. Can I
    > avoid using a temporary?


    > print "Count = $#{$term->{factors}}.\n";


    >
    > Also, how can I access an element by number, instead of using
    > $$factors[0], can I say something like @($term->{factors})[0] or some
    > such, without using a temporary variable.


    $term->{factors}[0]

    OR

    $term->{factors}->[0]

    OR

    ${$term->{factors}}[0]

    OR (if you want to access a slice)

    @{$term->{factors}}[0]

    Note: documentation on using references in Perl can be found in the
    manual perlref.
     
    Brian McCauley, Jul 7, 2005
    #2
    1. Advertising

  3. Thanks Brian, that's awesome!

    i

    On Thu, 07 Jul 2005 20:45:29 +0100, Brian McCauley <> wrote:
    > Ignoramus28164 wrote:
    >
    >> my $factors = $term->{factors};
    >>
    >> print "Count = " . $#$factors . ".\n";
    >>
    >> Unfortunately, this infolves creation of a temporary variable. Can I
    >> avoid using a temporary?

    >
    > > print "Count = $#{$term->{factors}}.\n";

    >
    >>
    >> Also, how can I access an element by number, instead of using
    >> $$factors[0], can I say something like @($term->{factors})[0] or some
    >> such, without using a temporary variable.

    >
    > $term->{factors}[0]
    >
    > OR
    >
    > $term->{factors}->[0]
    >
    > OR
    >
    > ${$term->{factors}}[0]
    >
    > OR (if you want to access a slice)
    >
    > @{$term->{factors}}[0]
    >
    > Note: documentation on using references in Perl can be found in the
    > manual perlref.
    >



    --
     
    Ignoramus28164, Jul 7, 2005
    #3
  4. Ignoramus28164

    Paul Lalli Guest

    Ignoramus28164 wrote:
    > Note: The author of this message requested that it not be archived. This
    > message will be removed from Groups in 6 days (Jul 14, 3:10 pm).


    Please don't do that. One of the points of Usenet is to allow people
    to search for answers to their questions. Often that involves reading
    posts that happened years in the past.

    > I wrote a perl based algebra expression simplifier with work shown.
    >
    > http://www.algebra.com/services/rendering/simplifier.mpl
    >
    > It uses deeply nested structures of hashes holding references to
    > arrays.
    >
    > Example:
    >
    > my $term = { factors => [ $factor1, $factor2], operator => '+' };
    >
    > I often need to get, say, the quantity of item in array held by the
    > hash.
    >
    > I do it like this:
    >
    > my $factors = $term->{factors};
    >
    > print "Count = " . $#$factors . ".\n";
    >
    > Unfortunately, this infolves creation of a temporary variable. Can I
    > avoid using a temporary?


    The temporary is the least of your problems with that code. $#$factors
    doesn't give you the size. It gives you one less than the size. You
    want @$factors there.

    To answer your question, yes, you can get the size of a nested array
    without using a temporary:

    print "Count = " . @{$term->{factors}} . ".\n";

    Simply dereference the array reference, and use the result in scalar
    context.

    > Also, how can I access an element by number, instead of using
    > $$factors[0], can I say something like @($term->{factors})[0] or some
    > such, without using a temporary variable.


    Same concept. Since @{$term->{factors}} is the array that
    $term->{factors} refers to, you find the first element of that array
    the same way you find the first element of any other array - replace
    the @ with $ and append [0]:
    ${$term->{factors}}[0]

    Another way of doing this is realizing that $term->{factors} is itself
    an array reference, and so you can get the first element of the array
    it references just like you got the 'factors' value of the hash $term
    references, using arrow notation:
    $term->{factors}->[0]
    You can then apply the rule that any time an arrow separates two
    bracket-like characters, it can be omitted:

    $term->{factors}[0]

    All three of the above statements are equally valid. Choose the one
    that makes the most sense to you. And for more information and
    explanation, check out the docs:

    perldoc perlreftut
    perldoc perlref
    perldoc perldsc
    perldoc perllol

    Paul Lalli
     
    Paul Lalli, Jul 7, 2005
    #4
  5. the X-No-Archive header (was Re: Getting associative array from a hash)

    Paul Lalli <> wrote:
    > Ignoramus28164 wrote:
    >> Note: The author of this message requested that it not be archived. This
    >> message will be removed from Groups in 6 days (Jul 14, 3:10 pm).

    >
    > Please don't do that.



    He was asked about that 2 months ago.

    (if there was any response is indeterminate, since I don't remember
    the thread, and the Google version is full of "holes" in the dialog.
    )


    > One of the points of Usenet is to allow people
    > to search for answers to their questions.



    So not setting X-No-Archive is giving to the community
    (which implies a lesser contribution when it _is_ set).

    Why does Ignoramus28164 want to help his fellow Perl programmers
    less than he could have?

    I am curious as to why he is setting it.

    Ignoramus28164:

    Why have you set X-No-Archive?



    On a more selfish note, setting X-No-Archive *reduces* the number
    of people who will see your question in the first place.

    (It is often a killfile pattern, probably because trolls like to
    set it so they'll leave no tracks (not that this OP is a troll)
    )


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 7, 2005
    #5
  6. Re: the X-No-Archive header (was Re: Getting associative array from a hash)

    I use X-No-Archive because I do not like my posts archived, in
    general. That said, I have nothing against my posts to clpm being
    archived, so I will try to manually remove this header.

    i

    On Thu, 7 Jul 2005 16:04:52 -0500, Tad McClellan <> wrote:
    > Paul Lalli <> wrote:
    >> Ignoramus28164 wrote:
    >>> Note: The author of this message requested that it not be archived. This
    >>> message will be removed from Groups in 6 days (Jul 14, 3:10 pm).

    >>
    >> Please don't do that.

    >
    >
    > He was asked about that 2 months ago.
    >
    > (if there was any response is indeterminate, since I don't remember
    > the thread, and the Google version is full of "holes" in the dialog.
    > )
    >
    >
    >> One of the points of Usenet is to allow people
    >> to search for answers to their questions.

    >
    >
    > So not setting X-No-Archive is giving to the community
    > (which implies a lesser contribution when it _is_ set).
    >
    > Why does Ignoramus28164 want to help his fellow Perl programmers
    > less than he could have?
    >
    > I am curious as to why he is setting it.
    >
    > Ignoramus28164:
    >
    > Why have you set X-No-Archive?
    >
    >
    >
    > On a more selfish note, setting X-No-Archive *reduces* the number
    > of people who will see your question in the first place.
    >
    > (It is often a killfile pattern, probably because trolls like to
    > set it so they'll leave no tracks (not that this OP is a troll)
    > )
    >
    >



    --
     
    Ignoramus28164, Jul 8, 2005
    #6
  7. Ignoramus28164

    John Bokma Guest

    Re: the X-No-Archive header (was Re: Getting associative array from a hash)

    Ignoramus28164 <ignoramus28164@NOSPAM.28164.invalid> wrote:

    >
    > I use X-No-Archive because I do not like my posts archived, in
    > general.


    That header is a request, nothing more. Not every archiving thingy is going
    to honor it. If you have to hide something, then don't post.

    Otherwise, you somewhat sabotage the Usenet :-(

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Jul 8, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. desktop
    Replies:
    5
    Views:
    388
    James Kanze
    Jun 26, 2007
  2. rp
    Replies:
    1
    Views:
    539
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    627
    David A. Black
    Jul 2, 2008
  4. VK
    Replies:
    47
    Views:
    550
    Thomas 'PointedEars' Lahn
    Jul 13, 2005
  5. VK
    Replies:
    36
    Views:
    653
    Martin Honnen
    Aug 3, 2005
Loading...

Share This Page