For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Using Tie::File second time doesn't read the file into an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Using Tie::File second time doesn't read the file into an array
Manish

2006-10-23, 6:57 pm

Using Tie::File second time doesn't read the file into an array, wonder
why?

#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use Tie::File;

my $dir = "c:/dev3/development";
my $filename = "C:/Documents and Settings/marora/Desktop/ssreport.txt";
my @fileContent = ();
my $debug = 1;
my $userid = "Marora";
my $dest = "c:/";

sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}

sub constructfilelist
{
tie @fileContent, 'Tie::File', $filename or "Couldn't open $filename
$!";

my $i = 0;

for(@fileContent) {
print "$_\n" if ($debug);

my $filepath = $fileContent[$i];
$fileContent[$i+1] =~ m/(^.*)\s+$userid/; #extract file name

my $fname = $1;
my $destfname = $dest.$fname;
my @FileToBeCopied = ();

$filepath =~ s/:/\/$fname/; # construct file path with name
$filepath =~ s/\$/c:/; # remove $ from the file path with name
$filepath = trim($filepath);
print "File: $filepath\n" if ($debug);
print "copy $filepath to $destfname\n" if ($debug);

my $newfname = $filepath;
print $newfname;

open(OUT, '>', $destfname) or die "couldn't open $destfname $!";
tie @FileToBeCopied, 'Tie::File', $newfname or "Couldn't open
$newfname $!";

for (@FileToBeCopied) {
print "i am here \n"; <------ PROBLEM HERE, FileToBeCopied is
never populated.
}

print @FileToBeCopied if ($debug);
print OUT @FileToBeCopied;

close OUT;

$i = $i + 2;
exit;
}
}

&constructfilelist();

Manish

2006-10-23, 9:57 pm

Hello everyone,

Thanks for taking your time to look at my question.

I realized my mistake, my file name path for the second Tie was not
properly constructed.

Manish

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com