For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2006 > Don't understand printf









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Don't understand printf
Salve Håkedal

2006-03-07, 6:58 pm

I issue this command with valg as 'm' or 'u':
$ echo hei | awk -f test.awk -v valg=m or u

$ cat test.awk
#!/usr/bin/gawk

{
sum = 34579
}
END {
if (valg ~ /m/) {
print ""
derav_mva = sum * 25 / 125
printf " derav mva. %.f\n", derav_mva
mellomrom = 71 - length(sum)
printf "Å betale %*s%.f\n", mellomrom, " ", sum
}
if (valg ~ /u/) {
print ""
pluss_mva = sum / 4
printf " + mva. %.f\n", pluss_mva
full_sum = sum + pluss_mva
mellomrom = 71 - length(full_sum)
printf "Å betale %*s%.f\n", mellomrom, " ", full_sum
}
}

but the last output line is not the same length for
m and u.

Why?
Grant

2006-03-07, 6:58 pm

On Tue, 07 Mar 2006 09:05:42 -0600, Salve Håkedal <ikkjespam.salve@fiolinmaker.no> wrote:

>I issue this command with valg as 'm' or 'u':
>$ echo hei | awk -f test.awk -v valg=m or u
>
>$ cat test.awk
>#!/usr/bin/gawk
>
>{
>sum = 34579
>}
>END {
>if (valg ~ /m/) {
> print ""
> derav_mva = sum * 25 / 125
> printf " derav mva. %.f\n", derav_mva
> mellomrom = 71 - length(sum)
> printf "Å betale %*s%.f\n", mellomrom, " ", sum
> }
>if (valg ~ /u/) {
> print ""
> pluss_mva = sum / 4
> printf " + mva. %.f\n", pluss_mva
> full_sum = sum + pluss_mva
> mellomrom = 71 - length(full_sum)
> printf "Å betale %*s%.f\n", mellomrom, " ", full_sum
> }
>}
>
>but the last output line is not the same length for
>m and u.


Because you are not giving printf and room to accommodate a variable
length number --> you specify a field width after the "%", not by
supplying a " " style spacer.

Grant.
--
Cats are smarter than dogs. You can't make eight cats pull
a sled through the snow.
Salve Håkedal

2006-03-07, 6:58 pm

On 2006-03-07, Grant <bugsplatter@gmail.com> wrote:
> On Tue, 07 Mar 2006 09:05:42 -0600, Salve Håkedal <ikkjespam.salve@fiolinmaker.no> wrote:
>
>
> Because you are not giving printf and room to accommodate a variable
> length number --> you specify a field width after the "%", not by
> supplying a " " style spacer.
>

Thank you, but I don't understand that explanation; I'm a newbie!
Is it possible to explain it better?
What is a " " style spacer?
And why don't the same thing happen for both u and m?
How would you write it?
Grant

2006-03-07, 6:58 pm

On Tue, 07 Mar 2006 14:38:22 -0600, Salve Håkedal <ikkjespam.salve@fiolinmaker.no> wrote:

^^^^^^^^^^^^^^^^[color=darkred]
>What is a " " style spacer?


as highlighted above

>How would you write it?


Perhaps: printf "%20s %6.2f\n", "+ mva.", pluss_mva

Play with printf until you get what you looking for, until it
makes sense to you.

See also: man sprintf

Grant.
--
Cats are smarter than dogs. You can't make eight cats pull
a sled through the snow.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com