Date: Sun, 9 Sep 2001 22:58:24 +1000 From: mgjv@tradingpost.com.au (Martien Verbruggen) Subject: Re: Arrays, References and indexes. Message-Id: On Sun, 09 Sep 2001 11:59:50 GMT, Sudhir Krishnan wrote: > i=file index > j=section index in file How do you distinguish between the 'sections'? > k=line (record) index in (file, section) > While iterating over the files, I'm storing it as: > > a[i][j][k] = $_; > > And it seems to work fine. > > Problems: > 1) I don't really understand the underlying implementation > with references. Have you read the documentation, specifically perlref, perllol and perldsc? They go into quite some detail about how perl references, and perl's complex data structures work.. > 2) I use a[i][j][k] for getting/setting values, its > 3 loops and is cumbersume. You will, most likely need three loops. However, you may not need them all explicitly. You could do something like: Read the line into a (lexically scoped) array, splitting it. Then use an assignment like: $a[i][j] = \@array; or, if the array needs to be reused, store a reference to a copy of the array: $a[i][j] = [@array]; Of course, you can do the same thing with the files, one level up. You'll still be using three iterations, just not as specific anymore, maybe. > 3) I'd like to know how I can get/set values without using indexes. > > i.e for a simple array: > instead of > for ($i=0; $i <=$#ARR; $i++) > > I'd like to use > for $val (@ARR) > > But in my case, $val will be a reference until I read the 3rd level, > right? I'm not entirely sure what you mean. $val, in this for loop, will always specifically be an alias (not the same as a reference, but similar) to the things you're iterating over. If there isn't anything yet to be an alias to, then how can this work? I mean, you're filling the data structure, right? So the data and the references aren't there yet... If these things that you work with are all numbered, what's wrong with using indexes? If you'd rather use push to add new data to the end, you could do something like (untested pseudo-code follows): my @files; foreach my $file (@files) { my @sections; # all the sections in a file my @section; # the records in a single section in a file open my $fh, $file or die $!; while (<$fh>) { if (line_looks_like_new_section) { push @sections, [@section]; @section = (); } else { my @records = split; push @section, \@records; } } push @array, \@sections; } ... unless I've misunderstood your structure. If I have, I'm sure you'll be able to get the idea and adapt it to your needs. as you can see, you can just store a reference to @sections and @records, since they will go out of scope immediately after you've stored them, and therefore won't be reused. @section, however, stays in scope, and therefore, you need to make a copy of the array, and store a reference to that, and empty the array after that. Martien PS. If you provide some real data next time, not much, but just enough, I might be tempted to write some real code. -- Martien Verbruggen | Since light travels faster than Interactive Media Division | sound, isn't that why some people Commercial Dynamics Pty. Ltd. | appear bright until you hear them NSW, Australia | speak?