logique
asked on
Increasing numbers
If I have a file where it is like
blah::0::22::blah
and I read the field 22 to $thenumber --
How can I increase 22 to 23? do I do $thenumber++ ?
blah::0::22::blah
and I read the field 22 to $thenumber --
How can I increase 22 to 23? do I do $thenumber++ ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Will it modify the file though?
To modify the file, you'd have to write to it. (unless it is a tie'd file)
ASKER
How would I replace the current line blah::0::24::blah
with blah::0::25::blah
with blah::0::25::blah
perldoc -q "How do I change one line in a file"
open (IO_FILE, "< lines.txt") or die $!;
@Lines = <IO_FILE>;
foreach $_ (@Lines) {
@Parts = split /::/;
$Parts[2]++;
push @OutArray, join ("::", @Parts);
}
close IO_FILE;
open (IO_FILE, "> lines.txt") or die $!;
print IO_FILE @OutArray;
close IO_FILE;
@Lines = <IO_FILE>;
foreach $_ (@Lines) {
@Parts = split /::/;
$Parts[2]++;
push @OutArray, join ("::", @Parts);
}
close IO_FILE;
open (IO_FILE, "> lines.txt") or die $!;
print IO_FILE @OutArray;
close IO_FILE;
My comment above was assuming you wanted to change all the lines.
If you only want to change a particular line then, as you read each one you check if it is the one to change, and if so you do the ++ operation.
If you only want to change a particular line then, as you read each one you check if it is the one to change, and if so you do the ++ operation.
This is what I mean, if you only want to change certain lines, not all:-
open (IO_FILE, "< lines.txt") or die $!;
while (<IO_FILE>) {
$Line = $_;
if (insert your test here) {
@Parts = split /::/, $Line;
$Parts[2]++;
push @OutArray, join ("::", @Parts);
} else {
push @OutArray, $Line;
}
}
close IO_FILE;
open (IO_FILE, "> lines.txt") or die $!;
print IO_FILE @OutArray;
close IO_FILE;
open (IO_FILE, "< lines.txt") or die $!;
while (<IO_FILE>) {
$Line = $_;
if (insert your test here) {
@Parts = split /::/, $Line;
$Parts[2]++;
push @OutArray, join ("::", @Parts);
} else {
push @OutArray, $Line;
}
}
close IO_FILE;
open (IO_FILE, "> lines.txt") or die $!;
print IO_FILE @OutArray;
close IO_FILE;
logique,
You awarded the points to quadalupe, but it seems that he left many of your questions unanswered. These questions were subsequently answered by others.
It is usual to give the points to the first person who actually fully satisfies you, rather than the first person claims to have an answer.
It is also possible to split the points among various contributors.
You awarded the points to quadalupe, but it seems that he left many of your questions unanswered. These questions were subsequently answered by others.
It is usual to give the points to the first person who actually fully satisfies you, rather than the first person claims to have an answer.
It is also possible to split the points among various contributors.
ASKER
Sorry, I'm new here.
Sorry I had understood only the increse and not the file edit...PC User321 is right...
ASKER