Rajesh Dorairajan wrote:

> I've a class (blessed, of course :)) that has variables like:
>
> $self->a = "1";
> $self->b = "2";
> $self->c = [ '1', '2', '3', '4' ];
>
> Now, I want to write a foreach loop to iterate through $self->c and print
> the values. However:
>
> foreach my $foo ( $self->c ) {
>         print $foo;
> }
>
> gets me the whole list (1, 2, 3, 4) in $foo instead of one-by-one. What I am
> doing wrong? I am sure I am missing something very basic here. I would
> appreaciate your responses.
>
> Thanks

Are you sure that is the code for which you got output?  I tried it verbatim:

$self->a = "1";
$self->b = "2";
$self->c = [ '1', '2', '3', '4' ];

foreach my $foo ( $self->c ) {
        print $foo;
}
^Z
Can't call method "a" on an undefined value at - line 1.

The compiler assumes that the barewords are method names.

OTOH, when I tried what you seem to mean, adding a newline after each element, I
got much better results:

Greetings! E:\d_drive\perlStuff>perl -w
$self->{a} = '1';
$self->{b} = '2';
$self->{c} = ['1', '2', '3', '4'];

for my $foo (@{$self->{c}}) {
        print "$foo\n";
}
^Z
1
2
3
4

Is that what you were looking for?

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to