Home > Archive > PERL Beginners > March 2008 > List of hashes
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]
|
|
| Krzysztof Chodak 2008-03-16, 8:08 am |
| Hi, as it is only my second w of perl I would be grateful for some
help with hashes. I need to build a list of multidimensional hashes
one by one, sort it by one of fields and iterate through it later.
1. building a hashes
sub t {
return {
a => 1,
b => 2,
}
}
sub r {
return (
c => t(),
d => t(),
)
}
my %record = r();
my @records;
loop
#setting record fields/overwriting defaults
$record{fieldA}{fieldB} = value...
#pushing record on list
push @records, %record
#preparing new record
%record = r()
#sorting hashes list by one of hash fields - no idea how?...
....
#looping through list
for $rec (@records)
print $rec->{fieldA}{fieldB}
Big thanks in advance for helping with this idea / correcting mitakes.
BR
| |
| Yitzle 2008-03-16, 7:10 pm |
| Always start your code with:
use warnings;
use strict;
> my %record = r();
This line won't work. r() returns a reference (think pointer if you
used C) to a hash. You need a scalar to store it.
$record = r();
> $record{fieldA}{fieldB} = value...
This requires a hash, unlike the above line that used a hash. You can
create a hash and assign its reference to a scalar like so:
$ref = \%hash;
> push @records, %record
I think you want an array of references, not of hashed themselves.
I'll let someone who knows more correct me on this one ;)
__CODE__
$array[0] = %hash;
print $array[0];
__END__
It seems the assignment expects a scalar (makes sense) so the value
assigned to the array element is scalar(%hash), which is actually the
"number of buckets and used buckets" or something, ie not what you
want ;)
You definitely want to use a reference.
__CODE__
$array[0] = \%hash;
print $array[0];
__END__
Now you can do something like
print $array[0]->{field_one};
just like you did in your for loop - except you got no semicolon ;)
> #sorting hashes list by one of hash fields - no idea how?...
Take a look at the sort perldoc - the age example is what you want.
`perldoc sort`
or
http://perldoc.perl.org/functions/sort.html
HTH!
| |
| John W. Krahn 2008-03-16, 7:10 pm |
| yitzle wrote:
> Always start your code with:
> use warnings;
> use strict;
>
> This line won't work. r() returns a reference (think pointer if you
> used C) to a hash. You need a scalar to store it.
> $record = r();
Or dereference the reference returned from the sub:
my %record = %{ r() };
> This requires a hash, unlike the above line that used a hash. You can
> create a hash and assign its reference to a scalar like so:
> $ref = \%hash;
Or copy the hash to an anonymous hash and assign that reference:
$ref = { %hash };
> I think you want an array of references, not of hashed themselves.
Actually, the hash is converted to a list and that list is pushed onto
the array.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
| |
| Yitzle 2008-03-16, 7:10 pm |
| On Sun, Mar 16, 2008 at 12:54 PM, John W. Krahn <krahnj@telus.net> wrote:
>
> Actually, the hash is converted to a list and that list is pushed onto
> the array.
>
>
> John
Darn! I tested it using
$array[0] = %hash;
in which case it converts to a scalar, not in the list context of push... Sorry!
| |
| Krzysztof Chodak 2008-03-17, 8:05 am |
| On 16 Mar, 18:30, yit...@users.sourceforge.net (Yitzle) wrote:
> On Sun, Mar 16, 2008 at 12:54 PM, John W. Krahn <kra...@telus.net> wrote:
>
>
o[color=darkred]
>
>
> Darn! I tested it using
> $array[0] =3D %hash;
> in which case it converts to a scalar, not in the list context of push... =
Sorry!
Are subs always return references or only for anonymous lists/hashes?
| |
| Yitzle 2008-03-17, 7:08 pm |
| On Mon, Mar 17, 2008 at 7:12 AM, <Krzysztof.Chodak@gmail.com> wrote:
> Are subs always return references or only for anonymous lists/hashes?
perldoc perlsub
"The Perl model for function call and return values is simple: all
functions are passed as parameters one single flat list of scalars,
and all functions likewise return to their caller one single flat list
of scalars. Any arrays or hashes in these call and return lists will
collapse, losing their identities--but you may always use
pass-by-reference instead to avoid this. Both call and return lists
may contain as many or as few scalar elements as you'd like. "
perldoc perlref
A reference to an anonymous hash can be created using curly brackets:
$hashref = {
'Adam' => 'Eve',
'Clyde' => 'Bonnie',
};
So. Anonymous hashes create a reference. So its returning a ref. And
subs can't return hashes in any case. Just scalar lists.
|
|
|
|
|