Home > Archive > PERL Beginners > August 2005 > reference trouble
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Tom Allison 2005-08-02, 4:59 pm |
| I'm trying to convert a hash of hashes to an array of hashes...
$hash ISA hash reference...
my $array;
while ( my ($k, $v) = each %$hash ) {
my $u = str2time($k); #convert to unix timestamp
$$array[$u] = $v;
}
result...
Segmentation Fault (core dumped)...
Do I need to predeclare $array = *array{ARRAY} ?
| |
| Tim Johnson 2005-08-02, 4:59 pm |
|
I think you need to explain a little better what you're trying to do
here, and what you EXPECT to happen. For one thing, why are you using
$u as your array index? Are you sure you don't want a hash?
-----Original Message-----
From: Tom Allison [mailto:tallison@tacocat.net]=20
Sent: Monday, August 01, 2005 5:07 PM
To: beginners perl
Subject: reference trouble
I'm trying to convert a hash of hashes to an array of hashes...
$hash ISA hash reference...
my $array;
while ( my ($k, $v) =3D each %$hash ) {
my $u =3D str2time($k); #convert to unix timestamp
$$array[$u] =3D $v;
}
| |
| Jeff 'japhy' Pinyan 2005-08-02, 4:59 pm |
| On Aug 1, Tom Allison said:
> I'm trying to convert a hash of hashes to an array of hashes...
>
> my $array;
> while ( my ($k, $v) = each %$hash ) {
> my $u = str2time($k); #convert to unix timestamp
> $$array[$u] = $v;
> }
>
> result...
> Segmentation Fault (core dumped)...
The problem is that $u, your array index, is an ENORMOUS integer. You
need to find some other way to index your array.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
| |
| Tom Allison 2005-08-02, 4:59 pm |
| Tim Johnson wrote:
>
> I think you need to explain a little better what you're trying to do
> here, and what you EXPECT to happen. For one thing, why are you using
> $u as your array index? Are you sure you don't want a hash?
>
>
Well, I was expecting to be able to store numbers in an array so that
the indexes would be in sort order. But the numbers prove to be too large.
I've changed it out to a hash and create a seperate array of keys that
are sorted:
my @keys = sort { $b <=> $a } keys %hash
And this works just fine.
But I still have some more learning to do on refs...
| |
| Tim Johnson 2005-08-02, 4:59 pm |
|
It looks like you've made a lot of headway on refs, don't sell yourself
short. It just looks like maybe you needed to back away from the
problem and see that the reason why it wasn't working was that you were
trying to stick a square peg in a round hole, so to speak. It's easy to
do when you're dealing with that level of abstraction, which is where
references trip a lot of people up.
Also, your question could have been a little clearer. Without giving
away any proprietary information, some background as to what you were
trying to do would have been nice. It looks like what you really wanted
was a way to store by a named index because then you would have a
pre-sorted list of indexes. Whenever you find yourself tempted to do
something like this, it might be worth thinking about whether a hash
might suit you better. Of course, your email was kind of cryptic, so
it's hard to tell.
-----Original Message-----
From: Tom Allison [mailto:tallison@tacocat.net]=20
Sent: Monday, August 01, 2005 7:03 PM
To: Tim Johnson
Cc: beginners perl
Subject: Re: reference trouble
[Tim Johnson] <snip>
I've changed it out to a hash and create a seperate array of keys that=20
are sorted:
my @keys =3D sort { $b <=3D> $a } keys %hash
And this works just fine.
But I still have some more learning to do on refs...
|
|
|
|
|