Pipe input from a Text-File

G

Gerhard

I've made this little script for adding two numbers from command-line:

my ($a,$b) = @ARGV;
my $prg_name = $0; $prg_name =~ s|^.*[\\/]||;
# Usage:
my $usage="Usage: \n$prg_name <A> <B>\n[]: optional Parameter\n";
if (! defined ($ARGV[0])) { print "$usage"; exit; }
print $a+$b;

C:\>add.pl 12 2
14

Content of the Text-File "input.txt":
12 2
2 4
5 7

My question is how can I "pipe" some numbers, which I've stored in a seperate
Text-File "input.txt" to the PERL Script from the DOS Command-line ?
I've tried this, but it don't work this way:
C:\>type input.txt | add.pl
 
T

Tore Aursand

I've made this little script for adding two numbers from command-line:

my ($a,$b) = @ARGV;
my $prg_name = $0; $prg_name =~ s|^.*[\\/]||;
# Usage:
my $usage="Usage: \n$prg_name <A> <B>\n[]: optional Parameter\n";
if (! defined ($ARGV[0])) { print "$usage"; exit; }
print $a+$b;

C:\>add.pl 12 2
14

Content of the Text-File "input.txt":
12 2
2 4
5 7

My question is how can I "pipe" some numbers, which I've stored in a seperate
Text-File "input.txt" to the PERL Script from the DOS Command-line ?
I've tried this, but it don't work this way:
C:\>type input.txt | add.pl

Read <STDIN>. Take a look at this code (untested);

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

my $sum = 0;
while ( <STDIN> ) {
chomp;
last unless ( defined && /\d/ );
$sum += $_;
}
print "$sum\n";

Should work, although it could have been better at checking if there is a
real number (decimal or not).
 
B

Ben Morrow

Quoth Tore Aursand said:
last unless ( defined && /\d/ );
$sum += $_;
}
print "$sum\n";

Should work, although it could have been better at checking if there is a
real number (decimal or not).

Scalar::Util::looks_like_number

Ben
 
J

J. Romano

My question is how can I "pipe" some numbers, which I've
stored in a seperate Text-File "input.txt" to the PERL
Script from the DOS Command-line ?
I've tried this, but it don't work this way:
C:\>type input.txt | add.pl


You normally can't pipe output of one program to the command-line
of another, Gerhard. However, there is a Unix utility called "xargs"
that allows you to do just that, but it's not normally found on Win32
platforms.

You could always download it, but you can do exactly what you want
in Perl very easily with this command:

C:\> perl -lne "system(qq/add.pl $_/)" input.txt

Explanation:

* The -ne switches mean that the code that follows
is to be run for each line of input.txt
* The -l switch automatically chomp()s each line
(so you don't have to)
* The system() command runs the add.pl program
with the parameters specified in a line of
input.txt (which is exactly what you wanted)

And finally, I recommend that you add a newline to the print
statement of your original program, otherwise all the output values
will print all together on one line (with no spaces in-between).

I hope this helps, Gerhard.

-- Jean-Luc
 
J

Joe Smith

Tore said:
my $sum = 0;
while ( <STDIN> ) {
chomp;
last unless ( defined && /\d/ );
$sum += $_;
}
print "$sum\n";

I interpret the problem as needing to produce three lines of output.
14
6
12

# Command-line argument = name of input file(s), '-' for STDIN.
while (<>) {
my $sum = 0;
$sum += $_ foreach split;
print "$sum\n";
}

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top