LPM: record creation in perl
Steve Lane
sml at zfx.com
Tue Feb 6 09:45:21 CST 2001
Kenneth Rogers wrote:
> They've created a record '$record' which is an anonymous hash, they claim to be able to make a hash of these records for use
>
> #store record
> $byname{ $record->{NAME} } = $record;
this is an example of restructuring an already-built hash
into a more-usable form, and from your problem description,
probably not what you want.
> So then in order to make a bunch of these does this mean I need to have a $record that's separate for each one that I want to build? I'm hoping not I'm trying to make a dynamic system where I can create these records (the one's I'm using have 4 integers and an array that contains integers and characters right now). The problem I see right now is that I don't know how to just add a new record to '$byname' without completely creating the whole record first, I'd rather be able to say something li
>
> $byname{$record->{NUMBER} } = { NUMBER => 2,
> PARENT => 1, BOARD => @board };
>
> but I can't tell if that's legal ( it doesn't compile but that could be my fault and not that its impossible to do. )
it should compile. but it's not correct. hash values must be
scalars, and @board is not a scalar. you'd want to use "[ @board ]"
(a reference to an anonymous list) instead.
> Is there a better way to do this? Maybe a two dimensional array or somesuch, I can't figure out how to get the data I need packed into a 2D array right now though.
yes. you're not too far off. try:
$record{2} = { NUMBER => 2, PARENT => 1, BOARD => [ @board ] };
or, more generally (and formatted),
$record{$number} = {
NUMBER => $number,
PARENT => $parent,
BOARD => [ @board ],
};
since $record{$number}{BOARD} is a reference, you'll eventually
have to dereference it to get what was @board back out. here's
one way to do that:
@board = @{ $record{$number}{BOARD} };
good luck, Steve
--
Steve Lane <sml at zfx.com>
More information about the Lexington-pm
mailing list