For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > December 2004 > Pipe input from a Text-File









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Pipe input from a Text-File
Gerhard

2004-12-03, 3:58 pm

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
Tore Aursand

2004-12-03, 3:58 pm

On Fri, 03 Dec 2004 05:30:04 -0800, Gerhard wrote:
> 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).


--
Tore Aursand <toreau@gmail.com>
"What we see depends mainly on what we look for." (Sir John Lubbock)
J. Romano

2004-12-04, 3:57 am

glange@libero.it (Gerhard) wrote in message news:<bfd7a7ec.0412030530.3f387002@posting.google.com>...
>
> 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
Ben Morrow

2004-12-04, 3:57 am


Quoth Tore Aursand <toreau@gmail.com>:
>
> 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

--
It will be seen that the Erwhonians are a m and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based
on the strictest morality. [Samuel Butler, paraphrased] ben@morrow.me.uk
Joe Smith

2004-12-07, 4:11 am

Tore Aursand wrote:

> 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com