Solved

Increasing numbers

Posted on 2000-02-21
12
199 Views
Last Modified: 2010-03-05
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++ ?
0
Comment
Question by:logique
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 3

Accepted Solution

by:
guadalupe earned 50 total points
ID: 2544185
yes that will work as long as you remember that if you use this variable in the same instance you will get thje old value and then it will increment

EX:

$n = 1;

print $n; # 1

print $n++; # 1

print $n; # 2

print ++$n; # 3

print $n; # 3
0
 

Author Comment

by:logique
ID: 2544209
Adjusted points to 55
0
 

Author Comment

by:logique
ID: 2544210
Will it modify the file though?
0
 
LVL 84

Expert Comment

by:ozo
ID: 2544357
To modify the file, you'd have to write to it. (unless it is a tie'd file)
0
 

Author Comment

by:logique
ID: 2544549
How would I replace the current line blah::0::24::blah

with blah::0::25::blah
0
 
LVL 84

Expert Comment

by:ozo
ID: 2544805
perldoc -q "How do I change one line in a file"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:PC_User321
ID: 2544896
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;
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2544905
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.
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2544972
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;


0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2548638
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.

0
 

Author Comment

by:logique
ID: 2548924
Sorry, I'm new here.
0
 
LVL 3

Expert Comment

by:guadalupe
ID: 2549993
Sorry I had understood only the increse and not the file edit...PC User321 is right...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl script help 5 233
Perl DBI Transactions Using Custom Module 7 45
Perl Awk Need Help 3 109
File Find regex problem 4 60
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now