Accessing an array inside of a package

R

Ryan McCoskrie

I've been working on a little Polish notation
calculator / interpreter just to get some
idea of how to deal with these issues.

However my design pins on having a package
that describes a sum including all of the numbers
in it and the perl interpreter won't accept this:

@self->{num} = []; #my list of numbers
.... #skip boring bit where the numbers are read in
#and check to be numbers
push @self->{num}, @input; #@input is the numbers

What I get is this:
Type of arg 1 to push must be array (not hash element) at
sum.pm line 16, near "@input;"
 
J

Jim Gibson

Ryan McCoskrie said:
I've been working on a little Polish notation
calculator / interpreter just to get some
idea of how to deal with these issues.

However my design pins on having a package
that describes a sum including all of the numbers
in it and the perl interpreter won't accept this:

@self->{num} = []; #my list of numbers

$self->{num} = []; # reference to an anonymous array
... #skip boring bit where the numbers are read in
#and check to be numbers
push @self->{num}, @input; #@input is the numbers

push( @{$self->{num}}, @input ); # add elements to that array

An anonymous array with a reference $ref to it is accessed as @{$ref}.
 
R

Ryan McCoskrie

Ryan McCoskrie wrote:

First of sorry for not replying to the right message
(news reader issues).

Thanks for that, I was sure there would be an answer
like that. I just haven't worked with perl enough
to know that sort of thing.
 
T

Tad J McClellan

Ryan McCoskrie said:
the perl interpreter won't accept this:

@self->{num} = []; #my list of numbers

push @self->{num}, @input; #@input is the numbers


perldoc perlreftut
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top