| | |
array problem - help!
![]() |
•
•
Join Date: May 2005
Posts: 45
Reputation:
Solved Threads: 0
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.
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
I am doing a very simple array declaration by reading a text file into an array.
Perl Syntax (Toggle Plain Text)
open (FILE, $file_var); my @contents=<FILE>; 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
•
•
Join Date: May 2005
Posts: 45
Reputation:
Solved Threads: 0
•
•
•
•
- 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".
•
•
Join Date: May 2005
Posts: 45
Reputation:
Solved Threads: 0
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:
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!?
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:
Perl Syntax (Toggle Plain Text)
if (-e $path_to_file) { open (FILE, $path_to_file) or die; my @contents=<FILE>; close(FILE); } else { open(FILE, >$path_to_file); close(FILE); }
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.
•
•
•
•
Well the 'my' exists inside a { } scope, so I guess it goes out of scope when the block exits.
Yep, thats probably the reason.
Perl Syntax (Toggle Plain Text)
my @contents = (); if (-e $path_to_file) { open (FILE, $path_to_file) or die "$!"; @contents=<FILE>; close(FILE); } print "$_\n" for @contents;
![]() |
Similar Threads
- simple array problem not so simple (Java)
- Array problem (Java)
- Array problem (C++)
- Array problem (C#)
- Is there a simplest way to work this array problem? (C++)
- class array problem! (C++)
- C++ help with student array problem (C++)
- i have problem with array plz help (Java)
- Large Array Problem (PHP)
Other Threads in the Perl Forum
- Previous Thread: Perl Newby Question
- Next Thread: Shutting down VMs on a host in order
| Thread Tools | Search this Thread |






