Creating .txt/.html file using perl script

Discussion in 'Perl Misc' started by jdblackford@gmail.com, May 12, 2007.

  1. Guest

    Hello,

    I was wondering if it was possible to create simple program that will
    create .txt (or .html) files based on information provided via
    prompts. If so, would someone be willing to assist me with this? I
    know absolutely nothing about Perl (or any other programming language
    for that matter)

    For instance, if I wanted to create a file called 01.20.07.html based
    off of a specific date (01 is MM, 20 is DD, 07 is YY) that looked like
    this:

    <html>
    <body>
    <table>
    <tr><td><img src="http://www.mywebsite.com/01.20.07/1.JPG">
    <td><img src="http://www.mywebsite.com/01.20.07/2.JPG">
    <td><img src="http://www.mywebsite.com/01.20.07/3.JPG">
    </tr>
    <tr><td><img src="http://www.mywebsite.com/01.20.07/4.JPG">
    <td><img src="http://www.mywebsite.com/01.20.07/5.JPG">
    <td><img src="http://www.mywebsite.com/01.20.07/6.JPG">
    </tr>
    </body>
    </html>

    I would like to be able to enter the following information when
    prompted by the program:

    MM
    DD
    YY
    Number of images

    If possible, I would like to have the code know how many table rows to
    create (in intervals of 3) based on the number of images.

    Is this something someone here can assist with?
    , May 12, 2007
    #1
    1. Advertising

  2. Mirco Wahab Guest

    wrote:
    > I was wondering if it was possible to create simple program that will
    > create .txt (or .html) files based on information provided via
    > prompts. If so, would someone be willing to assist me with this? I
    > know absolutely nothing about Perl (or any other programming language
    > for that matter)
    >
    > For instance, if I wanted to create a file called 01.20.07.html based
    > off of a specific date (01 is MM, 20 is DD, 07 is YY) that looked like
    > this:


    So you would like call it like

    perl myprogram.pl 01 20 07 9

    to produce the desired output?


    > I would like to be able to enter the following information when
    > prompted by the program:
    > MM
    > DD
    > YY
    > Number of images


    You already had to type the program name in order
    to start it, so adding the parameters directly would
    simplify the program and allows to mechanize your work
    later.

    Or did you think of 'clicking a file in explorer and
    entering info then'?

    > If possible, I would like to have the code know how many table rows to
    > create (in intervals of 3) based on the number of images.
    > Is this something someone here can assist with?


    I'm not really sure what you intend to solve and
    what would be the best way to do it.

    Your specification is 'proforma' (without "entering info then")
    fulfilled by a short perl script like:
    ==>
    use strict;
    use warnings;
    use CGI qw':standard';


    my $link = 'http://www.mywebsite.com/$$D.$$M.$$Y/$$N.JPG';
    my %transf = ( D => shift, M => shift, Y => shift );
    $link =~ s/\$\$$_/$transf{$_}/ for keys %transf;
    my ($n, $rows) = (1, (shift)/3);

    my $html =
    start_html('Image Gallery')
    . table(
    Tr([
    map
    td([ map {my $l=$link; $l=~s/\$\$N/$n++/e; img({src=>$l})."\n"} 1..3 ] ),
    1..$rows
    ])
    )
    . end_html();

    open my $fhd, '>', 'jdblackford.html' or die "cant't write files: $!";
    print $fhd $html;
    close $fhd;
    <==


    which you might invoke as said on line 14 of this posting.

    But maybe you give us some more information first -
    why Perl for example (if you didn't ever touch it)
    and so on.

    Regards

    Mirco
    Mirco Wahab, May 12, 2007
    #2
    1. Advertising

  3. Dr.Ruud Guest

    schreef:

    > I was wondering if it was possible to create simple program that will
    > create .txt (or .html) files based on information provided via
    > prompts. [...]
    > <td><img src="http://www.mywebsite.com/01.20.07/1.JPG">
    > <td><img src="http://www.mywebsite.com/01.20.07/2.JPG">
    > <td><img src="http://www.mywebsite.com/01.20.07/3.JPG">
    > </tr>
    > <tr>
    > <td><img src="http://www.mywebsite.com/01.20.07/4.JPG">
    > <td><img src="http://www.mywebsite.com/01.20.07/5.JPG">
    > <td><img src="http://www.mywebsite.com/01.20.07/6.JPG">


    It looks like you already know the filenames up ahead.
    So why not let your code work from that infomation?

    perldoc -f glob
    perldoc -f readdir


    Or create a file like

    2007-01-20,6
    2007-01-21,2

    etc., and let your code use that.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, May 13, 2007
    #3
  4. Xicheng Jia Guest

    On May 12, 5:45 pm, wrote:
    > Hello,
    >
    > I was wondering if it was possible to create simple program that will
    > create .txt (or .html) files based on information provided via
    > prompts. If so, would someone be willing to assist me with this? I
    > know absolutely nothing about Perl (or any other programming language
    > for that matter)
    >
    > For instance, if I wanted to create a file called 01.20.07.html based
    > off of a specific date (01 is MM, 20 is DD, 07 is YY) that looked like
    > this:
    >
    > <html>
    > <body>
    > <table>
    > <tr><td><img src="http://www.mywebsite.com/01.20.07/1.JPG">
    > <td><img src="http://www.mywebsite.com/01.20.07/2.JPG">
    > <td><img src="http://www.mywebsite.com/01.20.07/3.JPG">
    > </tr>
    > <tr><td><img src="http://www.mywebsite.com/01.20.07/4.JPG">
    > <td><img src="http://www.mywebsite.com/01.20.07/5.JPG">
    > <td><img src="http://www.mywebsite.com/01.20.07/6.JPG">
    > </tr>
    > </body>
    > </html>
    >
    > I would like to be able to enter the following information when
    > prompted by the program:
    >
    > MM
    > DD
    > YY
    > Number of images
    >
    > If possible, I would like to have the code know how many table rows to
    > create (in intervals of 3) based on the number of images.
    >
    > Is this something someone here can assist with?


    You might want to check any Perl templating modules like TT(Template
    Toolkits),

    http://search.cpan.org/dist/Template-Toolkit/

    which could possibly make your stuff much easier.(in your case, the
    template file need just several inputs and a simple loop.)

    BTW. if you can use <div> elements instead of table to organize your
    output, that might be much easier, my 2 cents. :)

    Regards,
    Xicheng
    Xicheng Jia, May 13, 2007
    #4
  5. Xicheng Jia Guest

    On May 13, 11:53 am, Xicheng Jia <> wrote:
    > On May 12, 5:45 pm, wrote:
    >
    >
    >
    >
    >
    > > Hello,

    >
    > > I was wondering if it was possible to create simple program that will
    > > create .txt (or .html) files based on information provided via
    > > prompts. If so, would someone be willing to assist me with this? I
    > > know absolutely nothing about Perl (or any other programming language
    > > for that matter)

    >
    > > For instance, if I wanted to create a file called 01.20.07.html based
    > > off of a specific date (01 is MM, 20 is DD, 07 is YY) that looked like
    > > this:

    >
    > > <html>
    > > <body>
    > > <table>
    > > <tr><td><img src="http://www.mywebsite.com/01.20.07/1.JPG">
    > > <td><img src="http://www.mywebsite.com/01.20.07/2.JPG">
    > > <td><img src="http://www.mywebsite.com/01.20.07/3.JPG">
    > > </tr>
    > > <tr><td><img src="http://www.mywebsite.com/01.20.07/4.JPG">
    > > <td><img src="http://www.mywebsite.com/01.20.07/5.JPG">
    > > <td><img src="http://www.mywebsite.com/01.20.07/6.JPG">
    > > </tr>
    > > </body>
    > > </html>

    >
    > > I would like to be able to enter the following information when
    > > prompted by the program:

    >
    > > MM
    > > DD
    > > YY
    > > Number of images

    >
    > > If possible, I would like to have the code know how many table rows to
    > > create (in intervals of 3) based on the number of images.

    >
    > > Is this something someone here can assist with?

    >
    > You might want to check any Perl templating modules like TT(Template
    > Toolkits),
    >
    > http://search.cpan.org/dist/Template-Toolkit/
    >
    > which could possibly make your stuff much easier.(in your case, the
    > template file need just several inputs and a simple loop.)
    >
    > BTW. if you can use <div> elements instead of table to organize your
    > output, that might be much easier, my 2 cents. :)
    >
    > Regards,
    > Xicheng- Hide quoted text -
    > - Show quoted text -


    Below is a TT test code for your needs, you actually only need to
    figure a data structure and then TT can do the other stuff pretty
    easily:

    you run it this way: ./ex1.pl 01.20.70 13 5

    If you can use <div> and set CSS styles to control their (float|
    width|...)s, then a plain 1-D array will do all stuff...

    Regards,
    Xicheng

    #### ex1.pl ####
    #!/usr/bin/perl
    use warnings;
    use strict;
    use Template;

    if (@ARGV < 2) {
    print "./ex1.pl mm.dd.yy numPic [numCol]\n";
    exit;
    }
    my ($date, $numPic) = @ARGV;
    my $numCol = $ARGV[2] || 3;

    my $residuals = $numCol - ($numPic % $numCol);
    my $items;
    my $n = 0;

    for my $item (1..$numPic) {
    push @{$items->[$n]}, $item;
    $n++ if not $item % $numCol;
    }
    push @{$items->[$n]}, (0) x $residuals;

    my $tt = Template->new();
    my $input = 'ex1.tt';
    my $args = {
    site => $date,
    items => $items,
    };
    $tt->process($input, $args, "$date.html") or die $tt->error( );

    __END__

    #### ex1.tt ####
    <html>
    <head>
    <title>TT test page</title>
    </head>
    <body>
    <table>
    [% FOREACH item IN items -%]
    <tr>
    [% FOREACH cell IN item -%]
    [% IF !cell -%]
    <td>&nbsp;</td>
    [% ELSE -%]
    <td><img src="http://www.mywebsite.com/[% site %]/[% cell
    %].JPG"></td>
    [% END -%]
    [% END -%]
    </tr>
    [% END -%]
    </table>
    </body>
    </html>
    Xicheng Jia, May 13, 2007
    #5
  6. Xicheng Jia Guest

    On May 13, 6:47 pm, Xicheng Jia <> wrote:
    > On May 13, 11:53 am, Xicheng Jia <> wrote:
    >
    >
    >
    >
    >
    > > On May 12, 5:45 pm, wrote:

    >
    > > > Hello,

    >
    > > > I was wondering if it was possible to create simple program that will
    > > > create .txt (or .html) files based on information provided via
    > > > prompts. If so, would someone be willing to assist me with this? I
    > > > know absolutely nothing about Perl (or any other programming language
    > > > for that matter)

    >
    > > > For instance, if I wanted to create a file called 01.20.07.html based
    > > > off of a specific date (01 is MM, 20 is DD, 07 is YY) that looked like
    > > > this:

    >
    > > > <html>
    > > > <body>
    > > > <table>
    > > > <tr><td><img src="http://www.mywebsite.com/01.20.07/1.JPG">
    > > > <td><img src="http://www.mywebsite.com/01.20.07/2.JPG">
    > > > <td><img src="http://www.mywebsite.com/01.20.07/3.JPG">
    > > > </tr>
    > > > <tr><td><img src="http://www.mywebsite.com/01.20.07/4.JPG">
    > > > <td><img src="http://www.mywebsite.com/01.20.07/5.JPG">
    > > > <td><img src="http://www.mywebsite.com/01.20.07/6.JPG">
    > > > </tr>
    > > > </body>
    > > > </html>

    >
    > > > I would like to be able to enter the following information when
    > > > prompted by the program:

    >
    > > > MM
    > > > DD
    > > > YY
    > > > Number of images

    >
    > > > If possible, I would like to have the code know how many table rows to
    > > > create (in intervals of 3) based on the number of images.

    >
    > > > Is this something someone here can assist with?

    >
    > > You might want to check any Perl templating modules like TT(Template
    > > Toolkits),

    >
    > > http://search.cpan.org/dist/Template-Toolkit/

    >
    > > which could possibly make your stuff much easier.(in your case, the
    > > template file need just several inputs and a simple loop.)

    >
    > > BTW. if you can use <div> elements instead of table to organize your
    > > output, that might be much easier, my 2 cents. :)

    >
    > > Regards,
    > > Xicheng- Hide quoted text -
    > > - Show quoted text -

    >
    > Below is a TT test code for your needs, you actually only need to
    > figure a data structure and then TT can do the other stuff pretty
    > easily:
    >
    > you run it this way: ./ex1.pl 01.20.70 13 5
    >
    > If you can use <div> and set CSS styles to control their (float|
    > width|...)s, then a plain 1-D array will do all stuff...
    >
    > Regards,
    > Xicheng
    >
    > #### ex1.pl ####
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    > use Template;
    >
    > if (@ARGV < 2) {
    > print "./ex1.pl mm.dd.yy numPic [numCol]\n";
    > exit;}
    >
    > my ($date, $numPic) = @ARGV;
    > my $numCol = $ARGV[2] || 3;
    >
    > my $residuals = $numCol - ($numPic % $numCol);
    > my $items;
    > my $n = 0;
    >
    > for my $item (1..$numPic) {
    > push @{$items->[$n]}, $item;
    > $n++ if not $item % $numCol;}
    >


    XC> push @{$items->[$n]}, (0) x $residuals;

    A bug from the above line, should be :

    push @{$items->[$n]}, (0) x $residuals if not $residuals == $numCol;

    Regards,
    Xicheng

    >
    > my $tt = Template->new();
    > my $input = 'ex1.tt';
    > my $args = {
    > site => $date,
    > items => $items,};
    >
    > $tt->process($input, $args, "$date.html") or die $tt->error( );
    >
    > __END__
    >
    > #### ex1.tt ####
    > <html>
    > <head>
    > <title>TT test page</title>
    > </head>
    > <body>
    > <table>
    > [% FOREACH item IN items -%]
    > <tr>
    > [% FOREACH cell IN item -%]
    > [% IF !cell -%]
    > <td>&nbsp;</td>
    > [% ELSE -%]
    > <td><img src="http://www.mywebsite.com/[% site %]/[% cell
    > %].JPG"></td>
    > [% END -%]
    > [% END -%]
    > </tr>
    > [% END -%]
    > </table>
    > </body>
    > </html>- Hide quoted text -
    >
    > - Show quoted text -
    Xicheng Jia, May 14, 2007
    #6
  7. Xicheng Jia Guest

    On May 15, 12:59 am, wrote:
    > On 13 May 2007 16:50:33 -0700, Xicheng Jia <> wrote:
    >
    >
    >
    >
    >
    > >On May 13, 6:47 pm, Xicheng Jia <> wrote:
    > >> On May 13, 11:53 am, Xicheng Jia <> wrote:

    >
    > >> > On May 12, 5:45 pm, wrote:

    >
    > >> > > Hello,

    >
    > >> > > I was wondering if it was possible to create simple program that will
    > >> > > create .txt (or .html) files based on information provided via
    > >> > > prompts. If so, would someone be willing to assist me with this? I
    > >> > > know absolutely nothing about Perl (or any other programming language
    > >> > > for that matter)

    >
    > >> > > For instance, if I wanted to create a file called 01.20.07.html based
    > >> > > off of a specific date (01 is MM, 20 is DD, 07 is YY) that looked like
    > >> > > this:

    >
    > >> > > <html>
    > >> > > <body>
    > >> > > <table>
    > >> > > <tr><td><img src="http://www.mywebsite.com/01.20.07/1.JPG">
    > >> > > <td><img src="http://www.mywebsite.com/01.20.07/2.JPG">
    > >> > > <td><img src="http://www.mywebsite.com/01.20.07/3.JPG">
    > >> > > </tr>
    > >> > > <tr><td><img src="http://www.mywebsite.com/01.20.07/4.JPG">
    > >> > > <td><img src="http://www.mywebsite.com/01.20.07/5.JPG">
    > >> > > <td><img src="http://www.mywebsite.com/01.20.07/6.JPG">
    > >> > > </tr>
    > >> > > </body>
    > >> > > </html>

    >
    > >> > > I would like to be able to enter the following information when
    > >> > > prompted by the program:

    >
    > >> > > MM
    > >> > > DD
    > >> > > YY
    > >> > > Number of images

    >
    > >> > > If possible, I would like to have the code know how many table rows to
    > >> > > create (in intervals of 3) based on the number of images.

    >
    > >> > > Is this something someone here can assist with?

    >
    > >> > You might want to check any Perl templating modules like TT(Template
    > >> > Toolkits),

    >
    > >> > http://search.cpan.org/dist/Template-Toolkit/

    >
    > >> > which could possibly make your stuff much easier.(in your case, the
    > >> > template file need just several inputs and a simple loop.)

    >
    > >> > BTW. if you can use <div> elements instead of table to organize your
    > >> > output, that might be much easier, my 2 cents. :)

    >
    > >> > Regards,
    > >> > Xicheng- Hide quoted text -
    > >> > - Show quoted text -

    >
    > >> Below is a TT test code for your needs, you actually only need to
    > >> figure a data structure and then TT can do the other stuff pretty
    > >> easily:

    >
    > >> you run it this way: ./ex1.pl 01.20.70 13 5

    >
    > >> If you can use <div> and set CSS styles to control their (float|
    > >> width|...)s, then a plain 1-D array will do all stuff...

    >
    > >> Regards,
    > >> Xicheng

    >
    > >> #### ex1.pl ####
    > >> #!/usr/bin/perl
    > >> use warnings;
    > >> use strict;
    > >> use Template;

    >
    > >> if (@ARGV < 2) {
    > >> print "./ex1.pl mm.dd.yy numPic [numCol]\n";
    > >> exit;}

    >
    > >> my ($date, $numPic) = @ARGV;
    > >> my $numCol = $ARGV[2] || 3;

    >
    > >> my $residuals = $numCol - ($numPic % $numCol);
    > >> my $items;
    > >> my $n = 0;

    >
    > >> for my $item (1..$numPic) {
    > >> push @{$items->[$n]}, $item;
    > >> $n++ if not $item % $numCol;}

    >
    > >XC> push @{$items->[$n]}, (0) x $residuals;

    >
    > >A bug from the above line, should be :

    >
    > > push @{$items->[$n]}, (0) x $residuals if not $residuals == $numCol;

    >
    > >Regards,
    > >Xicheng

    >
    > >> my $tt = Template->new();
    > >> my $input = 'ex1.tt';
    > >> my $args = {
    > >> site => $date,
    > >> items => $items,};

    >
    > >> $tt->process($input, $args, "$date.html") or die $tt->error( );

    >
    > >> __END__

    >
    > >> #### ex1.tt ####
    > >> <html>
    > >> <head>
    > >> <title>TT test page</title>
    > >> </head>
    > >> <body>
    > >> <table>
    > >> [% FOREACH item IN items -%]
    > >> <tr>
    > >> [% FOREACH cell IN item -%]
    > >> [% IF !cell -%]
    > >> <td>&nbsp;</td>
    > >> [% ELSE -%]
    > >> <td><img src="http://www.mywebsite.com/[% site %]/[% cell
    > >> %].JPG"></td>
    > >> [% END -%]
    > >> [% END -%]
    > >> </tr>
    > >> [% END -%]
    > >> </table>
    > >> </body>
    > >> </html>- Hide quoted text -

    >
    > >> - Show quoted text -

    >
    > Hahahaha, you make me lafff.
    > Systematic/programmable/generalization code.......
    >
    > "Well, if you could just get it to here, then if so, manually do this step".
    > Its just fuckin magic!!
    >
    > Hahahahahahaaaaaaaaaa.
    >
    > No time to do it right? Get off you fat fuckin ass and do it by hand losers!!!!
    > Or you could pay an expert to give you a million dolla, 1 time use gem <---
    >
    > losers- Hide quoted text -
    >
    > - Show quoted text -


    Hi, thank you for your comments. You are half right, and I just
    started learning TT and am still far from touching the gory details of
    this tool. :-( ....... clpm for me is pretty much like a classroom
    where I practice myself and listen from others (especially those
    professionals). I really apprecdiate all your input and suggestions.

    Best regards,
    Xicheng
    Xicheng Jia, May 15, 2007
    #7
    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. dpackwood
    Replies:
    3
    Views:
    1,767
  2. Sameen
    Replies:
    2
    Views:
    423
    Victor Bazarov
    Aug 29, 2005
  3. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,471
    Roedy Green
    Sep 15, 2011
  4. vikrant
    Replies:
    4
    Views:
    265
  5. Ram
    Replies:
    3
    Views:
    241
    Tad McClellan
    Apr 26, 2007
Loading...

Share This Page