473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Accessing Array Elements of Data Structure

eWish
971 Expert 512MB
Let's say you have the following data structure.

Expand|Select|Wrap|Line Numbers
  1. $VAR1 = {
  2.           'xxx' => {
  3.                      'xxx' => {
  4.                                 'xxx' => [
  5.                                            'xxx',
  6.                                            'xxx',
  7.                                            'xxx',
  8.                                            'xxx',
  9.                                            'xxx'
  10.                                            ]
  11.                               }
  12.                    }
  13.         };
Say you want to access the array portion, is there a more elegant way of accessing the array data?

Expand|Select|Wrap|Line Numbers
  1. $product_data{$category}->{$sub_category}->{$product_name}[0];
--Kevin
Apr 12 '08 #1
2 1592
KevinADC
4,059 Expert 2GB
If that is all the data you have, that is the only way to access it initially. If $var is not a reference itself you can dispense with the arrow operator:
Expand|Select|Wrap|Line Numbers
  1. %hash = (
  2.    xxx => {
  3.       xxx => {
  4.          xxx => [qw(xxx xxx xxx)]
  5.       }
  6.    }
  7. );
  8.  
  9. print $hash{xxx}{xxx}{xxx}[0]
But you could have the last hash key point to an array:

Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2. my @yellow_pencils = qw(foo bar baz);
  3.  
  4. my %product_data = (
  5.    'pencils' => {
  6.        '#2' => {
  7.          'yellow' => \@yellow_pencils
  8.       }
  9.    }
  10. );
  11.  
  12. print Dumper \%product_data;
  13.  
Now if all you needed was the data in the array you could access the array and only use the hash when necessary. That is one thing about references, you can make your data almost as arbitrarily complicated as you want. How practical/impractical this approach is though is probably open to debate.
Apr 13 '08 #2
eWish
971 Expert 512MB
That is what I thought. I was just wondering if there was away I was not familiar with. As always thanks for your input!

--Kevin
Apr 13 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
0
by: harsha1305 | last post by:
Hi all, I need to create a pointer to array of structure. Definition of structure: typedef struct { char b_name; unsigned long int sig; unsigned long int count; volatile unsigned char...
38
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
6
by: Chuck Anderson | last post by:
My knowledge of JavaScript is limited. I learn from example and then adapt those examples to suit my needs. I have stumped myself on this one. I have a form with checkboxes that I want to...
15
by: DL | last post by:
say, we have the following: // declare an array myArray = ; // short hand? same as myArray = new Array ? // populate it myArray = 0; myArray = 0; myArray = 1; myArray = 0;
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.