bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

What happened to my program ? is it wrong ? As your information, I want to print out all the files in the specific directory.

But, after i test it the program did not print it correctly, All the files have been printed twice. As example :-

TEST: EMSSIN_SR10_20061011115435.txt TEST: EMSSIN_VOIP_20061011115435.txt TEST: EMSSIN_SR10_20061011115435.txt TEST: EMSSIN_VOIP_20061011115435.txt
Could somebody tell me what wrong with my program ?
sub splitInputFile { my $cnt = shift; my $mscname = $swname[$cnt]; my $ftype = $outftype[$cnt]; my $ext = $outfext[$cnt]; my $indir = $inputdir[$cnt]; my $outdir = $outputdir[$cnt]; my $arcdir = $archivedir[$cnt]; if ( -d "$indir") { opendir(INDATA,$indir); while (my $indata = readdir(INDATA)) { next if $indata !~ /EMSSIN/; print "TEST: $indata\n"; } closedir(INDATA); } else { print "ERROR! $indir not found\n"; } }

Replies are listed 'Best First'.
Re: READDIR: files printed twice in the directory ?
by BrowserUk (Patriarch) on Oct 11, 2006 at 07:40 UTC

    Is that a complete output list? Are you sure that the subroutine isn't being called twice?

    Or maybe there are ...SR10... & ...SR1O... and SR(zero versus oh), but that doesn't appear to be the case from what you've posted.

    They're the only ways I can see you could get that output, from that code.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: READDIR: files printed twice in the directory ?
by svenXY (Deacon) on Oct 11, 2006 at 07:43 UTC
    Hi,
    I can only assume that the way you call this sub makes it run twice. The sub itself seems not to explain why it outputs twice.
    Can you give us a more complete example, please?
    Regards,
    svenXY
Re: READDIR: files printed twice in the directory ?
by caelifer (Scribe) on Oct 11, 2006 at 15:07 UTC

    It seems that traversing tree structures has proven to be harder that it looks :)

    Is there any reason not to use File::Find? Code reuse has been proven to be fruitful.

    As for your code, in the way it is presented, it is very hard to say anything concrete. You do not show the context of your function call (i.e. what the actual call looks like, what are the arguments...). The function itself uses global variables and there is no way to say how those are populated. Please look at How (Not) To Ask A Question for more comments on how to post your questions here. However, if I were to guess about the root cause of your problem, I would take a look at your @inputdir global array to see if it contains duplicate entries. Again, this is a shot in the dark!

    BR