Hi Stuart...
Below was one of my first "real" scripts, that is, that folks at
work use. I did mine interactively. It goes through a file of arbitrary
length asking the user to change xxx values for "ComputerID=xxx" and for
"Address=xxx". I think the next-to-last line is slightly risky to the
original file data, but in this case the file in question was known to be
backed up, so I wasn't living too dangerously ...
Newbie-to-newbie, there's stuff in here that you'll probably enjoy;
I had fun figuring out how to make this script work...
Of course, more experienced hands might have more refined ways of
doing such things...
-Chris
> use strict;
> use warnings;
>
> open (INFILE, "c:\\testmessage.txt") or die "The source file
> failed to
> open - $!";
> my @testarray = <INFILE>;
> my $name = "stuart";
> print @testarray;
>
> Here's what it prints: My name is $name.
>
> Here's the testmessage.txt file: My name is $name.
>
> After running my script, I'd like the testmessage.txt to
> read: My name is
> stuart.
>
> Thanks again for any help.
open INI, "$file"
or die "Couldn't open $file \n";
my @out;
while (<INI>) {
my ($flag,$value) = split /=/, $_;
if ($flag eq "ComputerID") {
print "ComputerID is $value . Change it to what?";
my $newval = <STDIN>;
my $line = "$flag=$newval\n";
print "$line\n\n";
push @out, $line;
}
elsif ($flag eq "Address") {
print "Address is $value. Change it to what?";
$newval = <STDIN>;
$line = "$flag=$newval\n";
print "$line\n\n";
}
else {
$line=$_;
}
push @out, $line;
}
open OUT, ">$file"
or die "Couldn't open $file on $machine for write \n";
print OUT @out
or die "Couldn't update file";
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>