Link to home
Start Free TrialLog in
Avatar of Europa MacDonald
Europa MacDonaldFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Changing output of a file

I have this code

while (<FILE>) {
open FILE, "1.vim" || die "unable to open file:$! \n";

chomp()
@array=split(/\s+);
$columns{'Col1'}{$array[0]}+=1;
$columns{'Col2'}{$array[1]}+=1;
$columns{'Col3'}{$array[2]}+=1;
$columns{'Col4'}{$array[3]}+=1;
}
foreach $key (keys %columns) {  #first Hash columns
      print "$key\npattern,count\n";
     foreach $pattern (sort keys %{$columns{$key}}) { #pattern sort
             print "$pattern,$column{$key}{$pattern}\n";      
     }
}

What code could I add to the end which would enable me just to have the perl script do that without opening the command window, and where do I put that code ?
Avatar of David Favor
David Favor
Flag of United States of America image

If I understand. You're just asking to write output to a file.

So at top of your script...

open(my $out, ">/path-to-outfile") || die "Unable to  open outfile\n";

Change all your print statements to print $out "...\n" + then close($out) at end of script.
Avatar of Europa MacDonald

ASKER

I am trying to make this work

#!/usr/bin/perl
use strict;
use warnings;

open M,"<master.vim" or die "master.vim $!";

while (<M>) {

chomp()
@array=split(/\s+);
$columns{'Col1'}{$array[0]}+=1;
$columns{'Col2'}{$array[1]}+=1;
$columns{'Col3'}{$array[2]}+=1;
$columns{'Col4'}{$array[3]}+=1;
}

open C,">output.txt" or die "output.txt $!";

foreach $key (keys %columns) {                          #first Hash columns

      print C, "$key\npattern,count\n";

     foreach $pattern (sort keys %{$columns{$key}}) {             #pattern sort

             print C,">output.txt $pattern,$column{$key}{$pattern}\n";
close($out);
       
     }
}
Hi Europa,

I found various typos, etc.
Also, I've commented out the "use strict" line, because I'm not that good.  It seems to run without syntax errors, now.

#!/usr/bin/perl
#use strict;
use warnings;

open M,"<master.vim" or die "master.vim $!";

while (<M>) {
        chomp();
        @array=split(/\s+/);
        $columns{'Col1'}{$array[0]}+=1;
        $columns{'Col2'}{$array[1]}+=1;
        $columns{'Col3'}{$array[2]}+=1;
        $columns{'Col4'}{$array[3]}+=1;
}

open C,">output.txt" or die "output.txt $!";
foreach $key (keys %columns) {                          #first Hash columns
        print C "$key\npattern,count\n";
        foreach $pattern (sort keys %{$columns{$key}}) {             #pattern sort
                print C "$pattern,$columns{$key}{$pattern}\n";
        }
}
close C;

Open in new window

You seem to be processing only the first 4 of the 5 columns in the input file.  Intentional?
Thanks Tel, Im getting error messages with it. I have attached a screenshot.
Untitled.jpg
Hi Europa,

I don't get those errors in Linux with your test data.
If you have the code starting like this, as I have:
    #use strict;
    use warnings;
then change it to this:
    #use strict;
    #use warnings;
which is effectively the same as removing those 2 lines completely.
If you still get errors, pls post (attached) the exact code you're using and a sample of input data that produces those errors.

And please answer this question from the bottom of my previous post:
  "You seem to be processing only the first 4 of the 5 columns in the input file.  Intentional?"
Thankyou Tel, I am afraid I might not have explained properly what I was looking for.

This is the code I am using column_counter.txt

This is the sample data I am using test.txt

This is the result that the code produces output.txt

This is the way that I need the results displayed result.txt

I first tested this on data which is about 1.7 million rows, and the output.txt just hung, so maybe it was not closed properly ? I dont know.

Using #use warnings; made a difference
ASKER CERTIFIED SOLUTION
Avatar of tel2
tel2
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial