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...

 



Rivotti
User

Apr 12, 2000, 6:06 AM

Post #1 of 5 (843 views)
Small Database... Can't Post

I have a text file like this:

#file.txt
# $1 $2 $3
name address email
name address email
name address email
name address email
name address email
name address email

How to:<UL TYPE=SQUARE>
<LI> For each line get the records: $1,$2,$3 separated?
<LI> Print a page for each line?
</UL>

Another thing: congratulations for the UBB Code. Is very usefull and easy. Smile

Thanks
Rivotti

[This message has been edited by Rivotti (edited 04-12-2000).]


Admin
Deleted

Apr 16, 2000, 7:42 PM

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

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open (FILE,"<file.txt") or die "Couldn't open file.txt $!";
while (<FILE> ){
chomp;
my ($name,$address,$email)=split(/\t/,$_);
# do something with $name $address and $email
}
</pre><HR></BLOCKQUOTE>

The above assumes that you are using a tab as the delimiter.

I'm not sure what you mean by "print a page for each line" -- you can't declare a page break in a web page (yet). Were you referring to writing the contents to a new file?

And you can congratulate Infopop, makers of the Ultimate Bulletin Board on the UBB code -- I agree, it's an awesome feature.


Rivotti
User

Apr 17, 2000, 2:09 AM

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

Hi:

Thanks for the help. Yes, I was referring to writing the contents to a new file using the variables. What is the function of chomp?

Thank you again
Rivotti


Admin
Deleted

Apr 17, 2000, 7:06 AM

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

To write the contents after it's been split...
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open (FILE,"<file.txt") or die "Couldn't open file.txt $!";
while (<FILE> ){
chomp;
my ($name,$address,$email)=split(/\t/,$_);
# do something with $name $address and $email}
open (FILE2,">file2.txt") or die "Couldn't open file.txt $!";
print FILE2 join(/\t/,$name,$address,$email);
print FILE2 "\n";
close FILE2;
}
</pre><HR></BLOCKQUOTE>

chomp takes off the newline character \n from a line.

chop removes the last character indiscriminantly.

For example:

$line = "Hello";

chomp $line;
would do nothing here, because there is no newline character.

chop $line;
would make $line = "Hell";

Example 2:

$line = "Hello
";
or
$line = "Hello\n";

both chomp and chop will make $line = "hello".

It's best, if working with a file to use chomp because it's the one way to make sure that your text never gets chopped Smile


Rivotti
User

Apr 17, 2000, 9:39 AM

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

Hi again:

Thanks for the lesson.

Keep the good work Smile
Rivotti

 
 


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

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