Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hiya fellas-
So I have this project I need to do, where the teacher sends the input a weird way and I wondered if anybody knows anything about it:The input is supposed to be read using STDIN (I'm writing it in perl but I could do it in java as well), but he is doing it some way in linux where you can specify a file on the command line when you call the program and linux sends the file via STDIN (ie instead of typing it.)
Anybody know how this is done? I have access to a linux box I can test it on but I don't know the syntax...I heard something about an arrow operator?
Does linux also send the end of file character? I'm trying to figure out how I can tell when I'm done with the input.
My program works fine when I give it the input other ways, I just want to ensure I don't lose 60% of the points due to some stupid I/O error specific to how he's sending in the input.
In case you're curious, the program converts a DFA into a regular expression.
Thanks,
SN

Okay, I figured out the syntax of how to test:
perl 355.pl<"input.txt"
Is this the only way? I think this is what he's talking about.Here's the first part of my program - Just puts all the input into $buffer.
#!/usr/bin/perl
$char="a";
while($char ne "")
{
read(STDIN, $char, 1);
$buffer=$buffer.$char;
}
print "argument was:\n";
print $buffer;
print "\nargument ended.\n"
Is this obscenely inefficient? Any better ways to do it?Thanks again,
-SN

There are several ways to do it.
perl 355.pl input.txt
#!/usr/bin/perl
while(<>) { # could be written as while(<STDIN>)
$line = $_;
print $line;
# do something with the line
}**********
option 2
@file = <>;
foreach (@file) {
# do something
}**********
option 3
{
local $/;
$file = <>; # entire file is loaded into $file
}I can probably come up with a couple of other metods.

Fish-
Thanks a lot! I like option 3...It works great in the program, and seems more elegant than using a loop. Although I don't understand how it works...what is the $/?Thanks again,
-SN

$/ is the scalar that defines the input record separator (which normally is \n), but that line "undefines" it so you can slurp up the entire file into a single scalar. This is a quick and "dirty" way to read-in the file, but it could make it more difficult to work with the data, depending on what you need to do with it.

SN,
I think you and I are just about the only ones on this site using Perl. For more indepth help, you might look at:

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |