matgold
asked on
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;
}
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;
}
#!/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];
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];
ASKER
no, there is no second script.
If all possible, I don't want to save it to a file.
If all possible, I don't want to save it to a file.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks,
I declare it the first time, but it didn't work. because I declare it like my @files = "";
I declare it the first time, but it didn't work. because I declare it like my @files = "";
ASKER
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;
}
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;
}
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?
Are you using "use strict" and "use warnings". They might help you find errors.
Are there any files found the second time through?
ASKER
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.
I've done this multiple FTP connection many time, never have a problem.
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;
}
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;
}
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?