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:
Reading Files

 



Pro_4
User

May 7, 2001, 10:39 AM

Post #1 of 13 (2082 views)
Reading Files Can't Post

Ok i have finally figured out how to put data into a database but now i am having problems reading it and then printing it out in html form. Any suggestions?

Thanks for the help :)

People Engrossed by a Reliable Language


rjoseph
Novice

May 7, 2001, 1:30 PM

Post #2 of 13 (2079 views)
Re: Reading Files [In reply to] Can't Post

Specific examples please: code, database structure, etc. It is really hard to help out without something concrete to start from.

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Pro_4
User

May 7, 2001, 1:32 PM

Post #3 of 13 (2078 views)
Re: Reading Files [In reply to] Can't Post

OH i am sorry, actually i was in a drafting class when i posted that so i didnt even have any of my scripts but i do now so click the attachements to download it. I use the pipe delimiter and my database is setup like this:
sponsorname|description|yesorno

Hope that helps to clarify things :)

People Engrossed by a Reliable Language


Pro_4
User

May 7, 2001, 1:37 PM

Post #4 of 13 (2076 views)
Re: Reading Files [In reply to] Can't Post

Arg i messed up and forgot to post the script. Ok now it is a attachment.

People Engrossed by a Reliable Language


rjoseph
Novice

May 7, 2001, 1:44 PM

Post #5 of 13 (2074 views)
Re: Reading Files [In reply to] Can't Post

That is what I was looking for! Ok, so say this is your database:

sponsorname|description|yesorno
sponsorname|description|yesorno
sponsorname|description|yesorno

So there are three lines of pipe-delimited text. Say also that this file was called db.txt - here is what I would do to say, print out all of the description fields:

Code
open (DB,"db.txt") or die "Cannot open db.txt: $!\n\n"; 
while (<DB>) {
# Spilt assumes $_ if no varible specified
my ($sponsor, $desc, $yesno) = split(/\|/);
print "$desc\n";
}

And what if I wanted to save all of those fields to a array-of-arrays (assume DB already open, as above)?

Code
my $i = 0; 
my @data;
while (<DB>) {
@{ $data[$i] } = split(/\|/);
}

# And to print the description field from the second line
print $data[1][1];

Now, this is all off the top of my head, so there might be some bugs in that code, but I think that you get the idea? If not, just ask! :)

Hope this helps!


r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Pro_4
User

May 7, 2001, 1:52 PM

Post #6 of 13 (2072 views)
Re: Reading Files [In reply to] Can't Post

WOW thanks that was what i wanted to know. Just wondering if i wanted to print out all of the sponsors and their descriptions, how would i do that? Like
Sponsornamehere : Their description they provided.

Thanks again :)

People Engrossed by a Reliable Language


Pro_4
User

May 7, 2001, 1:55 PM

Post #7 of 13 (2071 views)
Re: Reading Files [In reply to] Can't Post

oh wait nevermind i think i answered my own question.

hehe :)

People Engrossed by a Reliable Language


rjoseph
Novice

May 7, 2001, 1:59 PM

Post #8 of 13 (2070 views)
Re: Reading Files [In reply to] Can't Post

No problem at all.

Just in case you want to know:

Code
while(<DB>) { my ($s, $d, $yn)=split(/\|/); print "$s: $d and said $yn\n"; }

:-)

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Pro_4
User

May 7, 2001, 2:01 PM

Post #9 of 13 (2070 views)
Re: Reading Files [In reply to] Can't Post

Alright hehe now i feel smart i arrived at the same answer you did. :) Now the question is how do you make the sponsor name a link ( i am thinking of adding a new category to the database and i was wondering how to implement that ).
I used to know how but i forgot.

:)

People Engrossed by a Reliable Language


rjoseph
Novice

May 7, 2001, 2:05 PM

Post #10 of 13 (2069 views)
Re: Reading Files [In reply to] Can't Post

print "<a href=\"http://the_sponsor_link\">$s</a>";

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Pro_4
User

May 8, 2001, 2:53 PM

Post #11 of 13 (2058 views)
Re: Reading Files [In reply to] Can't Post

Ok i was looking over your second example given on how to print out one line from the database.
If i wanted to print the second line second data(in red)
sponsor|desc|yesno
sponsor|desc|yesno
in the line which would be desc , i would do this: print $data[1][1];

What if i wanted to read that $data[1][1] into a scalar. Let me explain why i am asking this:
For my login script i have a database setup as
username|password
I want to be able to compare usernames then compare the password that is in the database with the input password. Then set a cookie giving them permissions to certain areas of the site.

Thanks :)
( I am not sure if you will be able to understand that, ask if i need to clarify).

People Engrossed by a Reliable Language


rjoseph
Novice

May 8, 2001, 3:49 PM

Post #12 of 13 (2056 views)
Re: Reading Files [In reply to] Can't Post

$data[1][1] already is a scalar value, it is just part of a two-dimensional array. So, you can use it just as a scalar value if you want, or just reasign to a scalar:

Code
my $scalar = $data[1][1];

That is, if I understand exactly what it is you are asking :)


r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Pro_4
User

May 8, 2001, 4:18 PM

Post #13 of 13 (2054 views)
Re: Reading Files [In reply to] Can't Post

Alright i understand so let me give you a example of what i would like to achieve:
This is what the form sent the cgi :
Username = Bob
Password = JimmyBobo

Database:
Steve|asdf
Adam|Peace
Bob|JimmyBobo
Jill|jkl;

OK so how would the script find where bob was in the database and compare the passwords?

Thanks :)

People Engrossed by a Reliable Language

 
 


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

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