perl: printf indentation problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl: printf indentation problem
# 1  
Old 03-27-2008
Data perl: printf indentation problem

hi all,
im having a problem with using perl printf. my requirement is to print a string (like [OK]) at the right most end of the screen.
i tried this perl script, but it fails with an error;
Code:
#!/usr/bin/perl 

use strict;
use warnings;

my $scrW = 0;

my $str = `stty size`;   # get the screen character width 
$str =~ m/(\d+)\s*$/;    # extract the char width
$scrW = eval($1);           # put the char width in a varable $scrW

$scrW -=10;                     # reduce the char width by 10
printf "%($scrW)s\n", "abcde";

tried to use printf with a variable as indentation length, but fails.
this type of method works in bash.
does anyone have an idea ?

thankx.
# 2  
Old 03-27-2008
You don't need the eval, just assign $1 to $scrW. And take out the parentheses in the printf. You then need braces around the variable name to disambiguate the variable name from the traliing s, like ${scrW}s

If you subtract ten, you obviously don't use the entire screen width, but I guess you know that.
# 3  
Old 03-27-2008
Quote:
Originally Posted by era
You don't need the eval, just assign $1 to $scrW. And take out the parentheses in the printf. You then need braces around the variable name to disambiguate the variable name from the traliing s, like ${scrW}s

If you subtract ten, you obviously don't use the entire screen width, but I guess you know that.
thankx era for the quick reply,
i subtracted ten thinking that the length of the string im gonna print is 10. Smilie
what u said worked Smilie, but i still had to use the eval.
anyway, thankx for the solution.
# 4  
Old 03-27-2008
printf already does the subtraction for you; a positive number in %72s says pad with spaces as much as necessary to fill to the right up to, here, 72 characters width.
# 5  
Old 03-27-2008
Believe us, the eval really is redundant
Code:
$ perl -e '($c)=qx(stty size)=~/\d+\s+(\d+)/;printf"%${c}s\n",$ARGV[0]' Hello\ World
                                                                          Hello World

# 6  
Old 03-27-2008
sorry about my lack of understanding on how these functions work.
buffoonix`s method also works fine. thankx.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

[Perl] Different printf formating for different print options

Hi, Struggling with single quotes, double quotes, etc. I want to print a header line, followed by lines with actual values, based on a print option. In real life it is going to be something like 15 print options and 50 values. Output will be 1 header and several value lines. In this example... (5 Replies)
Discussion started by: ejdv
5 Replies

2. Shell Programming and Scripting

printf (awk,perl,shell) float rounding issue

Hi guys, could someone throw some light on the following behaviour of printf (I'll start with info about the system and the tool/shell/interpreter versions)?: $ uname -a Linux linux-86if.site 3.1.0-1.2-desktop #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0) x86_64 x86_64 x86_64... (9 Replies)
Discussion started by: elixir_sinari
9 Replies

3. Shell Programming and Scripting

problem with printf in shell script

i have written small script as follows: name="hi hello" printf "%-20s" $name This gives me strange output. -20s format is applied on both word of string. i.e it displays both word hi and hello in space of 20 length. I want to display entire string "hi hello" in length of 20 space. plz... (2 Replies)
Discussion started by: admc123
2 Replies

4. Shell Programming and Scripting

printf problem

In one of the scripts I am using pintf function as following printf "%s%s%s\n" "$f1" "$f2" "$f3" f3 variable contains a string of 10 characters. However it has value first 7 character and last 3 characters are empty. Example Aaaaaaa<3 spaces> bbbbbbb<3 spaces> ccccccc<3 spaces>... (4 Replies)
Discussion started by: varunrbs
4 Replies

5. Shell Programming and Scripting

Printf problem

I am having a major problem with printf, The more I pad it, the less I see :( The problem is in the first function, report Am I ruining output somewhere? I wont print out the names propely, it cuts them off or deletes them completely :( #!/bin/bash report() { printf "%-10s" STUD# ... (2 Replies)
Discussion started by: L0ckz0r
2 Replies

6. Shell Programming and Scripting

printf vs sprintf - perl

I would like to assign the output of printf to a variable in perl , it give me back a "1" instead of the time. How can I stuff the variable with what printf returns? Here is my code: #!/usr/bin/perl $time = localtime(time);... (3 Replies)
Discussion started by: Dabheeruz
3 Replies

7. Shell Programming and Scripting

Awk printf problem

Hi, I've got a basic problem using printf statement in awk. I want to write float values with always 8 characters width. Examples : 1.345678 12.45678 123.4567 1234.678 -23.5678 -2.45678 -23456.8 ..... I cannot find the right printf format %8.1f, %7.5f.... Can anyone help ?... (4 Replies)
Discussion started by: cazhot
4 Replies

8. Shell Programming and Scripting

printf problem

I have the following code: $ awk '{ printf "%-10s %s\n", $1, $2, $3, $4, $5, $5, $6 }' file i can only print the first 2 elements ($1,$2). How can i print all the elements to appear like this: aardvark 5555553 jhfjhfjkg efiigig ejkfjkej wjkdjk alpo-net 5553412 ... (2 Replies)
Discussion started by: DDoS
2 Replies

9. UNIX for Advanced & Expert Users

awk printf problem

Hi Friends, Can anyone guide me how to compute sum of column4 from the below file x using awk command? when i do using awk I'm getting sum 7482350198352648.000000 which is not accurate. $ cat x 56,232,dfgjkhdfj,,56,anand 56,22,dfgjkhdfj,7482347823453123.97834 ,56,Khan 56,23,dfgjkhdfj, ... (6 Replies)
Discussion started by: krishna
6 Replies

10. Programming

disturbing problem with PRINTF() !!

hello everybody, here is my problem: ________________________________________ #include <stdio.h> int main() { int i=10; printf("value is %i",i); return 0; } _________________________________________ when i compile and execute, nothing appears on screen!! but if i replace the printf... (2 Replies)
Discussion started by: brain_processin
2 Replies
Login or Register to Ask a Question