[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Hash of arrays of arrays...aaaargh.



Hello,

I'm trying to build what I thought would be a fairly simple data structure:
a hash of arrays of arrays. I want to read in a tab delimitted text file a
bit like this:

cat1	item1	price1	desc1
cat1	item2	price2	desc2
cat2	item1	price1	desc1
cat2	item2	price2	desc2
...


Each line becomes an array of length 4; each array is added to another
array until the category changes (eg, from 'cat1' to 'cat2'), at which
point, this array is added to a hash with a key equalling the category name
(eg, 'cat1').

Simple, I thought. And simple was certainly the way I felt by 4am, having
failed miserably at every turn. Reading the perlref man page only made me
feel more stupid. It must be the way it's written (ahem).

I present below the ugly spaghetti-like mess I've arrived at. It works,
clunkilly, up to a point; the first big problem is the line:

@catalogue{$currentCat} = \@$temp;

which only picks up the last category array (eg, 'cat2')

the second big problem is my trying to retrieve the arrays out of the hash,
and iterate through them.

I'd be extremely grateful if anybody could give me any pointers.

Sorry for my cotton-wool-headed explanations...

Alex

**********************************update.pl****************

open (UPDATE, 'update.txt');

@tempArray = ();
$currentCat = "";
$j = 0;
$w = 0;

while (<UPDATE>) {

	chomp;
	$count = "thisEntry$w$j";
	@$count = split /\t/;
	$temp = "temp$w";
	$currentCat = $$count[0] if $currentCat eq "";
	$$temp[$j] =  \@$count;
	if ($$count[0] eq $currentCat) {
		$j ++;
		next;
		}


	@catalogue{$currentCat} = \@$temp;
	$j = 0;
	$w ++;
	$currentCat = $$count[0]
}


@catalogue{$currentCat} = \@$temp;

$alex = "";


@finally = values %catalogue;
$finalLen = @finally;

for ($f = 0; $f < $finalLen; $f ++) {

	@innerArray = $finally[$f];
	$innerLen = @innerArray;
	for ($in = 0;$in <= $innerLen; $in ++) {
		$hey = $innerArray[0];
		@heyArray = $innerArray[0];
		$alex .= "$innerArray[$in][0]    $innerArray[$in][1]
$innerArray[$in][2]    $innerArray[$in][3] endOfForeach\n";
		print "$innerArray[$in][0]    $innerArray[$in][1]
$innerArray[$in][2]    $innerArray[$in][3] endOfForeach
		";
	}
}




***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch