in reply to Re^2: Duplicates in Win32::DirSize
in thread Duplicates in Win32::DirSize

For clarification, what kind of "hurtle" are you seeing? Is it messing up when it tries to itemCreate() because of the duplicate name? Is it reporting a wrong size? Are we talking duplicate full paths or just duplicate bottom-level folder names (c:/foo/bar/baz and c:/windows/baz, for instance)?

While I'm here, you could make the code a little more concise:

# Line 2 my $cleansource = "$course\\$_"; # or: my $cleansource = $course . "\\" . $_; # Lines 6/7 my $s = "$sourceSize$sourceSizeUnit"; # or: my $s = $sourceSize . $sourceSizeUnit

Using x= (with x being some operator) is only more efficient when working with one additional value. Which looks better to you:

$x = $a; $x += $b; $x += $c; $x += $d; $x = $a + $b + $c + $d;

The latter involves less typing, looks much clearer, and is likely more efficient. Sorry to be nitpicky. I'm often anal about grammar and all things related (both in code and in language)

[id://wink] waits for someone to find a grammar mistake in this reply (no, I didn't place one intentionally for anyone to find, but I likely made one). ;)

Replies are listed 'Best First'.
Re^4: Duplicates in Win32::DirSize
by Real Perl (Beadle) on Aug 21, 2005 at 03:50 UTC
    Dear Wink,
    Thank you very much to show me the efficient way of doing things. I am quite new at programming and I'm learning Perl on my own!! (with the Monks).
    Any way, I don't know how to clarirfy the hurtle. One thing I know is that if I have "user1, user2, user3 and then user1 again, the method calculates the right size for user1 then user2 and user3 and then gives me a long error message at the DOS prompt and eventhough the program goes on and displays what was calculated (that is done with the make_table method that I am not showing here), the program is not functional anymore and has to be closed. The duplicates are only in the user names. For example, I ask my program to go get the size for: C:\documents and settings\desktop\testfolder\ and then in a for loop I append the name of the folder under testfolder.
    I though of dealing with the duplicates before it even tries to calculate anything by sorting the array and checking if I have duplicates before I launch the method, but I have another issue with that. The issue is really embarrassing because Perl execute the if statement and the else statement also!
    Here is the all method:
    sub create_file_for_batch{ if($dest ne "" && $source ne "" && $logpath ne "" && length($sourc +etext)!=1){ $sourcetext=$sourcetextentry->get('1.0','end'); $original_dest = $dest; $dest =~s/\//\\/g; $original_source = $source; $source =~s/\//\\/g; $switches=""; save_switches(); print LOG ("The destination path was: $dest\n The source path was: $source\n The path for the log file is: $logpath\n The users are: $sourcetext\n"); @arraybatch = split /,|\n/, $sourcetext;#splits what is in the + variable $len = @arraybatch; for($i=1; $i<$len; $i=$i+2){ $arraybatch[$i] =~s/\G //g;#gets rid of the space in front $arraybatch[$i] =~s/ /_/g;#put _ in place of the space bet +ween the name push @arrayfilename, $arraybatch[$i]; }#for for($i=0; $i<$len; $i=$i+2){ push @arraydirectory, $arraybatch[$i]; }#for @sortedarraydirectory = @arraydirectory;#makes a copy of the a +rraydirectory so I can sort @sortedarraydirectory= sort @sortedarraydirectory;#sort the co +pied array $len = @sortedarraydirectory; for ($i=0; $i<$len; $i++){ if ($sortedarraydirectory[$i] ne $sortedarraydirectory[$i+ +1]){ make_table(); $notebook->pageconfigure('page1', -state=>'normal'); $notebook->pageconfigure('page2', -state=>'normal'); }else{ $dialog = $page3-> Dialog( -title => 'Alert', -text => "$sortedarraydirectory[$i] is a duplicate. Pl +ease correct the batch file.", -buttons => [ qw(OK) ]) ->Show(); }#else }#for }else{ $dialog = $page3-> Dialog( -title => 'Alert', -text => "Please enter paths for destination, sources and log +file and fill out the text area for batching", -buttons => [ qw(OK) ]) ->Show(); }#else }#create_file_for_batch
    The outside if else statement works just fine it is just the if and else inside that does not act properly.
    I would be very appreciative if you could point out what I am doing wrong, as I am about ready to give up on checking for duplicate all together!
    Thanks,
    Claire

      Is there any way you can provide your data file? If it's very large, put it on your scratchpad rather than here. Also, please give the "long error message" you are getting so I can try to see what's going on.

      It looks like you're using a LOT of global variables. This can be bad, as you may inadvertently change the global in another function. Instead, try passing variables between your subroutines and declaring scopes by using my. Also, some more code efficiency:

      1. You can avoid using a bunch of escape characters by using alternative characters for your regexes. It will sometimes look cleaner and be much easier to see what it is you're trying to do.

      $dest =~ s/\//\\/g; # Rewritten: $dest =~ s#/#\\#g; # Singular character $dest =~ s{/}{\\}g; # Paired characters

      There are a bunch of other characters you can use here. Don't worry, the # won't be seen as a comment.

      2. Don't bother making a copy of an array before sorting it. The sort function will return the sorted array without changing the original.

      @sortedarraydirectory = @arraydirectory; @sortedarraydirectory = sort @sortedarraydirectory # Change to 1 command: @sortedarraydirectory = sort @arraydirectory;

      I also learned Perl on my own, with some limited help from a guy at work (heck, I'm still learning it!). I highly suggest you look into O'Reilly's Learning Perl, as it's a very well-written resource that will teach you the essentials. Progamming Perl is also a valuable resource, but moreso after you are already familiar with the language.