CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Possibly a variable scope problem ...: Edit Log



reld
Novice

May 29, 2013, 2:53 AM


Views: 587
Possibly a variable scope problem ...

Hi, I am having trouble working out why the variable $name in the inner most while loop does not contain the value assigned to it in the while loop above.


Code
#!/usr/bin/perl 

use strict;
use warnings;

use Path::Class;
use autodie; # die if problem reading or writing a file

my $dir = dir("./"); # Read from current directory
opendir(DIR, $dir) or die $!; # Open the directory

while (my $file = readdir(DIR)) { # Read file from directory

next if ($file =~ m/^\./); # Skip . .. directories

if ( $file =~ /\.htm/ ) { # If file ends in .htm

my $file1 = $dir->file($file); # htm files

my $file2 = $dir->file("filelinks.txt"); # File to output to
my $file3 = $dir->file("file.txt"); #read from for data

# openr() returns an IO::File object to read from
my $file_handle1 = $file1->openr();
my $file_handle2 = $file2->open('>>'); # Append
my $file_handle3 = $file3->openr();

# Read in line at a time
while( my $line = $file_handle3->getline() ) {


my @cut = split ( /,/, $line );

my $name = $cut[0];
# $file_handle2->print($name) will work and put data1 into the output file
while ( my $line2 = $file_handle1->getline()) {

$file_handle2->print($name); #does not work
}



}
}
}


I am reading from a file.txt, and some .htm files. The file.txt has the form:


Code
data1,data2,data3 
data1,data2,data3
data1,data2,data3
...


I am trying to read from file.txt, and find matches from the value data1 in the .htm file.

I think this might be a scope problem. If anyone can help that would be greatly appreciated.


(This post was edited by reld on May 29, 2013, 3:45 AM)


Edit Log:
Post edited by reld (Novice) on May 29, 2013, 2:54 AM
Post edited by reld (Novice) on May 29, 2013, 2:56 AM
Post edited by reld (Novice) on May 29, 2013, 2:58 AM
Post edited by reld (Novice) on May 29, 2013, 3:01 AM
Post edited by reld (Novice) on May 29, 2013, 3:02 AM
Post edited by reld (Novice) on May 29, 2013, 3:04 AM
Post edited by reld (Novice) on May 29, 2013, 3:08 AM
Post edited by reld (Novice) on May 29, 2013, 3:10 AM
Post edited by reld (Novice) on May 29, 2013, 3:29 AM
Post edited by reld (Novice) on May 29, 2013, 3:45 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives