Perl Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Question
# 1  
Old 07-24-2008
Perl Question

Hi ,

I have a file that looks like this:
Code:
24.999
4.987
34.987
3.900
97.334

My Question:
How i can right aligment in perl the number to look like this:
Code:
24.999
 4.987
34.987
 3.900
97.334

I need code using sprintf or printf or format.

I am new to perl and I am not getting it reading perldoc ...i am lost.

Please provide help will be appreciated

thanks,
# 2  
Old 07-24-2008
Don't even treat them like numbers:

Code:
printf( "%6s", $number );

# 3  
Old 07-24-2008
another way:

Code:
while(<DATA>){
   my $t = sprintf("%6.3f",$_); 
   print $t,"\n";
}
__DATA__
24.999
4.987
34.987
3.900
97.334

Using the %f template treats the values as floating point numbers so if you do something like this:

Code:
while(<DATA>){
   my $t = sprintf("%6.2f",$_); 
   print $t,"\n";
}
__DATA__
24.999
4.987
34.987
3.900
97.334

the value of the number can change due to rounding off. If all you really want to do is right-justify, the %s template BMDan suggests might be more appropriate.
# 4  
Old 07-25-2008
Code:
perl -ne 'if($.==1){$len=length($_);} printf("%${len}s",$_);' file

# 5  
Old 07-25-2008
Thats the best suggestion I"ve ever seen you post summer_cherry. It does assume that the first line of the file should be used to set the width of all the rest of the lines, but sometimes we do have to make assumptions when posting suggestions.
# 6  
Old 07-25-2008
Thanks for everyone reply.

I am sorry for replying little late.

Here is part of perl code that i want to print out.

Code:
 print ("------------------------------------------------------","\n");
  printf "%+2sSELECTION\t%+3sVOTES\t%+3sPERCENTAGE\n";
  print ("------------------------------------------------------","\n");

  printf("Choice 1\t %6s $count\t\t %6s $iRoundup1 \n");
  printf("Choice 2\t %6s $count\t\t %6s $iRoundup2 \n");
  printf("Choice 3\t %6s $count\t\t %6s $iRoundup3 \n");

  #printf "%1s", Choice 1\t,"%6s",$count\t\t,"%6s",$iRoundup1\n";
  #printf "%1s", Choice 2\t,"%6s",$count2\t\t,"%6s",$iRoundup2\n";
  #printf "%1s", Choice 3\t,"%6s",$count3\t\t,"%6s",$iRoundup3\n";
  print ("------------------------------------------------------", "\n");

so basically i want to print numbers right alignment.

I tried DMan printf("%6s", $number) but it didn't worked.

Or
May be i am doing something wrong.

Can some help me and fix the printf so it print number right align.

thank you for your time
-Lalit
# 7  
Old 07-27-2008
Quote:
Originally Posted by email-lalit
Code:
 print ("------------------------------------------------------","\n");
  printf "%+2sSELECTION\t%+3sVOTES\t%+3sPERCENTAGE\n";
  print ("------------------------------------------------------","\n");

  printf("Choice 1\t %6s\t\t %6s\n", $count, $iRoundup1);
  printf("Choice 2\t %6s\t\t %6s\n", $count2, $iRoundup2);
  printf("Choice 3\t %6s\t\t %6s\n", $count3, $iRoundup3);

  print ("------------------------------------------------------", "\n");

Fixed it for you. Enjoy.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl question about

Hello everybody, I am new at the forum and a total newbie when it comes to Unix. I am trying to see how I can add the ability to kill a user's processes? I want to add this to my Shel Script Add the code/process into a subroutine. Also, I would like to use an array to store the list... (0 Replies)
Discussion started by: kinelisch
0 Replies

2. Shell Programming and Scripting

PERL Question ...

I am reading a file in perl script .. during the debug the $linein value is : linein : +ASM1,sys,||¬ |3Æqúoü;”ט|| from this line I am getting the tmepuser and password from above : ($tmpuser, $pwd) = ($linein =~ /^$server\s*,\s*(+)\s*,\|\|(.+)\|\|/sm); I am getting $tmpuser and... (2 Replies)
Discussion started by: talashil
2 Replies

3. Shell Programming and Scripting

another perl question

I have a question regarding bulding a hash from a file which has below pattern I thought I could write something like this but clearly my syntax is way off $/ = "\n\n"; $" = "\n"; open(FILE, file1) || die; my %keymaster = ( ); while (<FILE>) { my $topinfo =~... (5 Replies)
Discussion started by: hankooknara
5 Replies

4. Shell Programming and Scripting

another perl question

I fail to see how below answer is 1? can someone explain this for me? DB<3> $string = "The cat sat on the mat"; DB<4> $animal = ($string =~ m/The (.*) sat/); DB<5> print $animal; 1 (2 Replies)
Discussion started by: hankooknara
2 Replies

5. Shell Programming and Scripting

Perl question regarding [ ]

Below program, I do not get why item I am looking for is , instead of . When I do $#text, i get the right value for $value1, but when I do , i get somsething4, instead of somsethingxxxxxxxxxxxxxxxxxxx(which is what I am looking for. when I do , I get empty.. why? what did I do wrong? can you... (2 Replies)
Discussion started by: hankooknara
2 Replies

6. Shell Programming and Scripting

another perl question

I copy and paste from the book but this thing is not working. I cannot figure out what is wrong with myline 9.. can someone please tell me # cat ./sort4.pl #!/usr/bin/perl -w use strict; use warnings; my $input = shift; my $output = shift; open(IN, '<', $input) or die... (4 Replies)
Discussion started by: hankooknara
4 Replies

7. Shell Programming and Scripting

PERL question

Hello, pkzipc of a certain zip file yeilds the following in shell PKZIP(R) Version 6.0 FAST! Compression Utility for AIX Copyright 1989-2002 PKWARE Inc. All Rights Reserved. Registered Version PKZIP Reg. U.S. Pat. and Tm. Off. Patent No. 5,051,745 Viewing .ZIP: test.zip Length... (13 Replies)
Discussion started by: jerardfjay
13 Replies

8. Shell Programming and Scripting

perl question

If I use 2 system commands in a script, will one finish before the next one starts? or will it start the first and the second at the same time? i.e. system("ps | grep rminer"); system("ls -al | grep 431"); (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies

9. Shell Programming and Scripting

Perl: tk question

When i run my perl/tk script, a perl window pops up behind the GUI window,, can this be hidden???? Also, can the Icon be changed, the Tk icon in every window??? (1 Reply)
Discussion started by: perleo
1 Replies

10. Shell Programming and Scripting

Question about Perl

Where can i find solid information about programming in Perl? Thank you in advance!!!:) (5 Replies)
Discussion started by: SolidSnake
5 Replies
Login or Register to Ask a Question