Arrays of associative arrays in perl 4.0 
Author Message
 Arrays of associative arrays in perl 4.0

Hmmm, is this at all possible in 4.0, cause i have found some
references that it can be easily done in 5.0?  Or should i just
patch up a roundabout solution???

thanks,
brom

--
******************************************************

* NCOM Data Comms, NT Government, Australia          *
* 'The puppetmaster understands the need,            *
*  the jury needs to feel the greed'- The Church     *
******************************************************



Sat, 30 Jan 1999 03:00:00 GMT  
 Arrays of associative arrays in perl 4.0

: Hmmm, is this at all possible in 4.0, cause i have found some
: references that it can be easily done in 5.0?  Or should i just
: patch up a roundabout solution???

Yeah, dead easy: just concatenate the two keys together with something
you're never going to have in your first field, say ::::, and use
a single associative array with that joint field as the key. It'll behave in
the right way, just like the associative-hack for multidimensional [] numeric
arrays: $foo[3]{'tinyfrog'}[67] becomes, say, $foo{'3::::tinyfrog::::67'}

You can't interate the keyspace at all well, since you'll have to
 $id=3; for ( keys %foo ) { next unless /^$id::::/; &stuff() }
and you may even want to use eval so you can use /o on that, to speed it up
depending on circumstances.

Whether to upgrade to Perl 5 or not.. if you need this in a hurry, this will
probably be okay.  If you have the time, upgrade to Perl 5 and use that.


! Tim Baverstock, IP Network Admin !   http://www.mmm.co.uk [/~warwick]
`----------------------------------'   plan:"Level 1 RFC1149 compliance."



Sat, 30 Jan 1999 03:00:00 GMT  
 Arrays of associative arrays in perl 4.0



Quote:
>Hmmm, is this at all possible in 4.0, cause i have found some
>references that it can be easily done in 5.0?  Or should i just
>patch up a roundabout solution???

Nope, it can't be done in Perl 4. The only data type you can store in
an associative array in Perl 4 is a scalar.

You _can_ stick multiple fields in a scalar and split them on some
delimiter pattern, to emulate the storage of arrays. And in principle
you _could_ try to emulate arrays of hashes in perl 4 by (a) storing
the name of each hash as a value in an array, and (b) creating hashes
with the appropriate names at run-time using eval(). But that's a
pretty gross kluge.

Why not upgrade to perl 5?

--



Sat, 30 Jan 1999 03:00:00 GMT  
 Arrays of associative arrays in perl 4.0

Quote:

> Hmmm, is this at all possible in 4.0, cause i have found some

Contrary to what others have responded, this is possible using the
* reference operator. E.g.

$A[0] = "ara";
$A[1] = "arb";
$ara{"key1"} = "val1";
$arb{"key2"} = "val2";
# the following simulates $A[$ind]{$key}
sub Aindkey {

    local(*R)=$A[$ind];
    return $R{$key};

Quote:
}

print &Aindkey(0, "key1");
print &Aindkey(1, "key2");

In a similar way, you can make arbitrarily nested array and hash
structures. Just remember that * is a call-by-name operator.

olaf
--

__ o           <URL:http://www.inka.de/~bigred/>     <IRC:praetorius>
__/<_              >> Just as long as the wheels keep on turning round
_)>(_)______________ I will live for the groove 'til the sun goes down << ____



Tue, 02 Feb 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. associative array or associative arrays

2. Associative array of associative arrays

3. (Q) array of associative arrays

4. Associative array of associate arrays

5. Array of Associative Arrays?

6. Q: using arrays in an associative array

7. Array of associative arrays?

8. associative arrays of arrays

9. Help with sort routine and array of associative arrays

10. Associative array of arrays

11. Associative array containing arrays

12. Help on Arrays of Associative Arrays

 

 
Powered by phpBB® Forum Software