Hullo Venerable Monks:
The program below opens a file, reads the last two lines, looks for a number, increments that number, then (I hope) appends the new lines to the bottom of the file.
When I open $file for reading (and only reading) the program works, and prints out the new line to the screen. Making it read/write (+>>$file) gives me:
Use of uninitialized value in pattern match (m//)...at line 15
I'm not sure why the simple addition of making the file read and writable empties my $line1 variable (opening with >$file destroys the file completely). Any clues?
Full code bit:
------------------------
#!/usr/local/bin/perl -w
use strict;
my($newline);
my($file) = "d:\\softdev\\Pserver.txt";
my(@lines);
my($lines);
my($line1);
my($line2);
open (DAT, "+>>$file") or die "$!";
@lines = <DAT>;
$line1 = $lines[-1];
$line2 = $lines[-2];
if ($lines[-1] =~ /(\d)/){
$newline = ($1 + 1);
print "$newline\n";
$line1 =~s/(\d)/$newline/;
print "$line1\n";
print DAT "$line1";}
close DAT;
-------------------------
(It's probably ugly code, but I'm self-taught and have no Compsci background :F)
Cheers,
Flubb
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.