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



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: CD

You'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.

Paul Lalli

.



Relevant Pages

  • How do I output an array twice via pipe instead of only once?
    ... second script. ... array, my script sends them both over at the same time. ... here is the inputted array: ...
    (perl.beginners)
  • Re: match string with slashes
    ... Did you notice that you didn't have to explicitly put a newline here? ... That's because the backticks returned the string with the newline ... $#arr is not the size of the array. ... Here is the result of running the script: ...
    (perl.beginners)
  • Re: Logon script - function array and select case not working
    ... this all works well, except, the function i am using for the rules in the control script causes alot of querrys to AD. as there are alot of groups. ... objTSout.writeline retrv ... So if you think that this will assign an array value to the variable, how do you think the case select statement is going go compare this array value with the literal string values such as "group name here"? ... However, by not assigning ANY value to checkgrp in the function, you are guaranteeing that, should the function ever exit, it will return no information. ...
    (microsoft.public.scripting.vbscript)
  • Re: string retrieval issue
    ... Chicago Bears|NFC North ... not writing the third element back to the array). ... You didn't include it in your script. ... Fear is the mind-killer. ...
    (comp.lang.perl.misc)
  • behavioral change of "read" builtin for sh on 8-CURRENT
    ... This script waits for terminal input for each of the above variables (INT, ... On 8-CURRENT if I hit Enter with no input at the prompt the system seems to ... recognize the newline as input and continues to sit there until I hit Enter ... Enter network interface: ...
    (freebsd-current)