Re: How do I output an array twice via pipe instead of only once?
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 30 Jan 2007 10:00:35 -0800
On Jan 30, 12:55 pm, "Paul Lalli" <mri...@xxxxxxxxx> wrote:
On Jan 30, 12:13 pm, junk2j...@xxxxxxxxxxx wrote:
I have a script that outputs an array which gets used as input in a
second script. The problem is that if I want to send over a second
array, my script sends them both over at the same time. It seems like
perl is trying to be too efficient. Here are the simplified scripts:
#!/usr/bin/perl
#
# a.pl
#
open(OUTPUT,"|b.pl");
@array1=("A","B");
print OUTPUT @array1;
@array2=("C","D");
print OUTPUT @array2;
---------------------------------------------------
#!/usr/bin/perl
#
# b.pl
#
@input=<STDIN>;
print "here is the inputted array: @input \n";
-----------------------------------------------------
What the script displays is:
here is the inputted array: ABCD
-----------------------------------------------------
But what I want is:being sent, but of what's being read. When you use the standard
here is the inputted array: AB
here is the inputted array: CDYou've got your problem backwards. It is not the fault of what's
<STDIN> operation, you are telling Perl to read until the next
newline. You did not send any newlines in the first send, so Perl
keeps waiting until it does receive a newline (or it receives end-of-
file, which happens when a.pl closes, thereby closing the pipe to
b.pl).
Either change your read to only read two characters at a time (see
perldoc -f read), or change your printing to send newlines after each
send.
Taking another look at what you said you wanted, the above is only
half of the answer. You're evaluating <STDIN> in list context,
meaning that it's going to read the entire stream, at once, into an
array. It waits for end-of-file regardless. The individual elements
of the array will be the newline-terminated strings sent by the first
program. If you want the actual message of "here is the inputted
array" to be printed once for each send, you need to make multiple
reads, usually in a loop. Change a.pl to print newlines after each
send:
@array1=("A","B");
print OUTPUT @array1, "\n";
@array2=("C","D");
print OUTPUT @array2, "\n";
and change b.pl to continuously read and print until there is no more
data:
while (my $input = <STDIN> ) {
print "Inputted string: $input";
}
Paul Lalli
.
- Follow-Ups:
- Re: How do I output an array twice via pipe instead of only once?
- From: nobull67@xxxxxxxxx
- Re: How do I output an array twice via pipe instead of only once?
- From: junk2junk
- Re: How do I output an array twice via pipe instead of only once?
- References:
- How do I output an array twice via pipe instead of only once?
- From: junk2junk
- Re: How do I output an array twice via pipe instead of only once?
- From: Paul Lalli
- How do I output an array twice via pipe instead of only once?
- Prev by Date: Re: How do I output an array twice via pipe instead of only once?
- Next by Date: Re: How do I output an array twice via pipe instead of only once?
- Previous by thread: Re: How do I output an array twice via pipe instead of only once?
- Next by thread: Re: How do I output an array twice via pipe instead of only once?
- Index(es):
Relevant Pages
|