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

RE: Configuring a Perl script to extract certain records from an ASCII file

Greetings friends,

As an admin who appreciates clean HTML front-end and efficient Perl scripts on the back-end, I continue to use Perl applications for everything web-related.

I have an old Perl program which is currently being used to successfully open a Perl-based forum's ASCII files on the server, find and display the latest records on a web page through a SSI command.

Instead of displaying the latest records, I would like to display the latest records of certain author(s) only, for example moderators, which I will pre-select within the Perl script. I do not have enough skills to update this script shown below and need the help of the Perl Monks community.

I look forward to your help, thank you.

Zoro

#!/usr/bin/perl # # NOTE: The Perl script will display the forum's new messages # based on the administrators' settings within this script. # # Usage: # # <!--#include virtual="/cgi-bin/forums/new.cgi"--> # # NEW FEATURE - HELP NEEDED! # # Each forum category has a database file (database.txt) which appears + like this: # Forum fields: $num,$type,$lauthor,$date,$ltime,$name,$subject,$capti +on,$lock,$read,$topic_rating,$replies # ***** $name field is the author who started the forum message, $lnam +e is the last person who posted within that message. # # 189|0|JohnDoe|04/25/2010|20:31:02|JohnDoe|Title of Forum Message Her +e......|Caption of Forum Message here......||130||0 # 183|0|JaneDoe|01/31/2010|19:15:00|JohnDoe|Title of Forum Message Her +e Again|Caption of Forum Message here again||160||5 # 157|0|JeffDoe|04/05/2011|06:12:29|JohnDoe|Title of Forum Message Her +e Again|Caption of Forum Message here again||472||2 # # New feature to select only moderator(s) posted messages. # This function and fields have not been implemented. I would like to + find the latest three (3) messages # posted by the moderator(s) from the forums selected below and displa +y them together with the moderator's picture. # In summary, the moderator's photo then his 3 messages from the forum +s, followed by the next moderator and his 3 messages... # # $moderator1 = "JohnDoe"; # $moderator1photo = "JohnDoe.jpg"; # # $moderator2 = "JohnDoe2"; # $moderator2photo = "JohnDoe2.jpg"; # # $moderator3 = "JohnDoe3"; # $moderator3photo = "JohnDoe3.jpg"; # # # Program start... # $cgidir is the location of forum.setup $cgidir = "/home/sites/www.domain.com/web/cgi-bin/forums"; # Requirements... require "$cgidir/forum.setup"; require "$cgilib/forumlib.pl"; # Message select mode # # 0 - randomly choose a forum and display $num_topics # 1 - select limited number of forums... you must define @select_forum +s # 2 - select from all forums $select_mode = 1; # Which forums? # The forum ID you want to read messages from @select_forums = qw( education history news ); # Select how many topics per forum? $num_topics = 3; # Selection of topics based on? # 0 - Latest date, default # 10 - Most active $sort_by_field = 0; # If most active is chosen, then limit # number of current topics to choose from $num_current = 20; #>>>>>>>>>>>>>>> NO NEED TO EDIT ANYTHING BELOW THIS LINE <<<<<<<< # unbuffer output $| = 1; # initialize some internal variables... # Need to change this for speed improvement $r_setup = initialize(); require "$cgilib/global.pl"; foreach (@{$r_setup->{'forums'}}) { if ($r_setup->{'forum_status'}->{$_} eq "on" and $r_setup->{'forum_type'}->{$_} ne 'Private') { push(@all_forums,$_); } } if ($select_mode == 0) { my $num_forums = @all_forums; srand; my $random_number = int(rand $num_forums) + 1; @select_forums = ($all_forums[$random_number]); } elsif ($select_mode == 2) { @select_forums = @all_forums; } print_header(); print_header_end(); # printing table and messages... print qq~ <p>~; toggle_color("reset"); toggle_color(); # Default header display... print qq~ <p><font size="2" face="verdana" color="$font_color"><b>Most recen +t:</b></font></p> ~; # Read from public forums foreach (@select_forums) { my $r_data = readdata("$maindir/$_/$database"); # chop some topics my @data = @{$r_data}[0..$num_current-1]; # sort topics if ($sort_by_field == 10) { $r_data = sort_topics(\@data,$sort_by_field); } else { $r_data = \@data; } for ($j=0;$j<$num_topics;$j++) { toggle_color(); my $topic = $r_data->[$j]; chomp($topic); my ($num,$type,$lauthor,$date,$ltime,$name,$subject,$caption, +$lock, $read,$topic_rating,$replies) = split(/$split_delim/,$topic); $subject = decode($subject); $caption = decode($caption); $caption = substr($caption,0,50); if ($type == 0) { $image = "icon_general.gif"; $imagealt = "Member message"; } elsif ($type == 1) { $image = "icon_poll.gif"; $imagealt = "Poll"; } elsif ($type == 2) { $image = "icon_question.gif"; $imagealt = "Question"; } elsif ($type == 3) { $image = "icon_help.gif"; $imagealt = "Help"; } elsif ($type == 4) { $image = "icon_looking.gif"; $imagealt = "Searching"; } elsif ($type == 5) { $image = "icon_news.gif"; $imagealt = "News"; } else { $image = "icon_anchor.gif"; $imagealt = "Moderator message"; } # Display other information $ltime = time_format($ltime); $datelong = $date; $date = date_format($date); print qq~ <font face="verdana" color="$font_color"><a href="$mainurl/$_/$num +.$ext"><img src="$imgurl/$image" alt="$imagealt" border="0">$subject< +/a> <br><font face="verdana">$name, $datelong.</font></font><br> ~; } } print "</p>\n"; exit;
  • Comment on Configuring a Perl script to extract certain records from an ASCII file
  • Download Code

Replies are listed 'Best First'.
Re: Configuring a Perl script to extract certain records from an ASCII file
by GrandFather (Saint) on May 09, 2011 at 02:31 UTC

    That code is just plain broken, or at least is incomplete. If you add use strict; as the second line of the script then fix all the obvious places where my is required, there are still about a dozen variables which are, apparently, used without being initialised. The first, and probably the worst, of these is $cgilib in the second require.

    Aside from that, it looks very much like a real database (using SQLite perhaps) is a good solution to your problem. That will require you to learn some Perl or hire a Perl programmer however.

    True laziness is hard work

      Grandfather, thank you for your reply, I appreciate it very much.

      The initializations occur within the "forum.setup" file. As mentioned, this Perl new.cgi script is currently being used successfully to list the first three (3) messages within the forums specified in the script on the index.shtml web page using a SSI include statement. The script pulls information from a Perl-based forum software which uses ASCII database files (actual fields are shown in the code for reference purposes), not MySQL or any other database, and will be using this software in the future.

      To clarify, I need help in the additional FOR or IF statements which are needed to properly select the moderator(s) records from the three ASCII databases (education, history, and news forums) which this script opens and searches through.

      I hope to find a coding solution to this problem. Thank you again for your message.

      Zoro

        You say you want to filter by author/$lauthor, so use some comparison operator or regex to compare $lauthor to some other variable or string , see perlintro