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

accessing elements of a multidimentional array 5

Status
Not open for further replies.

jimberger

Programmer
Joined
Jul 5, 2001
Messages
222
Location
GB
hi all,

I have a a 6-d array which is populated as follows in a loop:

push(@ReportData, $Type, $Year, $Link, $ReportNo, $Authors,$Description);

I then want to access all the $ReportNo elements in a for each loop like so:

foreach $x (@ReportData)
{
print "$x";
}

However this prints out the enitre contents of the array. how do i program it to only print out the $reportNo elements of the array?

any help is muchly appreicated

thanks
 
6 dimensions? Why not try a hash? Can you even think in 6 dimensions? I get screwy in 3.

Mike
 
Actually what you've got there isn't a 6D array. What you've got is an array that contains:
$Type
$Year
$Link
$ReportNo
$Authors
$Description
$Type
$Year
$Link
$ReportNo
$Authors
$Description
$Type
$Year
$Link
$ReportNo
$Authors
$Description
...
What you probably want to do is create a TWO dimensional array, where each element in the first array is a reference to an anonymous 6 ELEMENT array containing the list of values for each entry. To do that you can modify your first line like this:
Code:
push(@ReportData, [$Type, $Year, $Link, $ReportNo, $Authors,$Description]);
Note the square brackets around the list, making it an anonymous array, and push a reference to it onto the @ReportData array. To get at the data do this:
Code:
foreach $ref (@ReportData) {
   print $ref->[3]; #ReportNo is the 4th element in the list
}
Or, as motte said, you could use a hash, where the key is the ReportNo and the value is, again, a reference to an anonymous array. I'll post how to do that too, if you want.

Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
this is good stuff tracy - i'm new to perl so i'm just learning. How would i do this as a associative array then? I understand i would define the hash as follows:

%hashtable = ($reportNo, [$type, $Year, $Link, $Authors, $Description]);

but then how do i access, for example the variable link in this structure?

cheers tracey - i would vote for you for tipmaster of the week but i dont know how to...

 
You mean you want to get the value of 3rd in your array?

$ReportNo = $ReportData[3];

the first element is 0 and so on...

You can check this by bellow script:
$i=0;
foreach $element (@ReportData) {
print &quot;$i : $ReportData[$i] : $element <br>\n&quot;;
}
 
When starting with

%hashtable = ($reportNo, [$type, $Year, $Link, $Authors, $Description]);

I believe you would get the value of variable $Link by doing

$link = $hashtable{$reportNo}[2];

but I'm not 100% sure.

***************************************************************

I'm going to take a wild guess at this and say that what I think you are trying to do is create a hash where $reportNo is the key, and with that key you want to be able to reach variables $type, $Year, $Link, $Authors, and $Description. If this is correct, you could do that like this:

%hash = ($reportNo => {
'Type' => $Type,
'Year' => $Year,
'Link' => $Link,
'Authors' => $Authors,
'Desc' => $Description
}
);

if you build your hash that way, then for a specific $reportNo you can retrieve the corresponding $Link variable by doing this:

$Link = $hash{$reportNo}{'Link'};

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
In order to vote for Tracy, click the link int he lower left
that says 'let tsdragon know the post was helpful'.

In order to access an element in your hash of hashes,
-- A very simple example. --

Code:
#!perl
# Given these hashes:
%vw = ('color'=>'green',model=>'GTI',year=>'2001');
%volvo = ('color'=>'white',model=>'740',year=>'2002');
%mercedes = ('color'=>'red',model=>'SLK',year=>'2000');

# Put them in another hash.
$cars{vw} = { %vw };
$cars{volvo} = { %volvo };
$cars{mercedes} = { %mercedes };

# What color is the volvo?
$vehicle = $volvo;
print &quot;The volvo is $cars{volvo}{color}.\n&quot;;                         
#                           |      |
#                           |     inner hash
#                          Outer hash

# What year is the vw?
print &quot;The vw was built in $cars{vw}{year}\n&quot;;

# What model is the mercedes?
print &quot;The mercedes is a $cars{mercedes}{model}.\n&quot;;

This is probably over simplified and would need to be
abstracted a little to make it useful.

HTH


keep the rudder amid ship and beware the odd typo
 
This is probably the best thread I've seen on arrays of arrays, hashes of arrays, and hashes of hashes! Great work all!

goBoating: Thanks for the plug!
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top