Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > print join to a file

Reply
Thread Tools

print join to a file

 
 
LHradowy
Guest
Posts: n/a
 
      07-27-2004
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") ;
}


 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      07-27-2004
"LHradowy" <(E-Mail Removed)> writes:

> 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\\
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      07-27-2004
LHradowy wrote:

> $[ = 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
You must join New York Paid To Read !!!!!!! NewYork PTR is online!!! JOIN NOW!!!!!! Alan Silver ASP .Net 0 06-05-2006 03:27 PM
list.join()... re.join()...? Do they exist? (newbie questions...) googleboy Python 1 10-01-2005 12:56 PM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
print LIST vs print join "", LIST Xavier Noria Perl Misc 14 04-19-2004 01:44 PM



Advertisments