Paul W. Roach III on 20 Jan 2011 12:10:21 -0800 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: [PLUG] perl question |
No, the array elements are hash references, not hashes themselves. SoOn Thu, Jan 20, 2011 at 02:44:37PM -0500, Eric at Lucii.org wrote:
> I have a data structure in perl:
>
> @output
>
> It is sparse (not all indexes have values.)
>
> Those that do look like this example which would be something like $output[5]:
> $VAR1 = {
> '128' => {
> 'salutation' => 'Dr.',
> 'area' => '800',
> 'spec_link' => '131',
> 'state' => 'PA',
> 'last_name' => 'Smith',
> 'dr_id' => '1',
> 'city' => 'Swarthmore',
> 'middle' => 'A',
> 'active' => undef,
> 'phone' => '555-1212',
> 'description' => 'some description goes here',
> 'sex' => 'M',
> 'address2' => '',
> 'loc_no' => '1',
> 'zip' => '19067',
> 'title' => 'DMD',
> 'address1' => '123 Any Street.',
> 'first_name' => 'John'
> }
> };
>
> When I put data in the element I do it like this:
>
> $var1 = 5 ; $var2 = 128 ;
> $output[$var1]->{$var2}->{'description'} = "some description goes here" ;
>
> To get the data out I walk through the array, skipping indexes where there is no
> data. Once I find an array element with data I want to access the elements.
> Currently, the only way I can get to it is to use Dumper to produce the above
> print out.
>
> How do I find out what the {'128'} value is so that I can address the elements?
>
> I tried this:
>
> for ( $x = 0; $x < $#output; $x++ ) {
> next if not defined $output[$x] ;
> $hashKey = keys $output[$x] ;
> print "record: $x has last name of: "
> . $output[$x]->{$hashKey}->{'last_name'} ;
> }
>
> ... but perl barfs on the use of keys
> Type of arg 1 to keys must be hash (not array element) at ./build_fast_doc.pl
> line 229, near "] ;"
>
> What am I missing here? I thought the array elements here ARE hashes.
you have to dereference it like so:
keys %{$output[$x]}
But your code would still have a problem, because keys returns a list,
not a scalar. So if you wanted to save all the keys you'd need to do
something like
my @hashKeys = keys %{$output[$x]};
Oh, and your for loop has a bug as well. If you have an array @a,
then $#a is the index of the last element in the array. Since you
have < instead of <=, you're going to miss the final element. You
hardly ever need to use C-style for loops in Perl, especially when
you're going through one element at a time. I'd probably write the
loop as
for my $x (0..$#output)
Walt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
iD8DBQFNOJSEXfGeK2entYQRAknvAJ4xaQfW5t1LxyVlbbzFefugHv/j0gCdEfXQ
84gir4tyI0J0HdNJcqaCCR4=
=fJui
-----END PGP SIGNATURE-----
___________________________________________________________________________
Philadelphia Linux Users Group -- http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion -- http://lists.phillylinux.org/mailman/listinfo/plug
___________________________________________________________________________ Philadelphia Linux Users Group -- http://www.phillylinux.org Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce General Discussion -- http://lists.phillylinux.org/mailman/listinfo/plug