Previous Thread
Next Thread
Print Thread
Rate Thread
#103307 11/21/2001 6:32 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
I am making a script, were i need to keep a database (just flat file), and i can add to it, and get stuff from it, but how do i get a specific line from it, edit the line, and put it right back over the old line (it's for the EDIT function)

Sponsored Links
#103308 11/22/2001 5:19 AM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
The easiest way is to read the lines of the file into an array, replace the desired array element, then write the array back to the file.

If the lines in the file are all fixed length, you could use the seek function to read and write a specific line, without having to read and write the whole file.

If you have variable length lines, and you don't want to read the entire file into memory, you can use the following method:
  • Copy the file, line by line, to a temporary file.
  • For the line you want to modify, write the modified line, instead of the original line.
  • Rename the temporary file to the original file.

#103309 11/22/2001 1:29 PM
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
Yup, dave's explanation hits it. There is no really good way to go to one line and edit it, in a random line length file.


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
#103310 11/22/2001 3:40 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
one last question. How do i get the info from a form, that is made inside the script? like i can make an external html file, with the form, and do the CGI function to get the data, easy, but how do i get the data from a form that is built into my script?

#103311 11/22/2001 4:32 PM
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
I have no idea what you are asking. Can you please explain a little better?

--mark


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
Sponsored Links
#103312 11/22/2001 4:34 PM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335

#103313 11/22/2001 5:18 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
yeah, something like that, i never knew it was so involved. heh, if you cant tell, im new to perl tipsy

well, one last question and im set.

in my script, i need it to open a file, and append to it, EZ, i know, but i cant seem to understand how to do it. (i alrady have the like i want to append joined and everything, just need to append it.) but the book i have, really doesn't tell you how to do it...so im confused.

#103314 11/22/2001 6:22 PM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
Code
code:

#103315 11/22/2001 6:30 PM
Joined: Feb 2000
Posts: 4,625
Member
Member
Offline
Joined: Feb 2000
Posts: 4,625
What kinda names are those for fish, Dave? tipsy

#103316 11/22/2001 7:24 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
omg, so ez, i wish the book was clearer on that.

Sponsored Links
#103317 11/22/2001 8:29 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
Just to let you know guys, that it works PERFECT!!!

EXECPT, how can i make it automaticly redirect you to the main page after you get done with the script?

#103318 11/22/2001 8:53 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
spoke too soon frown

it works, but it always displays a server misconfiguration message, anyone know why? hes the page its comming from (try putting info in, it should give you a nice looking error page)

and here is the script...

[edit]uh...code doesn't seem to work...oh well

#!/usr/bin/perl -w
#------------------------------------------------------------------------------#
# #
# Script name: Match Scheduler #
# Discription: Refs register game between two teams #
# Last updated: 11/22/01 #
# #
#------------------------------------------------------------------------------#

#---------
# Password to add a match
#---------
$pw = 'le3t';
#----------
# Get form info
#----------
use CGI qw(:standard);
$ref = param("name");
$f_pw = param("pw");
$team1 = param("team1");
$team2 = param("team2");
$hour = param("hour");
$min = param("min");
$amp = param("amp");
#---------
# Check the ref and password
#---------
open(REFS, "while ( {
if (!(/^$ref/) || $ref eq '') {
$message = 'Sorry, you must be a ref to schedule a match, if you are, make sure you typed in your name correctly';
&message;
}
}
close(REFS);

if ($f_pw ne $pw) {
$message = 'Password is incorrect, please try again';
&message;
} else {
&add;
}

sub add {
$match = join('|=|', $team1, $team2, $hour, $min, $amp, $ref);

open(MATCHES, ">>matches.txt") or die "Cannot open Match file for appending: $!";
print MATCHES "$match n";
close(MATHCES);

&message = "Match succesfully added, thanks $ref";
&message;
}

sub message {
print "Content-type: text/htmlnn";
print<



..:: Infantry Flagging League ::..













..:: Home :: Forums ::..






















































main


home

about IFL

League Staff

forums

standings

schedule

squads

refs

rules

refs


application

schedule a match

report a match

captains


application

add a player

delete a player

links


nmebase

specfreq

infantry-sector



























Message :|


$message













..:: Copyright :: Privacy Policy ::..





HTML
}

[ 11-22-2001: Message edited by: adam ]

[ 11-22-2001: Message edited by: Mark Badolato ]
#103319 11/22/2001 11:27 PM
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
Code
code:

remove that ;

--mark


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
#103320 11/23/2001 11:50 AM
Joined: Mar 2001
Posts: 7,394
LK Offline
Admin / Code Breaker
Admin / Code Breaker
Offline
Joined: Mar 2001
Posts: 7,394
Mark, actually it's a bug in the UBB...

> => >, so it takes the ";" from > and adds it to the ) of the WHILE (), and makes it > (notice the > doesn't have ";" in the end, and the while doesn't have ')')

