Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regarding Text files

Status
Not open for further replies.

TmlMatus

Technical User
Nov 24, 2007
12
CA
Allrite, i have a website on which a customer submits there order, say if they wanted 4 of one thing ... the info would be send to a text file.

Currently i have 4 products that i am offering ... so the text file looks something like this.

'2,3,1,0'

is there a way to put a counter on the text file so that everytime a new order is submited a new number inside the file would count up.

Example of what i mean is below.

- First order
0001,1,2,3,3

-Second order
0002,1,2,3,2

the first digit would increase everytime a new order was submited ... is there a way of doing this in perl?

or could anyone recommend a different way?

Thanks in advance.
 
Yes, open() the file for reading '<', read the line into a scalar, close() the file. split() the line into individual scalars, increment the counter scalar. open() the file for overwriting '>', maybe locking the file, print the scalars back to the file. close() the file.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Besides Kevin's suggestions, I would have to suggest that you move to using a DB (your host probably provides mysql or postgresql). Very simple, very easy, scales better.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
thanks kevin, but i'm not really getting what you mena by scalar.

Could u show me how you woudl go about it?

My Current code is
---------------------------------------------
#!/usr/bin/perl

use CGI qw:)standard);


print "Content-type: text/html\n\n";

$HTML = "message.htm";
open (HTML) or die "Can't open the file!";
print <HTML>;
close (HTML);


open(FILE,">productselection.txt");
flock(FILE,$exclusive_lock);

print FILE param("Input10");
print FILE param("Input11");
print FILE param("Input12");
print FILE param("Input13");



flock(FILE,$unlock_lock);
close(FILE);
 
ok i got this code up until now

---------------------------
#!/usr/bin/perl

use CGI qw:)standard);


print "Content-type: text/html\n\n";

$HTML = "message.htm";
open (HTML) or die "Can't open the file!";
print <HTML>;
close (HTML);


open(FILE,">productselection.txt");
flock(FILE,$exclusive_lock);

$a = '0000';
++$a;

print FILE ($a);

print FILE param("Input10");
print FILE param("Input11");
print FILE param("Input12");
print FILE param("Input13");



flock(FILE,$unlock_lock);
close(FILE);

------------------------------
how would i go about saving the 'a' variable so that everytime it is submitted the 'a' variable would increase by one.

 
Code:
open(FILE,">productselection.txt");
flock(FILE,$exclusive_lock);
Using 'flock' like that is counter-productive. Effectively what you're doing there is opening "productselection.txt" for writing, which deletes everything in the file. And --then-- you're checking to see if the file is locked. Dangerous.
 
Aye, not only that, but the lock state variables he's using are undefined, so aren't doing anything anyway.

If you want to learn how to do this properly, check out the following faq entry:

perlfaq5 - I still don't get locking...

Code:
[gray]#!/usr/bin/perl[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]CGI[/green] [red]qw([/red][purple]:standard[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]Fcntl[/green] [red]qw([/red][purple]:DEFAULT :flock[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Content-type: text/html[purple][b]\n[/b][/purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$html[/blue] = [red]"[/red][purple]message.htm[/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$ifh[/blue], [red]'[/red][purple]<[/purple][red]'[/red], [blue]$html[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$html[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] <[blue]$ifh[/blue]>[red];[/red]
[maroon]close[/maroon][red]([/red][blue]$ifh[/blue][red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]$outfile[/blue] = [red]'[/red][purple]productselection.txt[/purple][red]'[/red][red];[/red]
[url=http://perldoc.perl.org/functions/sysopen.html][black][b]sysopen[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$ofh[/blue], [blue]$outfile[/blue], O_RDWR|O_CREAT[red])[/red]
	or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$outfile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/flock.html][black][b]flock[/b][/black][/url][red]([/red][blue]$ofh[/blue], LOCK_EX[red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't flock [blue]$outfile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/truncate.html][black][b]truncate[/b][/black][/url][red]([/red][blue]$ofh[/blue], [fuchsia]0[/fuchsia][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]can't truncate numfile: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]$counter[/blue] = [red]'[/red][purple]0000[/purple][red]'[/red][red];[/red]
++[blue]$counter[/blue][red];[/red]    

[black][b]print[/b][/black] [blue]$ofh[/blue] [red]([/red][blue]$counter[/blue][red])[/red][red];[/red]
[black][b]print[/b][/black] [blue]$ofh[/blue] [maroon]param[/maroon][red]([/red][red]"[/red][purple]Input10[/purple][red]"[/red][red])[/red][red];[/red]
[black][b]print[/b][/black] [blue]$ofh[/blue] [maroon]param[/maroon][red]([/red][red]"[/red][purple]Input11[/purple][red]"[/red][red])[/red][red];[/red]
[black][b]print[/b][/black] [blue]$ofh[/blue] [maroon]param[/maroon][red]([/red][red]"[/red][purple]Input12[/purple][red]"[/red][red])[/red][red];[/red]
[black][b]print[/b][/black] [blue]$ofh[/blue] [maroon]param[/maroon][red]([/red][red]"[/red][purple]Input13[/purple][red]"[/red][red])[/red][red];[/red]

[black][b]flock[/b][/black][red]([/red][blue]$ofh[/blue],LOCK_UN[red])[/red][red];[/red]
[maroon]close[/maroon][red]([/red][blue]$ofh[/blue][red])[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.10.0) Modules used :
[ul]
[li]CGI - Simple Common Gateway Interface Class[/li]
[li]Fcntl - load the C Fcntl.h defines[/li]
[/ul]
[/tt]

Note: Code not actually tested.

- Miller
 
awesome miller ... i'll try that out right away.

Thanks.
 
Note #2:

I didn't actually read your original post. That code is only in response to the code that you posted. It may not be what you ultimately want, but it does fix some of the bad coding practices that you were using.

Let us know if it works out.

- Miller
 
ummm the digits are still not counting up even tho the counter is there.

like the first time i open my .txt file i want to be able to see how many orders have been taken by the first 4 digits in the code.

say if i open a the .txt file and 15 orders have been placed, i wanna see 0015 ( following that the order ).
 
post a better example of your data file

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top