Getting associative array from a hash

I

Ignoramus28164

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
 
B

Brian McCauley

Ignoramus28164 said:
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.
 
I

Ignoramus28164

Thanks Brian, that's awesome!

i

Ignoramus28164 said:
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.


--
 
P

Paul Lalli

Ignoramus28164 said:
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
 
T

Tad McClellan

Paul Lalli said:
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)
)
 
I

Ignoramus28164

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

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.
)




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)
)


--
 
J

John Bokma

Ignoramus28164 said:
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 :-(
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top