in reply to Nested While Loop Problem

You should always start every script with use strict; use warnings;. These are very good ways of avoiding a lot of pointless headaches.

You could achieve your goal in a fairly straight forward way using hashes. Replace the print statements in your while loop with $count{$dir}++;, which will increment a directory-specific counter. You can then finish your script with print join "\n", keys %count;. For some information on hashes, check out perlintro.

Replies are listed 'Best First'.
Re^2: Nested While Loop Problem
by Smith (Initiate) on Feb 06, 2009 at 15:06 UTC
    Thank you very much. Im still a little green on hashes and wasnt sure if it would work in this situation without introducing yet another loop.

    I had the two print statements to show that it prints fine from within the if statement. (it gives me the three directories like it should)
    if ($fsline =~ /(.:\\.*)/){ $dir = $1; print "$dir\n"; #works fine here }
    But if I print outside of the if statement I get the redundancies. And if there are then this command will repeat against the remote host multiple times for the same directory.

    $badhost = "$logdir\\BADHOST.csv"; open (BADHOST,">>$badhost"); $dest = "$dir\\$file"; #will print redundancies here $dest =~ s/[\s]//g; $md5sumtmp = `rctrlx.exe $target /u "domain\\username" /p "password" / +c md5sum.exe /app md5sum.exe $dest`;
      I am having a little difficulty interpreting your question, so if this answer is off point I apologize. I think what is happening is that since you are accessing the $dir variable outside of the conditional loop, you are caching a value of $dir from a previous traverse of the loop. Perhaps this could be fixed with something along the lines of

      if ($fsline =~ /(.:\\.*)/){ $dir = $1; print "$dir\n"; #works fine here } else { next; }

        You interpreted my question perfectly, thank you. This fixes my problem. I had tried using next but not with the else. So close but so far away.

        Thanks very much for your help and fast response.