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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie: Multidimensional array 3

Status
Not open for further replies.

AlbertAguirre

Programmer
Nov 21, 2001
273
0
0
US
This seems so simple but I dont understand it.

I want to print ONE element of a multi dimensional array.

Here are my base arrays:
@aTemp1 = ('albert', 'daniel', 'nathan');
@aTemp2 = ('danielle', 'mylee', 'rob');

Here is my multidimensional array:
@aTest = (@aTemp1, @aTemp2);

I want to print the value: 'daniel' from md array @aTest

How?

This doesnt work: print $aTest[0][1];



 
Code:
[gray][i]# Here are my base arrays:[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@aTemp1[/blue] = [red]([/red][red]'[/red][purple]albert[/purple][red]'[/red], [red]'[/red][purple]daniel[/purple][red]'[/red], [red]'[/red][purple]nathan[/purple][red]'[/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]@aTemp2[/blue] = [red]([/red][red]'[/red][purple]danielle[/purple][red]'[/red], [red]'[/red][purple]mylee[/purple][red]'[/red], [red]'[/red][purple]rob[/purple][red]'[/red][red])[/red][red];[/red]

[gray][i]# Here is my multidimensional array:[/i][/gray]
[black][b]my[/b][/black] [blue]@aTest[/blue]  = [red]([/red]\[blue]@aTemp1[/blue], \[blue]@aTemp2[/blue][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$aTest[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red][[/red][fuchsia]1[/fuchsia][red]][/red][red];[/red] [gray][i]# outputs 'daniel[/i][/gray]

Or a simplified definition:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@aTest[/blue] = [red]([/red]
	[red][[/red][red]qw([/red][purple]albert daniel nathan[/purple][red])[/red][red]][/red],
	[red][[/red][red]qw([/red][purple]danielle mylee rob[/purple][red])[/red][red]][/red],
[red])[/red][red];[/red]

Read the following perldoc pages:
[ol]
[li]perllol - Manipulating Arrays of Arrays in Perl[/li]
[li]perldsc - Perl Data Structures Cookbook[/li]
[/ol]

- Miller
 
Here is my multidimensional array:
@aTest = (@aTemp1, @aTemp2);

That is not a multi-dimensional array, it's just an regular array. See Millers post above for the correct way to create an array of arrays.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ok well how do I create a nested loop to itterate through the values?
(BTW I am a PHP guy...)

# MY CODE
my @aTest = (
[ 'albert', 'daniel', 'nathan' ],
[ 'danielle', 'mylee', 'rob' ],
);


# This does not work:
foreach $row (@aTest){
foreach $col($row){
print "$col \n";
}
print "----- \n";
}

should generate:
albert
daniel
nathan
------
danielle
mylee
rob

Help?
 
Very close. In the code you posted, $row is a reference to an array, so you need to dereference it by putting '@' before it, like so:
Code:
foreach $row (@aTest){
  foreach $col(@$row){
    print "$col \n";
  }
  print "----- \n";
}
 
$row is an array reference. I once again suggest that you read perllol.

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@aTest[/blue] = [red]([/red]
	[red][[/red][red]qw([/red][purple]albert daniel nathan[/purple][red])[/red][red]][/red],
	[red][[/red][red]qw([/red][purple]danielle mylee rob[/purple][red])[/red][red]][/red],
[red])[/red][red];[/red]

[gray][i]# This does not work:  [/i][/gray]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$row[/blue] [red]([/red][blue]@aTest[/blue][red])[/red][red]{[/red]
	[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$col[/blue] [red]([/red][blue]@$row[/blue][red])[/red][red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$col[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
	[black][b]print[/b][/black] [red]"[/red][purple]----- [purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

- Miller
 
ishnid thank you...

MillerH, i scanned through the doc but it will take me hours to find what I need.
I already understand multidimensional arrays, I just need to know the perl syntax for it.

ishnid gave me the one character I needed "@"

Thanx!
 
ok how do I append a value to one element of the array?

CODE
my @aTest = (
[ 'albert', 'daniel', 'nathan' ],
[ 'danielle', 'mylee', 'rob' ],
);

#I want to add 'paul' ot @aTest[0]

foreach $row (@aTest){
foreach $col(@$row){
print "$col \n";
}
print "----- $row[1] \n";
}

----------------------------------------------

So the result should be:
albert
daniel
nathan
paul
------
danielle
mylee
rob


How do I do that?
 
Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@aTest[/blue] = [red]([/red]
    [red][[/red][red]qw([/red][purple]albert daniel nathan[/purple][red])[/red][red]][/red],
    [red][[/red][red]qw([/red][purple]danielle mylee rob[/purple][red])[/red][red]][/red],
[red])[/red][red];[/red]

[gray][i]# Append Paul to first row[/i][/gray]
[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$aTest[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red], [red]'[/red][purple]paul[/purple][red]'[/red][red];[/red]

[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$row[/blue] [red]([/red][blue]@aTest[/blue][red])[/red] [red]{[/red]
	[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$col[/blue] [red]([/red][blue]@$row[/blue][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$col[/blue] [purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
	[black][b]print[/b][/black] [red]"[/red][purple]----- [blue]$row[/blue][1]  [purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

- M
 
The article Miller linked you to is maybe 20 minutes of reading for a very slow reader:


It is certainly not hours of reading, not even for me, and I'm about 70% blind. So give it a read when you have 15 minutes.

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

Part and Inventory Search

Sponsor

Back
Top