array problem - help!

Reply

Join Date: May 2005
Posts: 45
Reputation: derekn is an unknown quantity at this point 
Solved Threads: 0
derekn derekn is offline Offline
Light Poster

array problem - help!

 
0
  #1
Jun 29th, 2009
Ok, I am about to pull all my hair out. I have written many many perl scripts and have done it all pretty much. All of a sudden, I have hit a real problem, and I DON'T know why!

I am doing a very simple array declaration by reading a text file into an array.
  1. open (FILE, $file_var);
  2. my @contents=<FILE>;
  3. close(FILE);

Pretty simple stuff, huh!? I have used this same routine hundreds of times. But for some reason, declaring the array with "my" is resulting in an empty array! If I just declare it with "@contents=<FILE>", it works fine! Why on earth is this happening? I even declared another array using the same format in the same SCRIPT, and it works fine, but this particular array is turning up empty EVERY time!

I hope someone can help me!

Derek
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: array problem - help!

 
0
  #2
Jun 29th, 2009
- did you actually open the file?
- does the file you actually opened contain any data?
- does another approach to reading the same file yield data?

3 lines out of context, and an information-free story doesn't help.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 45
Reputation: derekn is an unknown quantity at this point 
Solved Threads: 0
derekn derekn is offline Offline
Light Poster

Re: array problem - help!

 
0
  #3
Jun 29th, 2009
Originally Posted by Salem View Post
- did you actually open the file?
>>Yes, file exists, returns true with "if (-e $file_var)"

- does the file you actually opened contain any data?
>>Yes

- does another approach to reading the same file yield data?
>>If I just declare it with "@contents=<FILE>", it works fine!

3 lines out of context, and an information-free story doesn't help.
>>Not out of context at all...this is "THE SCRIPT".
It's a simple open-file-and-read-contents-into-array script. The file is a TEXT file, there is at LEAST one line of data in the file, and the path to the file is valid. The "problem" is declaring it with "my", and again, I don't understand why, when I have done it thousands of times this exact way before.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 45
Reputation: derekn is an unknown quantity at this point 
Solved Threads: 0
derekn derekn is offline Offline
Light Poster

Re: array problem - help!

 
0
  #4
Jun 29th, 2009
OK this is getting stupider by the moment.

I set path like so: $path_to_file="path/file.txt";

If the file exists (need to verify that the file exists before continuing), then read the data into the array, otherwise create the file:

  1. if (-e $path_to_file)
  2. {
  3. open (FILE, $path_to_file) or die;
  4. my @contents=<FILE>;
  5. close(FILE);
  6. }
  7. else
  8. {
  9. open(FILE, >$path_to_file);
  10. close(FILE);
  11. }

Tada! The array is EMPTY!

However, if i remove the "if exists" check from around the routine to read data into array, it works! Why!?
Last edited by derekn; Jun 29th, 2009 at 2:11 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: array problem - help!

 
0
  #5
Jun 29th, 2009
Well the 'my' exists inside a { } scope, so I guess it goes out of scope when the block exits.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: array problem - help!

 
0
  #6
Jun 29th, 2009
Originally Posted by Salem View Post
Well the 'my' exists inside a { } scope, so I guess it goes out of scope when the block exits.

Yep, thats probably the reason.

  1. my @contents = ();
  2. if (-e $path_to_file)
  3. {
  4. open (FILE, $path_to_file) or die "$!";
  5. @contents=<FILE>;
  6. close(FILE);
  7. }
  8. print "$_\n" for @contents;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC