CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How to search and increment only the numbers

 



tux
New User

Aug 25, 2008, 5:35 AM

Post #1 of 10 (1549 views)
How to search and increment only the numbers Can't Post

Perl Guru's,

Want to add only the number by constant value = 5
eg:
Input.txt
A10
B70
C40
D29

Output.txt
A15
B75
C45
D34

Basically i want to maintane the Alpha letter as it is and add 5 to the numeric value.

Thanks
TuX


KevinR
Veteran


Aug 25, 2008, 11:29 AM

Post #2 of 10 (1548 views)
Re: [tux] How to search and increment only the numbers [In reply to] Can't Post

What have you tried? Do you know any perl?
-------------------------------------------------


tules
Novice

Aug 31, 2008, 7:26 PM

Post #3 of 10 (1498 views)
Re: [tux] How to search and increment only the numbers [In reply to] Can't Post


In Reply To
Perl Guru's,

Want to add only the number by constant value = 5
eg:
Input.txt
A10
B70
C40
D29

Output.txt
A15
B75
C45
D34

Basically i want to maintane the Alpha letter as it is and add 5 to the numeric value.

Thanks
TuX



something like this, correct me if im wrong please!

open(FILE, "Input.txt");

while(<FILE>) {

$_=~ s/(\d+)/$1 + 5/e;

open(FILE2, ">>output.txt");

print FILE2 "$_";}

close(FILE);close(FILE2);


PGScooter
stranger

Sep 1, 2008, 5:04 PM

Post #4 of 10 (1482 views)
Re: [tules] How to search and increment only the numbers [In reply to] Can't Post

Hi Tules,

That looks great to me, although I didn't try it. Do you not have Perl installed??? You should always test your code, and if it works as you want, then you're in good shape.
The more you teach me, the more I learn. The more I learn, the more I teach.


tules
Novice

Sep 1, 2008, 5:11 PM

Post #5 of 10 (1481 views)
Re: [PGScooter] How to search and increment only the numbers [In reply to] Can't Post

Yeah I tested it and it works! :)

only minor problem is that since the file is set to append, you'd have to delete the file if you wanted to replace the contents or else it would just go on the end, it was neccesary the way i did it because the loop would overwrite each time otherwise, I'm sure theres a way to do it otherwise by pushing them into an array before writting to file for instance, but this does the job fine.

Perhaps you could help me with a much more difficult problem, see the "robot" post? I'm basicaly having trouble populating the hashes, can you can deffinately use a variable in a foreach loop to generate keys? (In this case foreach link) Any tips would be much appreciated!


PGScooter
stranger

Sep 1, 2008, 6:07 PM

Post #6 of 10 (1476 views)
Re: [tules] How to search and increment only the numbers [In reply to] Can't Post

Ah, I was confused- I thought you (Tules) were the original poster. I guess I have to get my "T" names straight.

Regarding this post,

Quote
it was neccesary the way i did it because the loop would overwrite each time otherwise, I'm sure theres a way to do it otherwise by pushing them into an array before writting to file for instance, but this does the job fine.

its actually quite easy to change your script to do this; no arrays needed.

I do not have access to perl right now, so I cannot test it, but assuming it worked before, I think you can just move the open FILE2 outside the "while" loop. That way you don't have to append.


Code
open(FILE, "Input.txt"); 


open(FILE2, ">output.txt");
while(<FILE>) {

$_=~ s/(\d+)/$1 + 5/e;

print FILE2 "$_";}

close(FILE);close(FILE2);


Regarding your other post, I looked at it, but didn't understand much. I am a still a beginner with Perl :)

But if you want someone to respond, I would suggest that you write exactly (specific!) what went wrong with your script. Where there errors? If so, what where they? Post over there and you'll probably have better luck.
The more you teach me, the more I learn. The more I learn, the more I teach.


tules
Novice

Sep 1, 2008, 6:23 PM

Post #7 of 10 (1473 views)
Re: [PGScooter] How to search and increment only the numbers [In reply to] Can't Post

oh shit yeah i didnt even realise i'd put the open file function inside the loop, no wonder, its my lack of attention thats my biggest enemy!


tux
New User

Oct 11, 2008, 4:33 AM

Post #8 of 10 (1266 views)
Re: [tules] How to search and increment only the numbers [In reply to] Can't Post

Thanks Tules & PGScooter explaining how to work with regular expressions

thanks
TuX


KevinR
Veteran


Oct 11, 2008, 10:26 AM

Post #9 of 10 (1255 views)
Re: [tux] How to search and increment only the numbers [In reply to] Can't Post

When you want to edit a file inplace, use the Tie::File module, it comes with perl and is simple to use. See the Tie::File documentation if interested.
-------------------------------------------------


FishMonger
Veteran / Moderator

Oct 12, 2008, 1:53 PM

Post #10 of 10 (1239 views)
Re: [KevinR] How to search and increment only the numbers [In reply to] Can't Post


In Reply To
When you want to edit a file inplace, use the Tie::File module, it comes with perl and is simple to use. See the Tie::File documentation if interested.

Or, for something as trivial as the requirements in this question, just do a 1 liner inline edit.

Code
perl -pi.bak -e "s/(\d+)/$1 + 5/e" input.txt


If you're on a *nix system, use single quotes instead.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives