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

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