ex: foreach (

[ 11-23-2001: Message edited by: LK ]

#103321 11/23/2001 12:03 PM
Joined: Mar 2001
Posts: 7,394
LK Offline
Admin / Code Breaker
Admin / Code Breaker
Offline
Joined: Mar 2001
Posts: 7,394
BTW Adam,

1. Remove the space before 'HTML' in the end.
2. " close(MATHCES);" or something like that -> typo, should be MATCHES.

#103322 11/23/2001 12:48 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
thanks, can anyone tell my why my [code] thingys didn't work?

#103323 11/23/2001 12:52 PM
Joined: Mar 2001
Posts: 7,394
LK Offline
Admin / Code Breaker
Admin / Code Breaker
Offline
Joined: Mar 2001
Posts: 7,394
I think Mark removed it since it caused problems with the look. (since it doesn't wrap)

#103324 11/23/2001 1:52 PM
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
Yup. You had had the wrong slash on the code CODE so I fixed it and that caused the wrap to be screwy so I removed it all together smile


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
#103325 11/23/2001 2:06 PM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
please delete this

[ 11-23-2001: Message edited by: Dave_L ]

#103326 11/23/2001 2:08 PM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
I don't think this loop does what you want:

Code
code:

Try something like this (untested code):

Code
code:

[ 11-23-2001: Message edited by: Dave_L ]

#103327 11/25/2001 4:40 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
yeah, it didn't, thanks

can anyone tell me how you would split pages up? Like only 20 entries a page, or only entrys that start with A on one page?

#103328 11/27/2001 12:16 PM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
split pages up

I don't understand what you mean. Please explain in more detail what you want to do.

#103329 11/27/2001 12:34 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
like with ubb, it will only show topics from like 10 days ago on one page, I want to do something of the same nature, only show 20 entries on one page, or show just the entrys that start with "A" on one page, get me?

#103330 11/27/2001 1:10 PM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
Ok, I see.

I assume your "entries" are all in one file?

Read through the file, pick out the entries you want to display (based on whatever criteria you want to use), and display only those entries.

If you want to allow users to "page through" all the entries, it gets more complicated, since you need to keep track of what "page" the user is on. CGI is a "stateless" protocol, so you have to do this yourself. One solution is to use a hidden form field to store the current page number or item number.

Hope this helps.

#103331 11/27/2001 5:54 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
yes it does, thanks You are a BIG help Dave!

#103332 11/27/2001 5:57 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
Hmmm...I must be stupid or something, but i cant figure this out. go here (http://ifl.nmebase.com), look at the upcomming matches section, for SOME reason its just showing like the frist letter.

Okay, my script opens the file, for reading, and takes a line (like this)
Code
code:

then my script takes the file, splits it by |=| and saves it into vaiables to display, but like i said, its only displaying the frist letter of the variable, can anyone tell me why? here is the block of code being used.
Code
code:

[ 11-27-2001 04:58 PM: Message edited by: adam ]

#103333 11/28/2001 7:01 PM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
Code
code:
should be
Code
code:

The pipe is a metacharacter, so it has to be escaped when used in a 'split' pattern:
Code
code:

In this 'if' predicate, '=' should be '==' to do a numeric comparison, or 'eq' to do a string comparison. If $amp will always be a number, I would do a numeric comparison.
Code
code:

[ 11-28-2001 06:08 PM: Message edited by: Dave_L ]

#103334 11/29/2001 5:29 PM
Joined: Jul 2001
Posts: 108
Member
Member
Offline
Joined: Jul 2001
Posts: 108
thanks Dave, you should be the moderator of this forum


Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
Gizmo
Gizmo
Portland, OR, USA
Posts: 5,833
Joined: January 2000
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services