Title: RE: [Perl-unix-users] not understanding how to bless or should I even in this case

Kester, I used your suggestions. The following code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#! /usr/local/bin/perl

use Data::Dumper;
my @PERSON;

push @PERSON,
  new_person(name   => "JOE",
             nums   => [ "25","30","1" ],
             weight => "150"),
  new_person(name   => "DAVE",
             nums   => [ "29","52","46" ],
             weight => "190"),
  new_person(name   => "CAROL",
             nums   => [ "22","32" ],
             weight => '120');


if (grep { $_->{name} eq "DAVE" } @PERSON) {
 print "DAVE exists\n";
}
else {
 print "DAVE does not exist\n";
}

foreach my $people (@PERSON) {
    print "52 exists" if grep { $_ eq '52' } @{$people{nums}};
    print "Im looping.\n";
    print join (' ',@{$people{nums}});
    print @{$people{nums}};
}

print Dumper(@PERSON);

sub new_person {
  my %p = @_;
  my %r_person;
  my @fields = qw/name nums weight/;
  @r_person{ @fields } = @p{ @fields };
  return \ %r_person;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
produces the following output:

DAVE exists
Im looping.
Im looping.
Im looping.
$VAR1 = {
          'weight' => '150',
          'name' => 'JOE',
          'nums' => [
                      '25',
                      '30',
                      '1'
                    ]
        };
$VAR2 = {
          'weight' => '190',
          'name' => 'DAVE',
          'nums' => [
                      '29',
                      '52',
                      '46'
                    ]
        };
$VAR3 = {
          'weight' => '120',
          'name' => 'CAROL',
          'nums' => [
                      '22',
                      '32'
                    ]
        };

Im still stuck on why neither logic against the values or trying to print
the array values is working.  I'm reading over perltoot as we speak,
but I'm curious if OO is going to be required to solve this baseline
issue that I have here. Sorry to drag this out so long, but this seems
like it should work.





-----Original Message-----
From: Kester Allen [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, May 28, 2003 4:46 PM
To: Markham, Richard
Subject: Re: [Perl-unix-users] not understanding how to bless or should
I even in this case



Use Data::Dumper to examine your data structures.  When you
call new_person like this:

>   new_person(name   => "JOE",
>              nums   => "25","30","1",
>                  weight => "150"),
> ...
>
> sub new_person {
>   my %p = @_;
> ...

%p now has keys "name", "nums", "30", and "weight", and the values
"JOE", "25", "1", and "150; respectively.  Key/Value assignment is one-to-one,
and the => is just a synonym for the comma, so %p thinks that "30" is new key
and "1" is its value.

You need something like:

new_person(
    name   => "JOE",
    nums   => [ "25","30","1" ],
    weight => "150"
),

then $p{nums} in new_person will be reference to an anonymous array, and
you can read out with something like:

foreach my $people (@PERSON) {
    foreach my $num ( @{$people{nums}} ) {
        print "52 exists" if $num eq '52';
    }
}

or better:

foreach my $people (@PERSON) {
    print "52 exists" if grep { $_ eq '52' } @{$people{nums}};
}


Also, try reading perldoc perltoot to set this up as an OO
script.

--Kester



> This message is in MIME format. Since your mail reader does not understand
> this format, some or all of this message may not be legible.
>
> ------_=_NextPart_001_01C32557.B693FC40
> Content-Type: text/plain;
>       charset="iso-8859-1"
>
> ok no responses.
> can anyone tell me how I could get each value for nums so I can run
> statements against their values?
>
> for starters i tried
>  foreach $people (@PERSON) {
>   print "$people{nums}"
>  }
> which is only printing the first value so i tried throwing an [] field
> indicator around there and it would give syntax error....
>
> -----Original Message-----
> From: Markham, Richard
> Sent: Tuesday, May 27, 2003 10:55 AM
> To: '[EMAIL PROTECTED]'
> Subject: RE: [Perl-unix-users] not understanding how to bless or should
> I even in this case
>
>
> This worked well.  Thanks for the pointer.
>
> If you have time. In the following example age is replaced with a list of
> numbers, so in regards to grep, can it iterate over and element which is an
> array itself.  (I think I am asking this right).  Of course the code below
> for grep'n 52 fails.  Thanks for any additional info.
>
> #! /usr/local/bin/perl
>
> my @PERSON;
>
> push @PERSON,
>   new_person(name   => "JOE",
>              nums    => "25","30","1",
>            weight => "150"),
>   new_person(name   => "DAVE",
>              nums   => "29","52","46",
>            weight => "190"),
>   new_person(name   => "CAROL",
>              nums   => "22","31",
>            weight => "120");    

>
> if (grep { $_->{name} eq "DAVE" } @PERSON) {
>  print "DAVE exists";
> }
> else {
>  print "DAVE does not exist";
> }
>
> if (grep { $_->{nums} eq "52" } @PERSON) {
>  print "52 exists";
> }
> else {
>  print "52 does not exist";
> }
>
>
> sub new_person {
>  my %p = @_;
>   my %r_person;
>   my @fields = qw/name nums weight/;
>   @r_person{ @fields } = @p{ @fields};
>   return \ %r_person;
> }
>
> -----Original Message-----
> From: Bayard Bell [mailto:[EMAIL PROTECTED]]
> Sent: Friday, May 23, 2003 6:28 PM
> To: Markham, Richard
> Cc: '[EMAIL PROTECTED]'
> Subject: Re: [Perl-unix-users] not understanding how to bless or should
> I even in this case
>
>
> perl internal type reference need to be explicitly dereferenced.  The
> syntax you are using is for an object method call.  The line should be
> re-written as:
>
> if (grep { $_->{name} eq "DAVE" } @PERSON) {
>
> Of course, if/else that you've written could be made a bit more concise:
>
> print eval { (grep{ $_->{name} eq "DAVE" } @PERSON)
>     ? "Found DAVE" : "DAVE's gone missing" }, "\n";
>
> Markham, Richard wrote:
>
> >
> > error: Can't call method "name" on unblessed reference at kk line 17.
> >
> > #! /usr/local/bin/perl
> >
> > my @PERSON;
> >
> > push @PERSON,
> >   new_person(name   => "JOE",
> >              age    => "25",
> >                weight => "150"),
> >   new_person(name   => "DAVE",
> >              age    => "29",
> >                weight => "190"),
> >   new_person(name   => "CAROL",
> >              age    => "22",
> >                weight => "120");   
> > 
> >
> > if (grep { $_->name eq "DAVE" } @PERSON) {
> >  print "exists";
> > }
> > else {
> >  print "does not exist";
> > }
> >
> >
> > sub new_person {
> >  my %p = @_;
> >   my %r_person;
> >   my @fields = qw/name age weight/;
> >   @r_person{ @fields } = @p{ @fields};
> >   return \ %r_person;
> > }
> >
>

Reply via email to