in reply to Re^3: Sorting by value??
in thread Sorting by value??

Ok heres what i have so far
#!/usr/bin/perl my $inFile = "Report.txt"; my $outFile = "SortedReport.txt"; open IN, "< $inFile"; open OUT, ">> $outFile"; my @not_sorted = <IN>; print OUT "|CSED Form|OrderNumber|Date|Total Documents|Total Pages|\n +"; @sorted = sort { lc($a) cmp lc($b) } @not_sorted; # alphabetic +al sort my $count = 1; foreach(@sorted) { $frmType = substr $_, 1, 3; if ($frmType = "M729") { print OUT "$_"; $count ++; } else { print OUT "There were $count Monthly 729 runs\n"; } } foreach(@sorted) { $frmType = substr $_, 1, 3; if ($frmType = "M737") { print OUT "$_"; $count ++; } else { print OUT "there were $count Monthly 737 runs\n"; } } $count = 0; foreach(@sorted) { $frmType = substr $_, 1, 3; if ($frmType = "Q569") { print OUT "$_"; $count ++; } else { print OUT "there were $count Quarterly 569 runs\n"; } } close OUT; close IN;

Replies are listed 'Best First'.
Re^5: Sorting by value??
by ahmad (Hermit) on Jun 16, 2010 at 01:07 UTC

    in your code : if ($frmType = "M729") And the other "if"'s like it will always be true because you are assigning not comparing.

    And you cannot check strings using numerical test '==' , You'll have to use 'eq' instead. e.g: if ($frmType eq "M729")

Re^5: Sorting by value??
by graff (Chancellor) on Jun 16, 2010 at 05:16 UTC
    Once you fix the problem that was explained by ahmad above, you will discover the next problem with your code: now all the "if" conditions will come out false every time, because...
    ... $frmType = substr $_, 1, 3; if ($frmType eq "M729") { ...
    The "substr()" call is assigning a 3-character string to $frmtype, and you are testing that to see if it's identical to a 4-character string ("M729" in this case). That makes no sense.

    Also, proper indentation would help a lot. Please give that a try.

Re^5: Sorting by value??
by kennethk (Abbot) on Jun 16, 2010 at 14:05 UTC
    First, apply the suggestions from toolic, ahmed and graff. In particular, please fix your indentation - as toolic said, perltidy will do that for you.

    In addition:

    1. You initialize $count to 1 to start, which will give you an off-by-one error. You mean 0.
    2. You do not reinitialize $count before your M737 loop. That means this will count both M729 and M737.
    3. Since your print statement is inside your for loop, you will print your results string many times - 6, 9, and 7 times respectively for the provided input. In addition, your output will be wrong every time for Q569 as all your prints will happen before you process any of your Q569s - it will print 0 each time.
    Post code again if you have all implemented these changes and your code still doesn't function as expected.
      Once again I appreciate your guys' help a lot I'm still fairly new to PERL ok here is the code with the changes made and also another bit of code I have Added

      #!/usr/bin/perl use warnings; use strict; my $inFile = "UnSortReport.txt"; my $outFile = "SortedReport.txt"; open IN, "< $inFile"; open OUT, ">> $outFile"; my @not_sorted = <IN>; print OUT "|CSED Form|OrderNumber|Date|Total Documents|Total Pages +|\n"; @sorted = sort { lc($a) cmp lc($b) } @not_sorted; # alphabetic +al sort my $count = 0; foreach(@sorted) { $frmType = substr $_, 1, 4; if ($frmType eq "M729") { print OUT "$_"; $count ++; } else { print OUT "There were $count Monthly 729 runs\n"; } } $count = 0; foreach(@sorted) { $frmType = substr $_, 1, 4; if ($frmType eq "M737") { print OUT "$_"; $count ++; } else { print OUT "There were $count Monthly 737 runs\n"; } } $count = 0; foreach(@sorted) { $frmType = substr $_, 1, 4; if ($frmType eq "Q569") { print OUT "$_"; $count ++; } else { print OUT "There were $count Quarterly 569 runs\n"; } } close OUT; close IN; ##############The code Bellow looks a the SortedReport.txt and checks + for duplicate lines and removes them if any######################### +### my $file = 'SortedReport.txt'; my %seen = (); { local @ARGV = ($file); local $^I = '.bac'; while(<>) { $seen{$_}++; next if $seen{$_} > 1; print; } } print "finished processing file.\n";
        A couple of comments:

        1. You added strict at the top, but did not add variable declarations (my). Therefore, it seems unlikely you tried to run your code before posting it. This is poor form as it shows a lack of effort on your part.
        2. You require my before @sorted = ... and $frmType = ... to fix the above.
        3. You did not fix the issue I pointed out in item 3 above -- your print statements are located inappropriately for your desired output. If you fix that, you don't need your second filtering stage. Each of your foreach loops should look something like:
          foreach(@sorted) { my $frmType = substr $_, 1, 4; if ($frmType eq "Q569") { print OUT "$_"; $count ++; } } print OUT "There were $count Quarterly 569 runs\n";
          where I have attempted to follow your existant style.