Hash of arrays of arrays--newbie has ponweed for brains 
Author Message
 Hash of arrays of arrays--newbie has ponweed for brains

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:

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

open (UPDATE, 'update.txt');

$currentCat = "";
$j = 0;
$w = 0;

while (<UPDATE>) {

        chomp;
        $count = "thisEntry$w$j";

        $temp = "temp$w";
        $currentCat = $$count[0] if $currentCat eq "";

        if ($$count[0] eq $currentCat) {
                $j ++;
                next;
                }


        $j = 0;
        $w ++;
        $currentCat = $$count[0]

Quote:
}


$alex = "";


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



        for ($in = 0;$in <= $innerLen; $in ++) {
                $hey = $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
                ";
        }

Quote:
}



Tue, 11 Jan 2000 03:00:00 GMT  
 Hash of arrays of arrays--newbie has ponweed for brains

Quote:

> 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:


> 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

> open (UPDATE, 'update.txt');


> $currentCat = "";
> $j = 0;
> $w = 0;

> while (<UPDATE>) {

>         chomp;
>         $count = "thisEntry$w$j";

>         $temp = "temp$w";
>         $currentCat = $$count[0] if $currentCat eq "";

>         if ($$count[0] eq $currentCat) {
>                 $j ++;
>                 next;
>                 }


>         $j = 0;
>         $w ++;
>         $currentCat = $$count[0]
> }


> $alex = "";



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



>         for ($in = 0;$in <= $innerLen; $in ++) {
>                 $hey = $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
>                 ";
>         }
> }

  Read the perllol manpage, it has some great examples, if you are still
stuck after reading that then let me know.

Simon



Tue, 11 Jan 2000 03:00:00 GMT  
 Hash of arrays of arrays--newbie has ponweed for brains


Quote:

>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').

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

usually this is because you end up with lots of references to the same
array, whose contents are updated, one way around this is to make an
anonymous copy of the array and get a reference to that e.g.


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

One thing you can do is make each key in %catalog be the category name and
the value be a reference to a list of lists.  each of the list of lists
cont ains tuples [item, price, desc] e.g.

#!/usr/local/bin/perl  -w

while (<DATA>) {


Quote:
}

foreach $category (sort keys %catalog) {
  print "Category $category:\n";

    print "  Item $item->[0]:\n";
    print "    $item->[1], $item->[2]\n";
  }
Quote:
}

__END__
cat1    item1.1   price1.1  desc1.1
cat1    item1.2   price1.2  desc1.2
cat2    item2.1   price2.1  desc2.1
cat2    item2.2   price2.2  desc2.2

will build the following data structure in %catalog:

%catalog = (
   'cat1' => ARRAY(0x805e2c0)
      0  ARRAY(0x81109a0)
         0  'item1.1'
         1  'price1.1'
         2  'desc1.1'
      1  ARRAY(0x8114f28)
         0  'item1.2'
         1  'price1.2'
         2  'desc1.2'
   'cat2' => ARRAY(0x8114f88)
      0  ARRAY(0x8114f1c)
         0  'item2.1'
         1  'price2.1'
         2  'desc2.1'
      1  ARRAY(0x8114fc4)
         0  'item2.2'
         1  'price2.2'
         2  'desc2.2'
)

and print it out.  

Hope this helps,

Mike

--

http://www.stok.co.uk/~mike/       |   PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.tiac.net/users/stok/    |                   65 F3 3F 1D 27 22 B7 41



Tue, 11 Jan 2000 03:00:00 GMT  
 Hash of arrays of arrays--newbie has ponweed for brains

Quote:

> 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:



There are two mistakes here (you really need to read the camel book,
see the Perl FAQ for details.



           list. You take a reference to that list, which is redundant
           since, you already had one in $temp.

Let us, for the moment, ignore your original code, and solve the
problem at hand:

        #!/usr/bin/perl -w
        use strict;
        my %list = ();
        while(<>) {
                chop;


        }

That's it.

Ok, so you ask, what does that line-noise that starts with push do?

Let's take it apart one piece at a time:


This pushes a list-reference (anonymous) onto a list.

%list is a hash, and we're expecting $list{something} to name or be a
reference to a list-reference.

Make sense?

When you want to look something up in this structure:

        $list{cat1}[0][1]

Would be the second field of the first line that began with "cat1".

To be more versbose, you could say:

        $list{cat1}->[0]->[1]

                        -AJS



Tue, 11 Jan 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. URGENT: Hash in Hash, Array in Array, Array in Hash, Hash in Array

2. hash of values and array of hashes of values and array of hashes

3. Having trouble feeding an array into the keys of a hash

4. array/hash to file to array/hash

5. Simple Perl5 question (hash-array of hash-array refs)

6. Newbie question about hashes (and/or arrays)

7. hashed array of arrays

8. array into hash array with qw

9. hash of arrays using variable array names

10. a hash of arrays of arrays

11. Creating hash using array (newbie)

12. Adding values of an array to an array in a hash

 

 
Powered by phpBB® Forum Software