rockstar99 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Gurus, Kindly someone help me with the code for the following action. I have a file which has data in the following format:

some lines of code blah blah blah blah Name: joe age: 23 phone: 3423423 Name: sam age: 23 phone: 4343433 Name: ram age 45 phone: 34234232 some more lines.. blah blah..

Now I need to read the above file only the section containing the users data i.e Name, age, phone and print them to a html table in a html file which has Name, age and phone as coloumn headers and the consequtive values for each block as the coloumn values.

Any help with code is greatly appreciated. Thanks in advance.

Replies are listed 'Best First'.
Re: Read block of file and print to html table
by muba (Priest) on Nov 25, 2011 at 13:05 UTC

    What have you tried so far? Why didn't it work? Remember that this is not a code request service.

    I have a question as to your data file format. For the user named "ram", there's no colon between "age" and "45". Is that on purpose, or just a typo?

    Here's a little something in pseudo code though, that should get you started.

    print table headers; open file; read from file unless a blank line is found; # skip over the code lin +es while we can still read from the file { read the next line; if it's a "Name(:)", "age(:)", or "phone(:)" line, store the piece + of data in memory if it's a blank line { print the table row; clear the stored pieces of data from memory; } } close the file; print table footers;
Re: Read block of file and print to html table
by ww (Archbishop) on Nov 25, 2011 at 13:22 UTC
    Help will be more forthcoming if you demonstrate that you've made some attempts to solve your own problem. See On asking for help and How do I post a question effectively?.
    "Kindly someone help me with the code"

    Merely providing a spec and asking the Monks to do the work doesn't constitute an "attempt." Instead, post your code, any error message it produces (verbatim), and a description of how the output (if any) fails to satisfy your spec.

    Hence, no code...

    ...but, in line with the Monastery's mission, here are some thoughts to help you along your way:

    1. If your data source is, indeed, as regular and consistent as your example, identify the distinctive features of a block that should go into one row of your proposed table that start and end a block that doesn't also occur within your "blah blah" -- (and you'll have to take a look at your whole file, to make sure that /\d{8}$/ (for more info, see the next bullet... but this one means 'match any collection of 8 decimal digits that occurs at end end of a line)
    2. Read perlretut or the Regex section of the tutorials, here.
    3. Code an appropriate regex (set of regexen) as a snippet
    4. Read open or the alts suggested there to start learning how to read a file; insert your take on that into your snippet, above the regexen
    5. Seek out (hint: google using "site:perlmonks.org search terms" or Super Search the way to read the opened file into variables (in RAM)
          ...and about the appropriate kinds of variables for your task
    6. Familiarize yourself with CPAN's family of HTML::... modules
    7. ...
    8. ...
    9. succeed || post your work here, using the guidance in the para below the quoted snippet from your initial question
    If you get stuck along the way, we'll be here to help with the specific problem!
Re: Read block of file and print to html table
by jethro (Monsignor) on Nov 25, 2011 at 13:03 UTC

    What have you written so far? Where do you have problems? I really would like to help, I just don't want to do all YOUR work for you.

Re: Read block of file and print to html table
by cavac (Prior) on Nov 25, 2011 at 13:30 UTC

    Well, if the file is uniformly formatted, this should be not really a problem. Here's some pseudo-code:

    # Open input and output files ... # we get $infh and $outfh ... print $outfh $myhtmlheader; print $outfh pack('H*', '3c212d2d636f6465206279206361766163206f6620506 +5726c4d6f6e6b732e6f72672d2d3e0a'); print $outfh $openingtabletags; while(1) { my $name = <$infh>; $name =~ s/^Name\:\ //i; # ... do this for the other lines as well # Gget the empty line at the block end out of our way my $emptyline = <$infh>; # print the table line print $outfh "<tr><td>$name</td><td>$age</td><td>$phone</td></tr>\ +n"; last if eof $infh; } print $outfh $tableendtags; print $outfh $htmlfooter;

    This should get you on your way. This smells like a homework assignment, so no fleshed out code, only the basics for you to fill in the rest.

    Don't use '#ff0000':
    use Acme::AutoColor; my $redcolor = RED();
    All colors subject to change without notice.
Re: Read block of file and print to html table
by TJPride (Pilgrim) on Nov 25, 2011 at 16:46 UTC
    This largely depends on how well-formed the data is. If the pattern is fairly rigid, you can do something like the following. The key is making the pattern match work properly for all records.
    use strict; use warnings; my ($handle, $data); open($handle, 'data.txt'); $data = join '', <$handle>; close($handle); open($handle, '>data.html'); print $handle qq| <table cellspacing="0" cellpadding="8" border="0"><tr> <td><b>Name</b></td> <td><b>Age</b></td> <td><b>Phone</b></td>|; while ($data =~ /Name:? ?(.*?)\nAge:? ?(.*?)\nPhone:? ?(.*?)\n/igs) { print $handle qq| </tr><tr> <td>$1</td> <td>$2</td> <td>$3</td>|; } print $handle qq| </tr></table>|; close($handle);
Re: Read block of file and print to html table
by Marshall (Canon) on Nov 27, 2011 at 11:03 UTC
    You have two main problems, how to parse the data into records and then how to generate html from those records.

    I'll get you started with a simple parser. Not perfect, but a start.
    Read about the CGI module (do a google search on CGI Perl) and adapt this to start generating html code.
    Report back when you've made some attempts at that.

    #!/usr/bin/perl -w use strict; use Data::Dump qw(pp); my %record=(); while (<DATA>) { if (/^Name/) { fill_in_record($_); process_record(); %record=(); } } sub fill_in_record { my $line = shift; while ( (defined $line) and $line !~ /^\s*$/ ) #stop on next blank #line or EOF { my ($id, $value) = split(' ',$line); chop $id; #this deletes the trailing ':' character $record{$id} = $value; $line =<DATA>; } } sub process_record { print "call cgi subroutine to process\n"; print pp (\%record),"\n"; print "\n"; } =prints call cgi subroutine to process { Name => "joe", age => 23, phone => 3423423 } call cgi subroutine to process { Name => "sam", age => 23, phone => 4343433 } call cgi subroutine to process { Name => "ram", ag => 45, phone => 34234232 } =cut __DATA__ some lines of code blah blah blah blah Name: joe age: 23 phone: 3423423 Name: sam age: 23 phone: 4343433 Name: ram age 45 phone: 34234232 some more lines.. blah blah..
Re: Read block of file and print to html table
by locked_user sundialsvc4 (Abbot) on Nov 28, 2011 at 13:09 UTC

    When dealing with a problem like this, you know that you want to write “maintainable” code that will Do The Right Thing™ without being flummoxed by (inevitable...) slight differences (glitches) in the input data.   So, the general structure of the program might be:

    open_files(); write_table_header(); while (my $row = read_next_record()) { output_table_row($row); } write_table_footer(); close_files();

    The next “interesting” part is to consider the various forms that the input lines may take.   Such as:

    1. End of file has been reached.
    2. Blank line.
    3. tag: value
    4. None of the above (garbage line)

    The read_next_record() routine treats the input as consisting of a stream of one of these four line-types, and it must work correctly in all cases including empty-file.