print join to a file

L

LHradowy

I have been able to prompt user for data, then use join to add it to the
data. But, I need to go further.

First off I need to open the file, right now I just type script.pl <file>

Then I need to take the output of the data, and put it into a file. I have
tried opening then print to it, then close the file but it does not work.
Actually I need to do 3 things, print the join, then grep for CUST and send
that to a file, grep for TELN send that to a file, and grep -v and send
everything that does not have CUST of TELN to another file.

#!/opt/perl/bin/perl
system ("clear"); #Clear the screen
$acode="204";

$[ = 1; # set array base to 1
$, = ','; # set output field separator
$\ = "\n"; # set output record separator
Enter BLD: $room=<STDIN> ; chomp $room;
print "Enter ROOM:"; $room=<STDIN> ; chomp $room;

$[ = 1; # set array base to 1
$, = ','; # set output field separator
$\ = "\n"; # set output record separator

while(<ARGV>) {
chomp;
@a=split(",",$_);
print FH join(",",$acode.$a[0],$bld,$room,$a[2],$a[3],"\n") ;
}
 
B

Brian McCauley

LHradowy said:
Newsgroups: comp.lang.perl.misc,comp.lang.perl.tk

Can you explain why you think this has the slightest thing to do with TK?
I have been able to prompt user for data, then use join to add it to the
data. But, I need to go further.

First off I need to open the file, right now I just type script.pl <file>

You open files with open().
Then I need to take the output of the data, and put it into a file. I have
tried opening then print to it, then close the file but it does not
work.

Please show us a minimal but complete example of code in which you
have actually tried to do this and explain in what way it does not
work. (Code you posted would not compile and does not attempt to open
any output files). This, and much other, usefull advice can be found
in the posting guidelines for this group.
Actually I need to do 3 things, print the join, then grep for CUST and send
that to a file, grep for TELN send that to a file, and grep -v and send
everything that does not have CUST of TELN to another file.

#!/opt/perl/bin/perl
system ("clear"); #Clear the screen
$acode="204";

$[ = 1; # set array base to 1

Why are you doing this? Messing with $[ is very strongly discouraged.
Your code then later tries to access element 0 of an array which will
not, of course, exist if your array subscripts start at 1.
$, = ','; # set output field separator

Why are you doing this? There are no prints in your code that have
more than one argument.
$\ = "\n"; # set output record separator

Why are you doing this? You may have a reason but I suspect you are
just doing random things without knowing what they mean.
Enter BLD: $room=<STDIN> ; chomp $room;
print "Enter ROOM:"; $room=<STDIN> ; chomp $room;

That's not even syntactically valid Perl.

You should always declare all variables as lexically scoped in the
smallest applicable lexical scope unless you have a positive reason to
do otherwise. BTW: this is not perculliar to Perl, it applies in all
programming languges - allowing that a language not having lexical
variables is a positive reason :).

For Perl this means that most of the time the declaration of scalars
should be combined with the first assignment. BTW: this to is not
perculliar to Perl, it also applies in other programming languges
where assignment and declaration can be combined.

By following this convention you will be able to get maximum beniefit
out of putting "use strict" at the top of all your scripts.

Try to get into this habit now, do not wait for your failure to do so
to cause you the unecessary distress of wasting your own time and that
of other people. The longer you leave it the harder you will find it
to adjust. Worse still, if you leave it too long you may never adjust
and may mutate into a bitter and twisted troll.
$[ = 1; # set array base to 1
$, = ','; # set output field separator
$\ = "\n"; # set output record separator

Why are you doing these again? Perl is not going to have forgotten you know.
while(<ARGV>) {
chomp;
@a=split(",",$_);

The point of $_ is that it is the implicit "current thing that goes
without saying". So don't say it! :)
print FH join(",",$acode.$a[0],$bld,$room,$a[2],$a[3],"\n") ;

You have not opened FH.

Did you really want a comma before the newline?

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
J

Joe Smith

LHradowy said:
$[ = 1; # set array base to 1

Ugh. That is horrible. Don't do that.
Telling perl that you will be using $a[1] to mean the first element
in the array (as opposed to $a[0]) is not good perl programming.
print FH join(",",$acode.$a[0],$bld,$room,$a[2],$a[3],"\n") ;

You've told perl that you are never going to use $a[0], yet here
you are using it. With contradictions like that, the program won't work.

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top