Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Unable to write output to file taken as input from command line

by zing (Beadle)
on Jul 24, 2014 at 12:47 UTC ( [id://1094906]=perlquestion: print w/replies, xml ) Need Help??

zing has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, So I have this code which takes input and output file from command line, then writes a certain output to the output file (Only the relevant portion shown here due to privacy issues):
use strict; use warnings; use autodie; # check that two arguments have been passed die "usage: $0 input output\n" unless @ARGV == 2; my $infile = shift; my $outfile = shift; open my $in, "<", $infile; open(DATA,$in); open my $out, ">", $outfile; ins split /,\s*/ for <DATA>; print $out "%-4s: %20s, %-8s %6s\n", $_->[0], qq($_->[0]$_->[3]) +, $_->[2], $group{$_->[2]}; close $in; close $out;
The problem is that it isnt writing anything to the output file. Due to certain reasons I want to read in the input file in <DATA> format, so that cant be done away with. Please help

Replies are listed 'Best First'.
Re: Unable to write output to file taken as input from command line
by choroba (Cardinal) on Jul 24, 2014 at 13:03 UTC
    There seem to be some more than just privacy issues here.
    • What's ins?
    • You use print with a format parameter, you probably meant printf?
    • Also, DATA is not a format, it is a file handle.
    • Moreover, what do you expect open(DATA,$in) to do? $in is a filehandle opened in the previous line, do you really want to use it as a file name?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Unable to write output to file taken as input from command line
by muba (Priest) on Jul 24, 2014 at 13:03 UTC

    First of all, you don't have to open DATA to read from it. Also, without looking it up or testing it, I'm not too sure what these two lines, in this combination, are supposed to do:

    open my $in, "<", $infile; open(DATA,$in);
    but it looks wrong to me. Would you care to elaborate as to what your intensions were here?

    The next thing that strikes me as odd is the ins split /,\s*/ for <DATA>; line: where is ins defined? Is it imported from somewhere? Did you forget to include the relevant use ...; statement or the actual subroutine itself? Because as is, your code won't even run without an error message regarding this line.

    Not to mention the fact that your code has use strict; and then uses an undeclared variable:

    C:\Users\Lona\Desktop>perl x.pl syntax error at x.pl line 15, near "ins split" Global symbol "%group" requires explicit package name at x.pl line 16. Execution of x.pl aborted due to compilation errors. C:\Users\Lona\Desktop>
Re: Unable to write output to file taken as input from command line
by Anonymous Monk on Jul 24, 2014 at 13:45 UTC
    Due to certain reasons I want to read in the input file in <DATA> format

    Am I understanding you correctly that you want the code that reads from the filehandle to look like <DATA> instead of <$in>? Is that what you are trying to accomplish with open(DATA,$in);? If you could explain what your "certain reasons" are, that might help in finding a better solution.

      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,

        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
        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.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1094906]
Approved by muba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found