Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Array that is declared inside a Struct ?

Status
Not open for further replies.

CristianLuca

Programmer
Oct 25, 2007
36
RO
struct grades = {
math => '@';
};

$grades->{math} = (1,2,3); # why can't i do that ? How can this be done ?

my @array = $grades->{math}; # why is this not taking grades->math elements and copies them into array ? How can this be done ?

Than you in advance,
CL

 
You cannot assign an array to the value of a hash. What you can do is assign a reference to an array to the value of a hash.

Code:
[blue]$grades[/blue]->[red]{[/red]math[red]}[/red] = [red][[/red][fuchsia]1[/fuchsia],[fuchsia]2[/fuchsia],[fuchsia]3[/fuchsia][red]][/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$grades[/blue]->[red]{[/red]math[red]}[/red][red][[/red][fuchsia]1[/fuchsia][red]][/red][red];[/red] [gray][i]# Outputs 2[/i][/gray]

[gray][i]# Copy Array[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@array[/blue] = [blue]@[/blue][red]{[/red][blue]$grades[/blue]->[red]{[/red]math[red]}[/red][red]}[/red][red];[/red]

To learn more about complex data structures look here:


- Miller
 
Just guessing:

Code:
my @array = @{$grades->{math}};



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
oops... Miller correctly pointed out you need to use square brackets around the array to create a reference and assign the value to the hash key.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top