Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Perl / Getting Started / March 2009



Tip: Looking for answers? Try searching our database.

Can't Retrieve Data From foreach block

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thomas H. George - 30 Mar 2009 15:07 GMT
How can I retrieve data loaded into an array within a foreach block?
The array is defined outside the foreach block and is not the indexing
array of the foreach loop.

I have defined three arrays

my @lines = ();
my @line = ();
my @lastnames = ();

loaded into @lines a file in which each line is a series of tab
separated items and run the following code using warnings and strict

foreach (@lines) {
    @line = split /\t/, $_;
    push @lastnames, $line[2];
}

This works perfectly BUT when I try to uses @lastnames I get a warning
that it is undefined.  Specifically, I continued

@lastnames = sort @lastnames;

my $dup = "";
foreach (@lastnames) {
    if ($dup ne $_) {
        print $_;
        $dup = $_;
    }
}

and still everything works but there is a warning the $_ is undefined in
the condition statement.

I don't understand why it should not be possible to modify a global
variable inside the foreach loop.  In the second part of the problem I
modified the code as follows

my $dup = "";
my $item = "NoName";
foreach (@lastnames) {
    $item = $_;
    if ($dup ne $item) {
        print $item;
        $dup = $item;
    }
}

reasoning that foreach might be treating $_ like a numeral though it
actually holds current name from the array @lastnames. Now I received a
warning that $item was undefined in the conditional statement.

I hope there is a straightforward way of correctly doing what this code
does and I realy would like to understand why it is incorrect.  

Tom

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Alexander Koenig - 30 Mar 2009 15:37 GMT
Hi,

You wrote on 03/30/2009 04:07 PM:
> How can I retrieve data loaded into an array within a foreach block?
> The array is defined outside the foreach block and is not the indexing
> array of the foreach loop.

I ran your code and it works fine here.
I did however have the same problem as you at first, so maybe you made
the same mistake I did at first.

> foreach (@lines) {
>     @line = split /\t/, $_;
>     push @lastnames, $line[2];
> }

Are you sure that the split (always) produces an array with three
elements? In my test array it only had two a la "alex\tkoenig", which
resulted in the error messages you described.

To test this you could insert a print line after the split. Something
like this.

print "$line[0]\t$line[1]\t$line[2]\n";

bye
Alex

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Gunnar Hjalmarsson - 30 Mar 2009 16:22 GMT
> How can I retrieve data loaded into an array within a foreach block?
> The array is defined outside the foreach block and is not the indexing
> array of the foreach loop.

I was about to ask the same question as Alex did.

A few other remarks:

> my @lines = ();
> my @line = ();

It seems like there is no need to define those variables outside the loop.

> my @lastnames = ();
>
> loaded into @lines a file

Why? If there is no particular reason to do so, the better practice is
to read the file line by line.

Please consider this code:

    my $file = 'data.txt';
    open my $fh, '<', $file or die "Opening $file failed: $!";

    my @lastnames;
    while ( <$fh> ) {
        my @line = split /\t/;
        chomp @line;  # if the record has no further elements
        push @lastnames, $line[1];
    }

Signature

Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.