simple array dropping first element

simple array dropping first element

am 07.11.2007 14:16:09 von jbl

This should be simple but I don't see it.

When the print statement outputs to the command window it is dropping
the first element (Bayer) and printing

ocean isle beach
Dominican Republic
ailey
tisdale
O'Malley
finley
zeff
brunner
rowan
Stella
jessie



use warnings;
use strict;
my @newWords;

while () {
@newWords = ;

foreach my $newWords (@newWords) {
print $newWords;
}
}

__DATA__
Bayer
ocean isle beach
Dominican Republic
ailey
tisdale
O'Malley
finley
zeff
brunner
rowan
Stella
jessie

Thanks
jbl

Re: simple array dropping first element

am 07.11.2007 14:26:48 von Peter Wyzl

"jbl" wrote in message
news:d3e3j3pb67lhrjbqb878i0hqdejumb1651@4ax.com...
> This should be simple but I don't see it.
>
> When the print statement outputs to the command window it is dropping
> the first element (Bayer) and printing
>
> ocean isle beach
> Dominican Republic
> ailey
> tisdale
> O'Malley
> finley
> zeff
> brunner
> rowan
> Stella
> jessie
>
>
>
> use warnings;
> use strict;
> my @newWords;
>
> while () {

Here you read 'Bayer' and do nothing with it....

> @newWords = ;

Here you read everything else into the array...

> foreach my $newWords (@newWords) {
> print $newWords;
> }
> }
>
> __DATA__
> Bayer
> ocean isle beach

--
P

Re: simple array dropping first element

am 07.11.2007 15:29:21 von Paul Lalli

On Nov 7, 8:16 am, jbl wrote:
> This should be simple but I don't see it.
>
> When the print statement outputs to the command window it is dropping
> the first element (Bayer) and printing

> use warnings;
> use strict;
> my @newWords;
>
> while () {

This starts a loop in which one item at a time is read from __DATA__
and stored in $_. The first time through the loop, it reads the first
item, and stores it in $_.

> @newWords = ;

This reads EVERYTHING that hasn't yet been read into @newWords.

> foreach my $newWords (@newWords) {
> print $newWords;
> }
>
> }

You're trying to combine two different ways of doing it. Either read
and process one line at a time, or read everything in at once and then
loop through them to process. You can't just munge them together.

Either:
while (my $newWord = ) {
print $newWord;
}

Or:
my @newWords = ;
foreach my $newWord (@newWords) {
print $newWord;
}

The first way is preferred, as there's no need to read everything into
memory at once.

Paul Lalli