mathematical operation in a perl one liner in substitute

K

kevin kitenik

Hi everybody,

i need to make an arithmetic operation ine one field in a csv file.

exp:
cat file
a ; 123
b ; 432
c ; 45

i need a one liner perl code to substitute the second filed by
its value divided by 10
to have
a ; 12.3
b ; 43.2
c ; 4.5

i need this sample of code:

perl -i -pe 's/;(\d+)/ $1/10 /' file

this can't work because i dont know how to tell perl
to execute $1/10 (or even $1 - 10 )

please, help, i know i can do it onether way,
but want to know if this is possible
because it's so easy (one line)
 
J

Jürgen Exner

kevin kitenik said:
i need a one liner perl code to substitute the second filed by
its value divided by 10
to have
a ; 12.3
b ; 43.2
c ; 4.5

i need this sample of code:

perl -i -pe 's/;(\d+)/ $1/10 /' file

See "perldoc perlop", section "Regexp Quote-Like Operators", subsection
"s/PATTERN/REPLACEMENT/egimosx":

Options are:
e Evaluate the right side as an expression.

jue
 
J

John W. Krahn

kevin said:
i need to make an arithmetic operation ine one field in a csv file.

exp:
cat file
a ; 123
b ; 432
c ; 45

i need a one liner perl code to substitute the second filed by
its value divided by 10
to have
a ; 12.3
b ; 43.2
c ; 4.5

i need this sample of code:

perl -i -pe 's/;(\d+)/ $1/10 /' file

this can't work because i dont know how to tell perl
to execute $1/10 (or even $1 - 10 )

$ echo "a ; 123
b ; 432
c ; 45
" | perl -pe's{(;\s*)(\d+)}{$1.($2/10)}e'
a ; 12.3
b ; 43.2
c ; 4.5




John
 
T

Tad J McClellan

kevin kitenik said:
Hi everybody,

i need to make an arithmetic operation ine one field in a csv file.

exp:
cat file
a ; 123
b ; 432
c ; 45

i need a one liner perl code to substitute the second filed by
its value divided by 10
to have
a ; 12.3
b ; 43.2
c ; 4.5

i need this sample of code:

perl -i -pe 's/;(\d+)/ $1/10 /' file

this can't work because i dont know how to tell perl
to execute $1/10 (or even $1 - 10 )


You have 3 more problems than you are aware of.

First, lets deal with the problem that you are aware of:

you want an s///e modifier.

As to the 3 problems you have not gotten to yet:

1) your pattern will not match your data as it does not
allow spaces after the semicolon

2) even if your pattern matched, there would be no semicolons in
the resulting file since you failed to put them back in

3) you have an unescaped slash character in your replacement code
(I'll use curly braces as an alternate delimiter to fix this one).


So:

perl -i -pe 's{(;\s*)(\d+)}{ $1 . $2/10 }e' file
 
K

kevin kitenik

Thanks a lot to all your replies.
very usefuls.
il really appreciate yours helps
have a nice day.
 
U

Uri Guttman

TJM> I just want to say that that is some beautiful code.

didn't anyone smell homework here? might as well golf it since the
teacher would never expect that from a newbie!

there is no mention of other fields, numbers other than integers, etc.

perl -pe's{(\d+)}{$1/10}e'

uri
 
T

Tim Greer

Uri said:
TJM> I just want to say that that is some beautiful code.

didn't anyone smell homework here? might as well golf it since the
teacher would never expect that from a newbie!

there is no mention of other fields, numbers other than integers, etc.

perl -pe's{(\d+)}{$1/10}e'

uri

Actually, out of curiosity, are there any classes that would have
homework that actually use Perl code for problem solving? I'm being
genuine about this question.
 
U

Uri Guttman

TG> Actually, out of curiosity, are there any classes that would have
TG> homework that actually use Perl code for problem solving? I'm being
TG> genuine about this question.

just google this group or the perl beginner's list for homework. many
time obvious homework problems have been posted and usually labeled as
such by repliers (never by the OP). the problems have simple goals,
contrived data (like this one), etc. many times the problem states a
desired solution technique which is bogus (e.g. you must use a regex to
solve this). and if accused of posting homework, the OP will usually
defend it in some silly way.

as for the actual classes, yes, some colleges (community ones in
particular but others too) do teach SOME perl. this may be in a
'programming languages' class which flashes the students with snippets
of different langs but no real understanding. or an intro to perl class
taught by someone who doesn't know (or like!) perl very much. perl is
not taught as a mainstream language like java/c++ since those have more
jobs out there and colleges like to teach to the job market.

uri
 
J

Jürgen Exner

Uri Guttman said:
TG> Actually, out of curiosity, are there any classes that would have
TG> homework that actually use Perl code for problem solving? I'm being
TG> genuine about this question.

just google this group or the perl beginner's list for homework. many
time obvious homework problems have been posted and usually labeled as
such by repliers (never by the OP). the problems have simple goals,
contrived data (like this one), etc. many times the problem states a
desired solution technique which is bogus (e.g. you must use a regex to
solve this). and if accused of posting homework, the OP will usually
defend it in some silly way.


Hmmmm, I am not sure I totally agree. Certainly there are those cases
you mentioned, no doubt about it.

On the other hand the OPs problem could also be viewed as a perfect
excerpt from a larger project, stripped down to the bare minimum code
required to exhibit the problem, just like requested in the posting
guidelines.

jue
 
U

Uri Guttman

JE> Hmmmm, I am not sure I totally agree. Certainly there are those cases
JE> you mentioned, no doubt about it.

JE> On the other hand the OPs problem could also be viewed as a perfect
JE> excerpt from a larger project, stripped down to the bare minimum code
JE> required to exhibit the problem, just like requested in the posting
JE> guidelines.

you could view it that way but those who really show a snippet will say
that and it is part of a larger program. the OP doesn't say that but the
requirement for a one liner is also a clue to homework. real world projects
never require a one liner. anyhow the OP can answer this question. for
now unless told otherwise, i will assume homework.

and another odd point. when have you ever seen a csv file with ; as the
delimiter? i doubt most csv oriented applications (those that can
read/write them) would have ; as a normal option. tab or comma are the
standard ones.

it doesn't matter much anyhow. the OP got a bunch of answers.

uri
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top