print join to a file

Discussion in 'Perl Misc' started by LHradowy, Jul 27, 2004.

  1. LHradowy

    LHradowy Guest

    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") ;
    }
     
    LHradowy, Jul 27, 2004
    #1
    1. Advertising

  2. "LHradowy" <> 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\\
     
    Brian McCauley, Jul 27, 2004
    #2
    1. Advertising

  3. LHradowy

    Joe Smith Guest

    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
     
    Joe Smith, Jul 27, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    912
    Alan Silver
    Jun 5, 2006
  2. googleboy
    Replies:
    1
    Views:
    953
    Benji York
    Oct 1, 2005
  3. keto
    Replies:
    0
    Views:
    1,043
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    399
    David Cournapeau
    Dec 30, 2008
  5. Xavier Noria

    print LIST vs print join "", LIST

    Xavier Noria, Apr 16, 2004, in forum: Perl Misc
    Replies:
    14
    Views:
    206
    Uri Guttman
    Apr 19, 2004
Loading...

Share This Page