substitution with computed replacements

F

fred

I took this from Minimal perl by Tim Maher.

s/\d+ /$& * 1.6/ge

I am trying to add numbers instead of converting miles to KM.

input
2 3 4 1
3 4 6

output
2 3 4 1 = 10
2 4 6 = 12

1. What would the replacement part be ?
2. What should the seach part be if we have ten numbers instead of
four?

Thanks
 
K

kappa

I took this from Minimal perl by Tim Maher.

s/\d+ /$& * 1.6/ge

I am trying to add numbers instead of converting miles to KM.

input
2 3 4 1
3 4 6

output
2 3 4 1 = 10
2 4 6 = 12

1. What would the replacement part be ?
2. What should the seach part be if we have ten numbers instead of
four?

Thanks

If you want to practice regexp stuff:

kappa@kappa:~$ head -2 /etc/hosts | perl -lne 'use List::Util "sum";
@a = /(\d+)/g; print "@a = ",sum @a'
127 0 0 1 = 128
127 0 1 1 = 129
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top