Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Getting associative array from a hash

Reply
Thread Tools

Getting associative array from a hash

 
 
Ignoramus28164
Guest
Posts: n/a
 
      07-07-2005
I wrote a perl based algebra expression simplifier with work shown.

http://www.algebra.com/services/rend...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

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      07-07-2005
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.

 
Reply With Quote
 
 
 
 
Ignoramus28164
Guest
Posts: n/a
 
      07-07-2005
Thanks Brian, that's awesome!

i

On Thu, 07 Jul 2005 20:45:29 +0100, Brian McCauley <(E-Mail Removed)> 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.
>



--

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      07-07-2005
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/rend...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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-07-2005
Paul Lalli <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Ignoramus28164
Guest
Posts: n/a
 
      07-08-2005

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 <(E-Mail Removed)> wrote:
> Paul Lalli <(E-Mail Removed)> 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)
> )
>
>



--

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      07-08-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Why "associative" in associative container? desktop C++ 5 06-26-2007 07:49 AM
Array and Hash (Associative array) in JavaScript v.3.0 VK Javascript 36 08-03-2005 02:39 PM
<FAQENTRY> Array and hash (associative array) VK Javascript 47 07-13-2005 06:27 AM



Advertisments