perl Script error

Discussion in 'Perl Misc' started by Go Perl, Jul 9, 2003.

  1. Go Perl

    Go Perl Guest

    Thanks Eric Avick for helping me here
    The following is the perl script which is not giving out any output

    open (TT_FILE, ">sample.txt");
    while ($tt_line=<TT_FILE>) {
    chop($tt_line);
    my $i;
    my $total;
    @columns = split(/ +/, $tt_line);
    if ($i = ($columns[0] >= 700) .. ($columns[1] >= 700)) {
    $total += $columns[2];
    if ($i =~ /E0$/) {
    # end-of-range processing here
    $total = 0;
    }
    print "$total\n";
    }
    }
    close(TT_FILE);

    my sample.txt file is the following
    711 12 100
    12 13 80
    13 14 90
    14 714 85
    714 15 90
    15 16 45
    16 17 55
    17 18 50
    18 19 45
    19 20 56
    20 21 45
    21 22 34
    22 23 78
    23 723 87
    723 24 45
    24 25 34
    25 26 45
    26 27 65
    27 28 23
    28 29 23
    29 729 44
    729 30 77
    30 31 67
    31 32 87.4
    32 33 56.9
    33 34 66.1
    34 35 45.5
    35 36 78.4
    36 37 76.8
    37 38 2.8
    38 738 6.9

    when i am executing this script, i am not getting any output, where as
    it is supposed to print the sum of numbers between two pairs of 7xx
    series numbers from first column and second column.
    Go Perl, Jul 9, 2003
    #1
    1. Advertising

  2. Go Perl

    John Strauss Guest

    On 9 Jul 2003 08:08:06 -0700
    (Go Perl) wrote:
    >
    > Thanks Eric Avick for helping me here
    > The following is the perl script which is not giving out any output
    >
    > open (TT_FILE, ">sample.txt");
    > while ($tt_line=<TT_FILE>) {


    on the previous two lines, it looks like you are opening
    a file for writing, then trying to read from the filehandle.
    if i'm not making sense to you, read "perldoc -f open"

    use warnings; #this would have warned you about this.

    you are also not handling open() errors.

    perhaps you'd have been better off with:

    use warnings;
    open (TT_FILE, "<sample.txt") or die "cannot open sample.txt, '$!'\n";


    > chop($tt_line);

    minor point: use chomp() instead of chop()


    > my $i;
    > my $total;
    > @columns = split(/ +/, $tt_line);
    > if ($i = ($columns[0] >= 700) .. ($columns[1] >= 700)) {

    wow, that's a strange one, mate.


    > $total += $columns[2];
    > if ($i =~ /E0$/) {
    > # end-of-range processing here
    > $total = 0;
    > }
    > print "$total\n";
    > }
    > }
    > close(TT_FILE);

    you should change your indentation style for readability.



    >
    > my sample.txt file is the following
    > 711 12 100
    > 12 13 80
    > 13 14 90
    > 14 714 85
    > 714 15 90
    > 15 16 45
    > 16 17 55
    > 17 18 50
    > 18 19 45
    > 19 20 56
    > 20 21 45
    > 21 22 34
    > 22 23 78
    > 23 723 87
    > 723 24 45
    > 24 25 34
    > 25 26 45
    > 26 27 65
    > 27 28 23
    > 28 29 23
    > 29 729 44
    > 729 30 77
    > 30 31 67
    > 31 32 87.4
    > 32 33 56.9
    > 33 34 66.1
    > 34 35 45.5
    > 35 36 78.4
    > 36 37 76.8
    > 37 38 2.8
    > 38 738 6.9
    >
    > when i am executing this script, i am not getting any output, where as
    > it is supposed to print the sum of numbers between two pairs of 7xx
    > series numbers from first column and second column.


    i can't say i understand what you want, but i doubt you'll
    get it even if you fix your open() call. it would help *me*
    if you gave an example of the output you expect (though i
    grant that smarter news readers may have divined your intent.)

    from what i see, you never add anything to $total apart from
    entries in the third column, and you scope $total everytime
    you read a line from TT_FILE, so i don't see how you get a sum.
    maybe you want to move "mt $total;" outside your while() block?






    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    drop the .thetenant to get me via mail
    John Strauss, Jul 9, 2003
    #2
    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. Wet Basement
    Replies:
    1
    Views:
    2,505
    BobMonk
    Jul 15, 2003
  2. dpackwood
    Replies:
    3
    Views:
    1,768
  3. pj
    Replies:
    3
    Views:
    274
  4. Petterson Mikael

    Execute another perl script from my perl script

    Petterson Mikael, Jan 5, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    127
    Paul Lalli
    Jan 5, 2005
  5. Replies:
    20
    Views:
    672
    Gunnar Hjalmarsson
    Jan 18, 2005
Loading...

Share This Page