how do you save an array for later use?

how can I save file info for later use?
for each file I get through FTP, I would like to save the filename, server and dir name.
later I can do a report of why each file came from.

$ftp = Net::FTP->new("server", Timeout => 30, Debug=>1)       or die;
$ftp->login("user", "pass")          or die;
$ftp->cwd("$rdir")
foreach $fname ( $ftp->ls() ) {
if($fname =~ /^(anr.*\.txt)$/i){
  $ftp->get( $fname, "$localdir\\$1")
  push(@files, $fname,$rdir);
  }
}
$ftp->quit() ;

foreach $fn (@files)
{
     print $fn;
}
matgoldAsked:
Who is Participating?
 
Adam314Connect With a Mentor Commented:
The original script you have saves the data to the @files array.  If you declare the @files at the package level, it will be available to any part of your program.
Declare @files like this, outside of any subroutines:
our @files;

Then, within that package, you can access it just as @files (or $files[index]).  In other packages, you would access it is @packagename::files (or $packagename::files[index]).
0
 
Adam314Commented:
Do you want to save it to a variable, so other parts the program can access it?
Or do you want to save it to a file, so the information will be available even after your program exits?

If to a file: Will the only thing reading it be another perl program, or would you like it in a format that might be readable by other programs?
0
 
mjcoyneCommented:
#!/usr/bin/perl -w
use strict;
use Storable qw(store retrieve);

my @array = qw(zero one two three four);

store(\@array, "array.db") or die "Can't save array.db: $!\n";

# then, in another script:

my @stored_array = @{retrieve("array.db")} or die "Unable to open array.db: $!\n";

print $stored_array[3];
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
matgoldAuthor Commented:
no, there is no second script.
If all possible, I don't want to save it to a file.
0
 
matgoldAuthor Commented:
Thanks,

I declare it the first time, but it didn't work. because I declare it like my @files = "";
0
 
matgoldAuthor Commented:
adam314,
  If I have two FTP session, only the second array(@file2) is empty, not sure why.
my @files;
my @file2;

$ftp = Net::FTP->new("server", Timeout => 30, Debug=>1)       or die;
$ftp->login("user", "pass")          or die;
$ftp->cwd("$rdir")
foreach $fname ( $ftp->ls() ) {
if($fname =~ /^(anr.*\.txt)$/i){
  $ftp->get( $fname, "$localdir\\$1")
  push(@files, $fname,$rdir);
  }
}
$ftp->quit() ;
foreach $fn (@files)
{
     print $fn;
}

$ftp = Net::FTP->new("server", Timeout => 30, Debug=>1)       or die;
$ftp->login("user", "pass")          or die;
$ftp->cwd("$rdir")
foreach $fname ( $ftp->ls() ) {
if($fname =~ /^(abc.*\.txt)$/i){
  $ftp->get( $fname, "$localdir\\$1")
  push(@file2, $fname,$rdir);
  }
}
$ftp->quit() ;
foreach $fn (@file2)
{
     print $fn;
}
0
 
Adam314Commented:
Is that the actual code you are running?  It has several syntax errors - i don't think it will even compile.
Are you using "use strict" and "use warnings".  They might help you find errors.
Are there any files found the second time through?
0
 
matgoldAuthor Commented:
I don't understand, I'm getting a much of "requires explicit package name"
I've done this multiple FTP connection many time, never have a problem.
0
 
Adam314Commented:
Did you start getting that error when you added a "use strict"?
The use strict requires that all variables be declared before being used.

my @files;
my @file2;

# CHANGE THESE FOR YOUR SETUP
my $rdir='';
my $localdir='';

my $ftp = Net::FTP->new("server", Timeout => 30, Debug=>1)       or die;
$ftp->login("user", "pass")          or die;
$ftp->cwd("$rdir");
foreach my $fname ( $ftp->ls() ) {
      if($fname =~ /^(anr.*\.txt)$/i){
            $ftp->get( $fname, "$localdir\\$1");
            push(@files, $fname,$rdir);
      }
}
$ftp->quit() ;
foreach my $fn (@files)
{
      print $fn;
}


$ftp = Net::FTP->new("server", Timeout => 30, Debug=>1)       or die;
$ftp->login("user", "pass")          or die;
$ftp->cwd("$rdir");
foreach my $fname ( $ftp->ls() ) {
      if($fname =~ /^(abc.*\.txt)$/i){
            $ftp->get( $fname, "$localdir\\$1");
            push(@file2, $fname,$rdir);
      }
}
$ftp->quit() ;
foreach my $fn (@file2)
{
      print $fn;
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.