LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   need help in programming.. (http://www.linuxquestions.org/questions/programming-9/need-help-in-programming-517371/)

jagankumarmehta 01-08-2007 04:45 AM

need help in programming..
 
Hi,

i have one file with the name of region.txt.

In that file i have counts for the particular hour.Given below:-
hour count
00 641
01 706
02 604
03 559
04 264
06 1
09 8
10 4
11 2
13 1
14 12
16 1
19 2
20 1
22 66
23 1

I want to print the count hour by hour in the html page.I need the coding for that.

acid_kewpie 01-08-2007 04:55 AM

what do you mean "in the html page"?? you need to agive a *LOT* more information than that... you've not even said what langauge you're using.

Wim Sturkenboom 01-08-2007 05:04 AM

Just put some HTML tags around it :)

Basic html page:
Code:

<html>
<head>
</head>
<body>
paste your stuff here
</body>
</html>

And you can use <br> for line breaks.

PS
Usually you try things first, get stuck and post a question indicating what the problem is.

jagankumarmehta 01-08-2007 07:38 AM

Quote:

Originally Posted by Wim Sturkenboom
Just put some HTML tags around it :)

Basic html page:
Code:

<html>
<head>
</head>
<body>
paste your stuff here
</body>
</html>

And you can use <br> for line breaks.

PS
Usually you try things first, get stuck and post a question indicating what the problem is.


I am using Linux & this is the output of the one shell script which has been written by me.I have to arrange this in the form so that i can be able to put that data in wabpage hour by hour..

taylor_venable 01-08-2007 09:53 AM

Different Names for the Same Thing
 
Solution in Awk:
Code:

BEGIN { printf "<html><head><title>Awk!</title></head><body><table>\n" }
{ printf "<tr><td>%d</td><td>%d</td></tr>\n", $1, $2 };
END { printf "</table></body></html>\n" }

Run it with `awk -f SCRIPT DATAFILE > OUTPUT`.

Solution in C:
Code:

#include <ctype.h>
#include <stdio.h>

int main(int argc, char** argv) {
    FILE* fp = fopen("data.txt", "r");

    char c = '_';
    char datum[1024];

    int i = 0, j = 0;

    printf("<html><head><title>C!</title></head><body><table>\n");

    while ((c = fgetc(fp)) != EOF) {
        if (isdigit(c)) {
            datum[i++] = c;
        }
        else if (isspace(c)) {
            datum[i++] = '\0';
            if (c != '\n') {
                j = i;
            }
            else {
                printf("<tr><td>%s</td><td>%s</td></tr>\n", datum, datum + j);
                i = 0; j = 0;
            }
        }
    }

    printf("</table></body></html>\n");

    return 0;
}

Compile this with `gcc SOURCE` and run `a.out`, or specify the output binary with `gcc -o BINARY SOURCE`.

Solution in Java:
Code:

import java.io.*;

class genHtml {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("data.txt"));
            String s;

            System.out.println("<html><head><title>Java!</title></head><body><table>");

            while ((s = br.readLine()) != null) {
                String[] data = s.split(" ");
                System.out.println("<tr><td>" + data[0] + "</td><td>" + data[1] + "</td></tr>");
            }

            br.close();

            System.out.println("</table></body></html>");
        }
        catch (Exception e) {
            System.err.println("ERROR!");
        }
    }
}

Compile and then run like any other Java class, probably using `javac SOURCE` and then `java genHtml`.

Solution in Perl:
Code:

#!/usr/bin/env perl

use warnings;
use strict;

open my $file, "data.txt";

print "<html><head><title>Perl!</title></head><body><table>\n";

while (<$file>) {
    my $line = $_; chomp $line;
    my @data = split /\s/, $line;
    print "<tr><td>$data[0]</td><td>$data[1]</td></tr>\n";
}

print "</table></body></html>\n";

Run it by setting its execute bit and running it like a program.

Solution in Python:
Code:

#!/usr/bin/env python

print "<html><head><title>Python!</title></head><body><table>"

f = open("data.txt")

for line in f.readlines():
    data = line.split()
    print "<tr><td>" + data[0] + "</td><td>" + data[1] + "</td></tr>"

f.close()

print "</table></body></html>"

Run it by setting the file's executable bit and invoking it like a program.

Solution in Ruby:
Code:

#!/usr/bin/env ruby

puts "<html><head><title>Ruby!</title></head><body><table>"

File.open "data.txt" do |file|
    file.each do |line|
        data = line.split
        puts "<tr><td>" + data[0] + "</td><td>" + data[1] + "</td></tr>"
    end
end

puts "</table></body></html>"

Again, just set the executable bit and then run it like any other program.

Solution in Scheme:
Code:

