Hi everyone,
Few days back a message was posted about modifying a line in an ASCII file. Well I’m trying to do the same, but I’m getting my parameters through a get method from a web browser which calls myscript.pl, one of the subroutines in the script deals with changing the held quantity based on what the user typed:
#!/usr/bin/perl -wT
use CGI qw(:standard);
$ItemForm = param('item');
$AmountForm = param('amount');
$OldLine = `grep $ItemForm stock.dat`;
($item, $OldHeld, $order)=split(/” “/, $OldLine);
$NewHeld = $OldHeld-$AmountForm;
$NewLine= join (" ",$item, $NewHeld, $order);
open(STOCK,"items.dat");
@items=<STOCK>;
close STOCK;
for ($i=0; $i < $#items; $i++) {
$items[ $i ] = $NewLine if ($items[ $i ] eq $OldLine);
}
open (STOCK,">items.dat");
foreach (@items) {
print STOCK;
}
close(STOCK);
It's working fine but my question: is there any better way doing it? I believe it’s not practical to read the file in an array and then rewrite it back.
The other point is I don’t have the Tie::File module and chances are very low that I would be allowed to install it on the server.
Cheers,
Tom
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.