CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Small Database 2...

 



Rivotti
User

Apr 18, 2000, 9:53 AM

Post #1 of 8 (1006 views)
Small Database 2... Can't Post

Hi again:

Imagine this file with data separated by Tabs:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

# data.txt

secion1 title1 text1
secion1 title2 text2
secion1 title3 text3

secion2 title1 text1
secion2 title2 text2
secion2 title3 text3

secion3 title1 text1
secion3 title2 text2
secion3 title3 text3
</pre><HR></BLOCKQUOTE>
And now the CGI file
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# file.cgi

...
open (FILE,"<data.txt");
while (<FILE> )
{ chomp;
my ($section,$title,$text)=split(/\t/,$_);
...
}
...
</pre><HR></BLOCKQUOTE>
How do I:

- Get all $section and print the values but only once. Ex: there are 3 section1 but it will print only one time.

- Get all sections and add it to @sec with no duplicates.

- add a line before the first line of section2 .. ..

- and after the last line "section2 .. .."

I imagine that to add a line between line 1 a and line 2 the code would be like:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

open (FILE,"<data.txt");
while (<FILE> )
{ chomp;
my ($section,$title,$text)=split(/\t/,$_);
while ($section="section1")
{ if ($n=1)
{ print FILE "section1\t...\t...\n"; }
else
{ $n++ }
}
}
</pre><HR></BLOCKQUOTE>

Is it right? Feel free to correct me...
Rivotti


Rivotti
User

Apr 27, 2000, 1:18 AM

Post #2 of 8 (1006 views)
Re: Small Database 2... [In reply to] Can't Post

anyone?


Peter Van Hoecke
Deleted

Apr 27, 2000, 2:54 AM

Post #3 of 8 (1006 views)
Re: Small Database 2... [In reply to] Can't Post

I'm working on it, but 1 question:

"Get all sections and add it to @sec with no duplicates", what if @sec already containded sectionx? Just ignore it or what?

Peter


Peter Van Hoecke
Deleted

Apr 27, 2000, 4:33 AM

Post #4 of 8 (1006 views)
Re: Small Database 2... [In reply to] Can't Post

This is just a first version, and I print to my command line, so give me some directions here...

#!perl.exe
use strict;
my(@lines, $section, $line, @sec);
open (FILE, "data.txt") &#0124; &#0124; die "no file?";
@lines = <FILE>;
close FILE;
@sec = qw( sectionA sectionB sectionC section2);
&PrintOnce;
&AddSectionOnce;
&FormatFile;

sub PrintOnce
{
my($section, $memory);
$memory = "Cindy";
foreach $line (@lines)
{
($section, undef, undef) = split(/\t/,$line);
($memory eq $section) &#0124; &#0124; ((print "$section\n") && ($memory = $section));
}
}

sub AddSection_once
{
my($section, $memory);
$memory = "Cindy";
foreach $line (@lines)
{
($section, undef, undef) = split(/\t/,$line);
($memory eq $section) &#0124; &#0124; (($memory = $section) &&
((( join " ", @sec ) =~ /$section/ ) &#0124; &#0124; (@sec = (@sec, $section))) );
}
}

sub FormatFile
{
my($section, $memory);
($memory , undef, undef) = split(/\t/, $lines[0]);
open(FILE, ">data.txt");
foreach $line (@lines)
{
($section, undef, undef) = split(/\t/, $line);
($memory eq $section) &#0124; &#0124; (($memory = $section) && print FILE "\n");
print FILE $line;
}
close FILE;
}


Rivotti
User

Apr 27, 2000, 5:33 AM

Post #5 of 8 (1006 views)
Re: Small Database 2... [In reply to] Can't Post

Hi Peter: Smile

Thanks for the reply. You didn't need to write a program. I just wanted examples to use in the future. Thanks anyway. It was very usefull.
I have this idea of a FAQ Generator Program that anyone could use it in any Site without needing to know programing. Each one of those examples are possibles ways to implement in my program.

The program have 3 steps:

1 - A interface were you can add a new entry, delete a entry. The New entry will generate a small form to enter the data about the FAQ. Ex: Section, Question, Answer, Importance.

2 - A program that generates a list of all questions in the FAQ ordered by section and by importance ( @sec ). Ex:

Section 1

Question 1 ( link:program3.cgi?sec=1&que=1)
Question 2 ( link:program3.cgi?sec=1&que=2)
Question 3 ( link:program3.cgi?sec=1&que=3)

Section 2

Question 1 ( link:program3.cgi?sec=2&que=1)
Question 2 ( link:program3.cgi?sec=2&que=2)
Question 3 ( link:program3.cgi?sec=2&que=3)

...

3 - A program that generates the screen of the link. Ex Secion2, Question 3

Section 2

Question3
Answer3

See? This is the way I like to learn something. I imagine a little program and then I try do make it. That's why I prefer just to know the examples. I like to understand how to and why. But if you want to built the program with me we're welcome.

Thanks Peter, all the help is precious. Smile

P.S.- By the way, can you explain the code you wrote? There is some parts that I dont understand so well.


Peter Van Hoecke
Deleted

Apr 27, 2000, 6:05 AM

Post #6 of 8 (1006 views)
Re: Small Database 2... [In reply to] Can't Post

Well, that's exactly my way of learning Perl too, just trying to solve little programs.

About the FAQ idea... I have never written Perl for webdisplay, but I'm more than interested to focus on that area of Perl applicatons. I allso just bought a book about using Perl to connect to databases. The problem is that I only surf from my desk at work, and on Friday I won't be here, so send me a mail on Monday, and I'll see what I can do...

Peter.Van_Hoecke@ecom.be

PS: I'll clean up the little program this weekend and populate it with comments (somehow submitting the copy-pasted code deleted all my tabs)


Rivotti
User

Apr 27, 2000, 6:15 AM

Post #7 of 8 (1006 views)
Re: Small Database 2... [In reply to] Can't Post

Hi again:

Check your mail box. Smile


darian
Deleted

Apr 27, 2000, 6:52 AM

Post #8 of 8 (1006 views)
Re: Small Database 2... [In reply to] Can't Post

Peter try using the UBB tags for code:

http://www.perlguru.com/forum/ubbcode.shtml

Then you can just copy and paste and your tabs will be there.

Smile



[This message has been edited by darian (edited 04-27-2000).]

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives