Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Accessing variables

by pavanmach (Novice)
on Feb 02, 2010 at 07:03 UTC ( #820900=perlquestion: print w/replies, xml ) Need Help??
pavanmach has asked for the wisdom of the Perl Monks concerning the following question:

my program is as under:
#!/usr/bin/perl my ($file1,$file2); open(FILE1,"<$ARGV[0]"); open(FILE2,"<$ARGV[1]"); while(<FILE1>) { chomp($_); $file1 = $_; $file1=~s/\s+//; #print "$file1 \n"; while(<FILE2>) { print "<$file1> \n"; } }
now the two files are as under: file1.txt
haai hru welcome hello thats gr8 select * from emp; file2.txt haai hru welcome hello thats gr8 select * from emp; select * from dual;
In the above program i have two while statements. I am trying to print file1 variable in first while which is successful and the output is as under:
haai hru welcome hello thatsgr8 select* from emp;
while i am tring to print the same $file1 in second while statement statement the output is :
<> <> <> <> <> <>
I am not able to access the variable ($file1) in second while loop. Why so? How can i overcome the problem ?

Replies are listed 'Best First'.
Re: Accessing variables
by Utilitarian (Vicar) on Feb 02, 2010 at 08:08 UTC
    The code as supplied works here,in that I get the first line of file1 for each line in file2 and then nothing as we have reached the end of file2 so the while loop needs to be reset if you wish to see further into file1 than line1.

    Are you sure your output is from a run with the code you supplied?

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Accessing variables
by Anonymous Monk on Feb 02, 2010 at 07:30 UTC
    use autodie;
    and you get error checking for open/close...

      hi, There is no open close error in the program as i have used autodie module as u have suggested and i am not into $@ at all. My problem is the value which i am assigned to a variable in first while loop is not accessible in second while loop, y so ? Your response will be highly appreciated.

        Maybe your code should look somewhat like
        #!/usr/bin/perl my ($file1,$file2); open(FILE1,"<$ARGV[0]"); while(<FILE1>) { chomp($_); $file1 = $_; $file1=~s/\s+//; open(FILE2,"<$ARGV[1]"); while(<FILE2>) { print "<$file1> \n"; } close FILE2; }

        What output do you wish or expect?


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://820900]
Approved by Corion
[jedikaiti]: 'ello
marto waves

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2017-11-21 17:54 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (308 votes). Check out past polls.