Re: How do I output an array twice via pipe instead of only once?



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:
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
being sent, but of what's being read. When you use the standard
<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

.



Relevant Pages

  • Re: How do I output an array twice via pipe instead of only once?
    ... array, my script sends them both over at the same time. ... print OUTPUT @array1; ... You did not send any newlines in the first send, ...
    (perl.beginners)
  • Re: Printing an Array: Formatting Problem
    ... Some of the consecutive elements have the same IP address, ... How can I format the printed output of this array so that the elements with ... the current element followed by two newlines instead of one. ...
    (comp.lang.perl.misc)
  • Re: Posting Usenet News Messages
    ... >> This depends on how the NNTP server interprets it. ... > assumptions about where you wanted newlines. ... > array 'lines' doesn't make it actual lines; you should put a newline at ... CR/LF pair as a "blank" line, so I've tried combinations of LF and CR/LF to ...
    (comp.lang.perl.misc)
  • Re: Printing an Array: Formatting Problem
    ... How can I format the printed output of this array so that the elements with ... the current element followed by two newlines instead of one. ... Re-factor your datastructure to use a hash of array reference, ... Loop through the keys, and print each of the value's ...
    (comp.lang.perl.misc)
  • Re: bullet proof for loop over $(find) in bash, how?
    ... > blanks, newlines or tabs? ... > be possible to fill an array with each file name found, ...
    (comp.unix.shell)