Home > Archive > PERL Beginners > October 2006 > Printing an array within a hash
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]
| Author |
Printing an array within a hash
|
|
| Reginald Johnson 2006-10-02, 6:59 pm |
| I am trying to print the contents of the array from within the hash. I
see that I can do it by
print "$name: $items[0][0] $items[0][1] $items[0][2] $items[0][3] \n "
Is there a better way to accomplish this, especially if I don't know the
number of items in the array.
Here is the code:
while (( my $name, my @items) = each(%all)) {
my $len = @items;
print "this is len=>$len\n";
# print "$name: $items[0][0] $items[0][1] $items[0][2]
$items[0][3] $items[0][@items] \n ";
print Dumper(@items);
}
Entire code:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
my @skipper = qw ( blue_shirt hat preserver sunscreen);
my @professor = qw(sunscreen water_bottle slide_rule batteries
radio);
my %all = (
"Gilligan" => \@gilligan,
"Skipper" => \@skipper,
"Professor" => \@professor,
);
warn Data::Dumper::Dumper (\%all);
check_items_for_all(\%all);
sub check_items_for_all{
my $all = shift;
for my $person(sort keys %$all) {
check_items_required($person, $all->{$person});
} #end for
} #end check_items_for_all
sub check_items_required {
my $who = shift;
my $items = shift;
my @missing = ();
my @required = qw(preserver sunscreen water_bottle
jacket);
for my $item (@required) {
unless (grep $item eq $_, @$items) { #if
statement is false
print "$who is missing $item\n";
push @missing, $item;
} #end unless
} #end for
if (@missing) {
push @$items, @missing;
print "$who now has @$items\n\n";
} #end if
} #end sub
while (( my $name, my @items) = each(%all)) {
my $len = @items;
print "this is len=>$len\n";
# print "$name: $items[0][0] $items[0][1] $items[0][2]
$items[0][3] \n ";
print Dumper(@items); # I want to print the contents of
dumper
}
foreach my $line (@gilligan) {
print "Gilligan: $line\n";
}
| |
| usenet@DavidFilmer.com 2006-10-02, 6:59 pm |
| Reginald Johnson wrote:
> print "$name: $items[0][0] $items[0][1] $items[0][2] $items[0][3] \n "
>
> Is there a better way to accomplish this, especially if I don't know the
> number of items in the array.
print join( ' ', @{$items[0]} ); #untested
--
David Filmer (http://DavidFilmer.com)
| |
| John W. Krahn 2006-10-02, 6:59 pm |
| Johnson, Reginald (GTI) wrote:
> I am trying to print the contents of the array from within the hash. I
> see that I can do it by
>
> print "$name: $items[0][0] $items[0][1] $items[0][2] $items[0][3] \n "
>
> Is there a better way to accomplish this, especially if I don't know the
> number of items in the array.
>
> Here is the code:
> while (( my $name, my @items) = each(%all)) {
Hash keys and values are scalars not arrays, although you can store a
reference to an array in a scalar:
while ( my ( $name, $items ) = each %all ) {
> my $len = @items;
And then dereference the array:
my $len = @$items;
> print "this is len=>$len\n";
> # print "$name: $items[0][0] $items[0][1] $items[0][2]
> $items[0][3] $items[0][@items] \n ";
And dereference the elements:
# print "$name: $items->[0] $items->[1] $items->[2] $items->[3]\n";
Or use an array slice:
# print "$name: @$items[0..3]\n";
> print Dumper(@items);
print Dumper $items;
> }
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
|
|
|
|
|