Simple PERL Question

Discussion in 'Programming' started by oncy, May 24, 2008.

  1. #1
    Hello,

    Now I want to ask a very simple question. THe following PERL code puts the data on the screen but I want it to put the data in a file "throughput.txt" Can you add this line to put the data into a file instad of putting the data on screen? Is not it very simple please help me!

    #Code

    # explanation: perl throughput.pl <trace file> <granlarity>

    $infile=$ARGV[0];

    $granularity=$ARGV[1];

    $sum=0;

    $sum_total=0;

    $clock=0;

    $init=0;

    open (DATA,"<$infile")

    || die "Can't open $infile $!";



    while (<DATA>) {

    @x = split(' ');

    if($init==0){

    $start=$x[1];

    $init=1;
    }


    if ($x[1]-$clock <= $granularity)
    {

    $sum=$sum+$x[2];

    $sum_total=$sum_total+$x[2];
    }

    else

    {
    $throughput=$sum*8.0/$granularity;

    print STDOUT "$x[1]: $throughput bps\n";

    $clock=$clock+$granularity;

    $sum=$sum+$x[1];

    $sum=0;
    }

    }

    $endtime=$x[1];

    $throughput=$sum*8.0/$granularity;

    print STDOUT "$x[1]: $throughput bps\n";

    $clock=$clock+$granularity;

    $sum=0;

    #print STDOUT "$sum_total $start $endtime\n";

    $avgrate=$sum_total*8.0/($endtime-$start);

    print STDOUT "Average rate: $avgrate bps\n";

    close DATA;

    exit(0);
     
    oncy, May 24, 2008 IP
  2. milesbparty

    milesbparty Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You are explicitly writing your output to STDOUT:

    You need to open a new file handle to write to:

    $outfile = "throughput.txt";
    open (OUTFILE, ">> $outfile");


    Then instead of printing to STDOUT, print to OUTFILE:

    print OUTFILE "$x[1]: $throughput bps\n";
    close OUTFILE;
     
    milesbparty, May 24, 2008 IP
  3. oncy

    oncy Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Can you modify it? Because where should I use open and outfiles? There 5-6 stdouts....
     
    oncy, May 24, 2008 IP