(define (doit input-file)
  (let ((datum (read input-file)))
    (if (not (eof-object? datum))
        (begin
          (display "<tr><td>")
          (display datum)
          (display "</td><td>")
          (display (read input-file))
          (display "</td></tr>\n")
          (doit input-file))
        '())))

(define (init input-file)
  (display "<html><head><title>Scheme!</title></head><body><table>\n")
  (doit input-file)
  (display "</table></body></html>\n")
  '())

(call-with-input-file "data.txt" init)

Run this with your Scheme implementation. PLT Scheme, for example, uses `mzscheme -f SOURCE` to execute it.

Solution in Sed:
Code:

1i\
<html><head><title>Sed!</title></head><body><table>
s/^/<tr><td>/
s/$/<\/td><\/tr>/
s/ /<\/td><td>/
$a\
</table></body></html>

You can execute this on your data file and redirect output by running `sed -f SOURCE DATAFILE > OUTPUT`.

Solution in Standard ML:
Code:

fun getList stream =
    case TextIO.inputLine stream of
        NONE => []
      | SOME(v) => String.tokens ( fn #" "  => true
                                    | #"\n" => true
                                    | _    => false ) v;

fun doit stream =
    case getList stream of
        [] => ()
      | xs => ( print ( "<tr><td>" ^ List.nth (xs, 0)
                        ^ "</td><td>" ^ List.nth (xs, 1)
                        ^ "</td></tr>\n" ) ;
                doit stream ) ;

fun main file =
    ( print "<html><head><title>Standard ML!</title></head><body><table>\n";
      doit (TextIO.openIn file);
      print "</table></body></html>\n" ) ;

val () = main "data.txt";

You can compile it with `mlton SOURCE` and then run the resulting binary, or execute it with `main DATAFILE` from your favorite SML REPL.

Solution in TCL:
Code:

#!/usr/bin/env tcl

set channelId [open data.txt r]
set line ""

puts "<html><head><title>TCL!</title></head><body><table>"

while { [gets $channelId line] > 1 } {
    regexp {([0-9]+)\s+([0-9]+)} $line match submatch(0) submatch(1)
    puts "<tr><td>$submatch(0)</td><td>$submatch(1)</td></tr>"
}

puts "</table></body></html>"

close $channelId

Run this by setting the executable bit of the source file and invoking it like any other script. You may have to change the first line to use `tclsh` instead of `tcl`, depending on your setup.

Wim Sturkenboom 01-08-2007 02:09 PM

There is 'something' that you can put in the head section of the above html page. That way you can let the browser refresh the page every hour.

Unfortunately you have to figure that out yourself (as I'm not 100% sure at this moment).

95se 01-08-2007 08:01 PM

Quote:

Originally Posted by taylor_venable
Solution in Awk:
...
Solution in C:
...
Solution in Java:
...
Solution in Perl:
...
Solution in Python:
...
Solution in Ruby:
...
Solution in Scheme:
...
Solution in Sed:
...
Solution in Standard ML:
...
Solution in TCL:

Haha, awesome. Thanks for that reply!

jagankumarmehta 01-08-2007 11:52 PM

Dear all

Thanks a lot for your support..............

jagankumarmehta 01-09-2007 06:01 AM

Hi Taylor,

If we have more file like this then how we can do.
Describe in C language.

AnanthaP 01-09-2007 06:11 AM

Hey Jagan,

As a principle you should read up HTML tags and how HTML means that your ordinary text (hourly readings in this case) are "marked up" with the tags (no hyper text in your case). You wont always have a ready forum for your code reference.

End

taylor_venable 01-09-2007 10:35 AM

Quote:

Originally Posted by jagankumarmehta
If we have more file like this then how we can do.
Describe in C language.

Here you go:
Code:

#include <ctype.h>
#include <stdio.h>

void genHtml(char* infile, char* outfile) {
    FILE* ins = fopen(infile, "r");
    FILE* outs = fopen(outfile, "w");

    char c = '_';
    char datum[1024];

    int i = 0, j = 0;

    fprintf(outs, "<html><head><title>C!</title></head><body><table>\n");

    while ((c = fgetc(ins)) != EOF) {
        if (isdigit(c)) {
            datum[i++] = c;
        }
        else if (isspace(c)) {
            datum[i++] = '\0';
            if (c != '\n') {
                j = i;
            }
            else {
                fprintf(outs, "<tr><td>%s</td><td>%s</td></tr>\n", datum, datum + j);
                i = 0; j = 0;
            }
        }
    }

    fprintf(outs, "</table></body></html>\n");

    fclose(ins);
    fclose(outs);
}


int main(int argc, char** argv) {
    int i = 1;
    for (; i + 1 < argc; i += 2) {
        genHtml(argv[i], argv[i+1]);
    }
    return 0;
}

Compile and run, specifying for arguments pairs of the input and output files to be used. For example, `./genHtml input.txt output.txt` or `./genHtml input1.txt output1.txt input2.txt output2.txt`. Don't worry about specifying the wrong number of parameters, like `./genHtml input.txt` because the unpaired value won't get used! (The same goes for any odd number of arguments, the last one is discarded.)

BTW, caveat emptor, this code shouldn't be considered extremely safe or totally correct. It's written in C for one. :) The maximum line size of 1023 characters. It also presumes that the file is in the right format, and won't complain if it's not. It's also undocumented, so don't use it if you don't understand it. (You can ask me for help on that, if you need too, though.)

You'd really probably be better off with a shell script that does the generation of the file, then uses Awk to create the HTML all at once. Then add it to cron so it runs every hour.

Alternatively, when you generate this data you could do it in XML and then use an XSLT stylesheet have your client browser generate HTML from it (the XML data). This would give your data more form (in XML) for other processing if necessary. Its also lighter on the server load. But it may lead to bloated files if you have a LOT of them, and the data doesn't have that much structure anyway. Just a thought.


All times are GMT -5. The time now is 08:05 AM.