in reply to Re: Unable to write output to file taken as input from command line
in thread Unable to write output to file taken as input from command line

Sorry guys, so here's everything Ive got.
#! /usr/bin/perl #=======================Processing the input file===================== +===# print "Enter file name:\n"; chomp(my $file = <STDIN>); open(DATA,$file) or die "failed opening file!!"; my %DEF = ( I => [qw( P Pl P.P P.Pl Pl.P Pl.Pl P.P.P P.P.Pl P.Pl.P P.Pl.Pl Pl. +P.P Pl.P.Pl Pl.Pl.P Pl.Pl.Pl )], II => [qw( E P.E Pl.E P.P.E P.Pl.E Pl.P.E Pl.Pl.E )], III => [qw( E.P E.Pl P.E.P P.E.Pl Pl.E.P Pl.E.Pl E.P.P E.P.Pl E.Pl.P + E.Pl.Pl )], IV => [qw( E.E P.E.E Pl.E.E E.P.E E.Pl.E E.E.P E.E.Pl E.E.E )] ); # Hash table/dictionary for all the groups my @rank = map @$_, @DEF{qw(I II III IV)}; my %rank = map {$rank[$_-1] => $_} 1..@rank; my @group = map {($_) x @{$DEF{$_}}} qw(I II III IV); my %group = map {$rank[$_-1] => $group[$_-1]."_".$_} 1..@group; sub rank { $rank{$a->[2]} <=> $rank{$b->[2]} } my %T; sub oh { map values %$_, @_ } sub ab { my ($b, $a) = @_; [$b->[0], $a->[1], qq($a->[2].$b->[2]), qq($b->[3]<-$a->[3])] } sub xtend { my $a = shift; map {ab $_, $a} oh @{$T{$a->[0]}}{@_} } sub ins { $T{$_[3] //= $_[1]}{$_[2]}{$_[0]} = \@_ } ins split /,\s*/ for <DATA>; #ins split /,\s*/ for $filename; ins @$_ for map {xtend $_, qw(P E Pl)} (oh oh oh \%T); ins @$_ for map {xtend $_, qw(P E Pl)} (oh oh oh \%T); for (sort {rank} grep {$_->[1] eq 'Q'} (oh oh oh \%T)) { printf "%-4s: %20s, %-8s %6s\n", $_->[0], qq($_->[0]<-$_->[3]), $_->[2], $group{$_->[2]}; }
Line no. 40 is printing out to console. But all those outputs I want in a user specified(while running the program) output file, apart from taking the input file also from command line , i.e. -->
perl program.pl input_file output_file
The input file
input_file M19,Q,P, M31,M19,Pl, M420,M31,E, M421,M31,E, M33,M31,E, M438,M33,Pl, M445,M33,E, M437,M33,E, M444,M33,E, M73,M33,E, M552,M73,Pl, M553,M73,Pl, M569,M73,E, M549,M73,E, M550,M73,E,

Replies are listed 'Best First'.
Re^3: Unable to write output to file taken as input from command line
by GotToBTru (Prior) on Jul 24, 2014 at 14:53 UTC

    To collect the file names from the command line, replace

    print "Enter file name:\n"; chomp(my $file = <STDIN>); open(DATA,$file);

    with

    chomp(my $infile = shift); open(DATA,$infile) or die "Could not open $infile\n";

    Add

    chomp (my $outfile = shift); open OUTPUT, '>', $outfile or die "Could not open $outfile\n";

    before the final for loop. Replace

    printf "%-4s: %20s, %-8s %6s\n",

    with

    printf OUTPUT "%-4s: %20s, %-8s %6s\n",
    1 Peter 4:10
      You don't need to use chomp on values drawn from @ARGV. There's stuff in zing's code that doesn't make sense, but whatever the purpose may be, the basic template should be something like this:
      die "Usage: $0 infile outfile\n" unless ( @ARGV == 2 ); my $infile = shift; my $outfile = shift; open( INPUT, '<', $infile ) or die "$infile: $!\n"; open( OUTPUT, '>', $outfile ) or die "$outfile: $!\n"; while (<INPUT>) { # do stuff with $_; # print something to OUTPUT }
Re^3: Unable to write output to file taken as input from command line
by graff (Chancellor) on Jul 25, 2014 at 02:59 UTC
    Regarding the use of @ARGV to get file names, see my reply above.

    I have to rephrase choroba 's initial question: what is ins supposed to do? Also, what do you expect/intend to accomplish with the code (oh oh oh \%T)? (Is that supposed to be some sort of inside joke?)

    If the input file contained just one line of data (e.g. "M19,Q,P,"), what would you want to see in the output file? (Be specific.)