Re: printf

2013-03-04 Thread Nathan Hilterbrand

On 03/04/2013 11:22 AM, Chris Stinemetz wrote:

I would like to pass a list of variables to printf.

Is there a way to multiply a set printf length instead of
righting typing printf for each variable?

what I am trying to do is below:

  printf "%-${longest}s x 27\n",

  
$rptType,$mkt,$timeStamp,$cell,$sector,$carr,$satt,$sest,$fit,$psEst,$catt,$cest,$pcEst,$rfLost,

  
$cpDropCell,$cpDropRnc,$tuneAway,$tDrops,$pDcr,$ia,$pIa,$tccf,$failAp,$failTp,$failA10,$failAAA,$failPDSN;

Thank you,

Chris


Chris,

You can use the '*' length specifier to do what you are wanting to do..  
If you set '$longest' to the longest length, then this should do the trick:


printf("%-*s\n", $longest, $string);

Unfortunately, that only gets you one string out.  Using the printf in 
this way would require you to repeat the length ahead of each variable 
if you want to print them all at one time, with one printf call.  There 
are further "printf tricks" that would allow you to specify the length 
only once, but they get sort of ugly (IMHO).  If I were doing this,

I think that I would do something like:

my @vals2print;
my $longest;
foreach my $val 
($rptType,$mkt,$timeStamp,$cell,$sector,$carr,$satt,$sest,$fit,$psEst,$catt,$cest,$pcEst,$rfLost, 
$cpDropCell,$cpDropRnc,$tuneAway,$tDrops,$pDcr,$ia,$pIa,$tccf,$failAp,$failTp,$failA10,$failAAA,$failPDSN) 
{

  push @vals2print, $val;
  $longest = ($val > $longest) ? $val : $longest;
}
printf("%-*s ",$longest, $_) foreach @vals2print;
print "\n";

It might be more efficient to just generate the format string 
programmatically, and only make one printf call:


my @vals2print;
my $longest;
foreach my $val 
($rptType,$mkt,$timeStamp,$cell,$sector,$carr,$satt,$sest,$fit,$psEst,$catt,$cest,$pcEst,$rfLost, 
$cpDropCell,$cpDropRnc,$tuneAway,$tDrops,$pDcr,$ia,$pIa,$tccf,$failAp,$failTp,$failA10,$failAAA,$failPDSN) 
{

  push @vals2print, $longest;
  $longest = ($val > $longest) ? $val : $longest;
}
my $pf_format = "%-" . $longest . "s ";
$pf_format = $pf_format x scalar @vals2print;
$pf_format .= "\n";
printf($pf_format, @vals2print);


Note:  I just typed that up without testing, so caveat scriptor. 
Hopefully the ideas come across, at least.


Nathan


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2013-03-04 Thread Dr.Ruud

On 2013-03-04 20:27, Dr.Ruud wrote:


   print sprintf +("| %-${wid}s" x @data)." |\n", @data;


Rather:

  print sprintf +("| %-${wid}s " x @data) . "|\n", @data;

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2013-03-04 Thread Dr.Ruud

On 2013-03-04 17:22, Chris Stinemetz wrote:


I would like to pass a list of variables to printf.

Is there a way to multiply a set printf length instead of
righting typing printf for each variable?

what I am trying to do is below:

  printf "%-${longest}s x 27\n",

  
$rptType,$mkt,$timeStamp,$cell,$sector,$carr,$satt,$sest,$fit,$psEst,$catt,$cest,$pcEst,$rfLost,

  
$cpDropCell,$cpDropRnc,$tuneAway,$tDrops,$pDcr,$ia,$pIa,$tccf,$failAp,$failTp,$failA10,$failAAA,$failPDSN;


There are many ways to do this. For 'lazy' values, use a subroutine.


Another approach:

perl -Mstrict -we'
  my @data = qw/ abc d ef ghi jklmn /;

  my $wid = 1;
  $wid < length and $wid = length for @data;

  print sprintf +("| %-${wid}s" x @data)." |\n", @data;
'
| abc  | d| ef   | ghi  | jklmn |

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2012-09-02 Thread Torsten
Am Sun, 02 Sep 2012 13:12:02 +0200
schrieb "Dr.Ruud" :

> On 2012-08-31 15:17, Torsten wrote:
> 
> > I found a strange behaviour for printf: If you do for example
> >
> > printf "%d",29/100*100
> >
> > you get 28. But with
> >
> > printf "%d",29*100/100
> >
> > it's 29. Seems to be related to rounding.
> > The perl version is 5.10.1 on debian.
> 
> There is nothing strange about it.
> 
> I think you are looking for the "%.0f" format.
> 
> See also 'perldoc -q decimal'.
> 

Good idea. Thank you!

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2012-09-02 Thread Dr.Ruud

On 2012-08-31 15:17, Torsten wrote:


I found a strange behaviour for printf: If you do for example

printf "%d",29/100*100

you get 28. But with

printf "%d",29*100/100

it's 29. Seems to be related to rounding.
The perl version is 5.10.1 on debian.


There is nothing strange about it.

I think you are looking for the "%.0f" format.

See also 'perldoc -q decimal'.

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2012-08-31 Thread Shawn H Corey
On Fri, 31 Aug 2012 15:28:38 +0300
"Octavian Rasnita"  wrote:

> From: "Torsten" 
> 
> > Hey,
> > 
> > I found a strange behaviour for printf: If you do for example
> > 
> > printf "%d",29/100*100
> > 
> > you get 28. But with
> > 
> > printf "%d",29*100/100
> > 
> > it's 29. Seems to be related to rounding.
> 
> 
> Yep
> 
> use bignum;
> 
> might help.
> 
> Octavian
> 
> 

A known problem; it's call Numerical Analysis
https://en.wikipedia.org/wiki/Numerical_analysis


-- 
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

_Perl links_
official site   : http://www.perl.org/
beginners' help : http://learn.perl.org/faq/beginners.html
advance help: http://perlmonks.org/
documentation   : http://perldoc.perl.org/
news: http://perlsphere.net/
repository  : http://www.cpan.org/
blog: http://blogs.perl.org/
regional groups : http://www.pm.org/

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2012-08-31 Thread Octavian Rasnita
From: "Torsten" 

> Hey,
> 
> I found a strange behaviour for printf: If you do for example
> 
> printf "%d",29/100*100
> 
> you get 28. But with
> 
> printf "%d",29*100/100
> 
> it's 29. Seems to be related to rounding.


Yep

use bignum;

might help.

Octavian


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf explicitly

2011-03-19 Thread Uri Guttman
> "PJ" == Paul Johnson  writes:

  >> 2,1,1,1175,2.58522727272727,1
  >> 2,1,1,1175,,
  >> 2,1,1,1175,1.354167,1
  >> 2,1,1,1175,1.82765151515152,1
  >> 2,1,1,1175,,
  >> 2,2,1,1175,,
  >> 8,1,1,1175,,
  >> 10,2,1,1175,0.710227272727273,1

that is his output from MY version of the program, not input.

  >> use File::Slurp;
  >> 
  >> my $filepath = 'C:/temp/PCMD';
  >> my $output  = 'output.txt';
  >> 
  >> my %cols = (
  >> cell   => 31,
  >> sect   => 32,
  >> chan   => 38,
  >> dist   => 261,
  >> precis => 262,
  >> );


  PJ> These values don't match the data you have above.  Do you really have
  PJ> 262 items of data per line?

yes, he does. please follow the whole thread.

  >> next unless $line =~ /;/;

  PJ> Your data is separated by commas, not semicolons.

again, follow the whole thread. yes, he put his output at the top.

  >> $record{dist} = ( length( $record{dist}) > 1 ) ? 
$record{dist}/6.6/8/2*10/10 : '' ;

  PJ> This seems strange.

it is a wacky expression but legal and seemingly what he wants. 

  >> 
  >> printf '<%.2g>', @report ;
  >> #print @report ;
  >> write_file($output, @report) ;

  PJ> And this bit is just wrong.

which bit? the write_file is perfectly fine. the printf is very
wrong. and i posted about it already. again (you like that word), please
follow the thread and don't jump in the middle.


  PJ> my %cols = (
  PJ> cell=> 0,
  PJ> sect=> 1,
  PJ> chan=> 2,
  PJ> dist=> 3,
  PJ> precis  => 4,
  PJ> );

wrong.

  PJ> my @records;
  PJ> open my $fh, "<$filepath";
  PJ> while (my $line = <$fh>) {
  PJ> next unless $line =~ /,/;

wrong.

  PJ> my %record;
  PJ> # this gets just what you want into a hash using a hash slice and an # 
array slice.
  PJ> # the order of keys and values will be the same for any # given hash

  PJ> @record{ keys %cols } = (split /,/, $line)[ values %cols ];

wrong.


  PJ> for my $s (@sorted) {
  PJ> printf "<%.2g>", $s->{$_} for qw(cell sect carr chan dist precis);

wrong. he wants output to the screen and also to a file.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf explicitly

2011-03-19 Thread Paul Johnson
On Fri, Mar 18, 2011 at 10:43:09PM -0600, Chris Stinemetz wrote:
> Hello,
> 
> I would like to explicitly use printf '<%.2g>' for the element in the array 
> called dist.
> Below is the error I am getting and below that is a sample of the output I 
> can currently generate before I add printf.
> 
> Thank you
> 
> Argument "2,1,1,1175,2.58522727272727,1\n" isn't numeric in printf at 
> ./DOband1.pl line 47.
> <2> 
> 
> 2,1,1,1175,2.58522727272727,1
> 2,1,1,1175,,
> 2,1,1,1175,1.354167,1
> 2,1,1,1175,1.82765151515152,1
> 2,1,1,1175,,
> 2,2,1,1175,,
> 8,1,1,1175,,
> 10,2,1,1175,0.710227272727273,1
> 
> 
> 
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> use File::Slurp;
> 
> my $filepath = 'C:/temp/PCMD';
> my $output  = 'output.txt';
> 
> my %cols = (
>   cell=> 31,
>   sect=> 32,
>   chan=> 38,
>   dist=> 261,
>   precis  => 262,
> );

These values don't match the data you have above.  Do you really have
262 items of data per line?

> my @records;
> 
> my @lines = read_file( $filepath );
> 
> chomp @lines;
> 
> foreach my $line ( @lines ) {
> 
>   next unless $line =~ /;/;

Your data is separated by commas, not semicolons.

>   my %record;
> # this gets just what you want into a hash using a hash slice and an # array 
> slice. 
> # the order of keys and values will be the same for any # given hash
> 
>   @record{ keys %cols } = (split /;/, $line)[ values %cols ];

Again, commas, not semicolons.

>   $record{carr} = ( $record{chan} == 15 ) ? 2 : 1 ;   
>   $record{dist} = ( length( $record{dist}) > 1 ) ? 
> $record{dist}/6.6/8/2*10/10 : '' ;

This seems strange.

>   push( @records, \%record ) ;
> }
> 
> my @sorted = sort {
>   $a->{cell} <=> $b->{cell} ||
>   $a->{sect} <=> $b->{sect} ||
>   $a->{carr} <=> $b->{carr}
>   } @records ;
> 
> my @report = map 
> "$_->{cell},$_->{sect},$_->{carr},$_->{chan},$_->{dist},$_->{precis}\n" , 
> @sorted;
> #my @report = map "@{$_{ keys %cols }}\n", @records ;
> 
>  printf '<%.2g>', @report ;
> #print @report ;
> write_file($output, @report) ;

And this bit is just wrong.

I don't really know what you are doing here, but this might get you a
little further.  I've left most of it the way you had it and just fixed
up a few problems or made a few changes to get things to "work".


#!/usr/bin/perl

use warnings;
use strict;
use autodie;

my $filepath = 'PCMD';

my %cols = (
cell=> 0,
sect=> 1,
chan=> 2,
dist=> 3,
precis  => 4,
);

my @records;
open my $fh, "<$filepath";
while (my $line = <$fh>) {
next unless $line =~ /,/;
my %record;
# this gets just what you want into a hash using a hash slice and an # array 
slice.
# the order of keys and values will be the same for any # given hash

@record{ keys %cols } = (split /,/, $line)[ values %cols ];
$record{carr} = ( $record{chan} == 15 ) ? 2 : 1 ;
$record{dist} = ( length( $record{dist}) > 1 ) ? 
$record{dist}/6.6/8/2*10/10 : '' ;

push @records, \%record;
}

my @sorted = sort {
$a->{cell} <=> $b->{cell} ||
$a->{sect} <=> $b->{sect} ||
$a->{carr} <=> $b->{carr}
} @records ;

for my $s (@sorted) {
printf "<%.2g>", $s->{$_} for qw(cell sect carr chan dist precis);
print "\n";
}


-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf explicitly

2011-03-18 Thread Uri Guttman
> "CS" == Chris Stinemetz  writes:

  CS> I would like to explicitly use printf '<%.2g>' for the element in
  CS> the array called dist.  Below is the error I am getting and below
  CS> that is a sample of the output I can currently generate before I
  CS> add printf.

  CS> Thank you

  CS> Argument "2,1,1,1175,2.58522727272727,1\n" isn't numeric in printf at 
./DOband1.pl line 47.

  CS> my @report = map 
"$_->{cell},$_->{sect},$_->{carr},$_->{chan},$_->{dist},$_->{precis}\n" , 
@sorted;

what is in @report? it is not a number. it is a string with numbers in
it along with commas and a newline

  CS>  printf '<%.2g>', @report ;

even if @report contained good data, that would only print 1
value. printf doesn't loop over its data. it only prints values for the
keys in the format string. you have only one format element in there.

if you want each value formatted, then use sprintf on each one before
you do the report. i leave that as an exercise for you.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2010-07-12 Thread John W. Krahn

Chas. Owens wrote:

On Mon, Jul 12, 2010 at 07:25, Rob Coops  wrote:
snip

In other words you are using a function inside a function. If you split this
into two lines.
my @split_result = split /:/;
print $outfile join(" ", "socks5", @split_result)

snip

By that logic it should read

my @split_result = split /:/;
my $join_result  = join " ", "socks5", @split_result;
print $outfile $join_result;

or more verbosely:

my @split_result = split /:/;
unshift @split_result, "socks5";
my $join_result  = join " ", @split_result;
print $outfile $join_result;

or the ridiculously magicless:

my @split_result = split /:/, $_, 0;
unshift @split_result, "socks5";
my $join_result  = join " ", @split_result;
print $outfile $join_result;


Or:

my @split_result = ( 'socks5', split /:/ );
print $outfile "@split_result";




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2010-07-12 Thread Chas. Owens
On Mon, Jul 12, 2010 at 07:25, Rob Coops  wrote:
snip
> In other words you are using a function inside a function. If you split this
> into two lines.
> my @split_result = split /:/;
> print $outfile join(" ", "socks5", @split_result)
snip

By that logic it should read

my @split_result = split /:/;
my $join_result  = join " ", "socks5", @split_result;
print $outfile $join_result;

or more verbosely:

my @split_result = split /:/;
unshift @split_result, "socks5";
my $join_result  = join " ", @split_result;
print $outfile $join_result;

or the ridiculously magicless:

my @split_result = split /:/, $_, 0;
unshift @split_result, "socks5";
my $join_result  = join " ", @split_result;
print $outfile $join_result;

Using the output of one function as part of the arguments for another
is a common practice, and the sooner people learn how to deal with it
the better.

What I found confusing about the code was all of the useless
parentheses.  Why does join deserve parentheses, but print and split
don't?  What is the deal with the parentheses around join; are they
intended to add a visual distinction between print's filehandle and
its argument list?  Personally, I would write it like

print $outfile join " ", "socks5", split /:/;

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2010-07-12 Thread Rob Coops
On Mon, Jul 12, 2010 at 12:54 PM, Shlomi Fish  wrote:

> On Monday 12 Jul 2010 11:07:49 newsense wrote:
> > This is what i currently have but am not sure how i can use printf
> > here instead of print so i can get some nice columns.
> >
> > #!/usr/bin/perl
> > use warnings;
> > use strict;
> >
> > open my $list, "<", "proxylist.txt" or die $!;
> > open my $outfile, ">", "test.txt" or die $!;
> >
> > while (<$list>) {
> > print $outfile (join(" ", "socks5",  split /:/ ));
> > }
>
> It depends how many columns you have and how wide each one of them should
> be.
> See the printf / sprintf tutorial at:
>
> http://perl-begin.org/tutorials/perl-for-newbies/part4/#page--sprintf--DIR
>
> Regards,
>
>Shlomi Fish
>
> --
> -
> Shlomi Fish   http://www.shlomifish.org/
> Best Introductory Programming Language - http://shlom.in/intro-lang
>
> God considered inflicting XSLT as the tenth plague of Egypt, but then
> decided against it because he thought it would be too evil.
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>
Currently you are doing something neat but also confusing for a lot of
beginning per coders, you are in the following line: print $outfile (join("
", "socks5",  split /:/ )); splitting the $_ variable (if no variable
is explicitly handed to split it operates on $_) then instantly joining the
output with the string "socks5" at the start and a " " between every value
that the split has returned.

In other words you are using a function inside a function. If you split this
into two lines.
my @split_result = split /:/;
print $outfile join(" ", "socks5", @split_result)

With a setup like that you are most likely able to workout a nice printf
statement that prints the various variables correctly. It would not be any
fun if I just tell you how to do the whole thing in the end you want to
remember how to do these sorts of things and the best way to do that is by
working it out for your self as Shlomi pointed out before the printf manual
should provide you all the information you need to work it out from here.

Rob


Re: printf

2010-07-12 Thread Shlomi Fish
On Monday 12 Jul 2010 11:07:49 newsense wrote:
> This is what i currently have but am not sure how i can use printf
> here instead of print so i can get some nice columns.
> 
> #!/usr/bin/perl
> use warnings;
> use strict;
> 
> open my $list, "<", "proxylist.txt" or die $!;
> open my $outfile, ">", "test.txt" or die $!;
> 
> while (<$list>) {
> print $outfile (join(" ", "socks5",  split /:/ ));
> }

It depends how many columns you have and how wide each one of them should be. 
See the printf / sprintf tutorial at:

http://perl-begin.org/tutorials/perl-for-newbies/part4/#page--sprintf--DIR

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2009-11-03 Thread C.DeRykus
On Oct 27, 1:52 pm, aim...@sfbrgenetics.org (Aimee Cardenas) wrote:
> Hi, All!
>
> I need to fix the width of some strings padding with leading spaces if  
> necessary.  I wanted to use printf but I don't know if you can put a  
> variable in the format part the the printf statement.  For example, if  
> I wanted to use the following format type:
>
> printf OFILE "%7s\n", $str;
>
> but instead of '7' have a variable such as $w there, how might I do  
> this?  Or is there a better function for what I'm trying to do?


my $width = 7;
my $fmt = "%${width}s";
printf $fmt, $str;

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: printf

2009-10-28 Thread Wagner, David --- Senior Programmer Analyst --- CFS
> -Original Message-
> From: Aimee Cardenas [mailto:aim...@sfbrgenetics.org] 
> Sent: Tuesday, October 27, 2009 15:53
> To: Perl Beginners
> Subject: printf
> 
> Hi, All!
> 
> I need to fix the width of some strings padding with leading 
> spaces if  
> necessary.  I wanted to use printf but I don't know if you can put a  
> variable in the format part the the printf statement.  For 
> example, if  
> I wanted to use the following format type:
> 
> printf OFILE "%7s\n", $str;
Yes you can.

printf OFILE "%*s\n",
$varlen,
$var;
 If you have any questions and/or problems, please let me know.
 Thanks.
 
Wags ;)
David R. Wagner
Senior Programmer Analyst
FedEx Freight Systems
1.719.484.2097 Tel
1.719.484.2419 Fax
1.408.623.5963 Cell
http://fedex.com/us 

> 
> but instead of '7' have a variable such as $w there, how might I do  
> this?  Or is there a better function for what I'm trying to do?
> 
> Thanks,
> 
> Aimee Cardenas
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 
> 

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2009-10-27 Thread Uri Guttman
> "AC" == Aimee Cardenas  writes:

  AC> Awesome!  Thanks, David!  :-D

you can do it is true. but you should have been able to figure it out on
your own with simple logic or checking the docs. arguments to perl's
functions are just normal values and expressions. they may be converted
to something else (coercion) but you still can pass anything to about
any function. the first arg to sprintf (and same for printf) is just a
perl scalar value which will be used as a string format. you can put a
function there that returns a string and it will work fine. you can
write any expression there you want. so an interpolate string with a
variable in it will work too. get this clear in your head and you will
improve your perl quickly. a similar issue comes up with here docs as
newbies see it used often with print. they assume the << op is actually
part of the print syntax when it really is just another way to create a
quoted string. it just happens to be used quite a bit with print but it
has no direct association with print.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf

2009-10-27 Thread Aimee Cardenas

Awesome!  Thanks, David!  :-D

Aimee Cardenas


On Oct 27, 2009, at 4:56 PM, Wagner, David --- Senior Programmer  
Analyst --- CFS wrote:



-Original Message-
From: Aimee Cardenas [mailto:aim...@sfbrgenetics.org]
Sent: Tuesday, October 27, 2009 15:53
To: Perl Beginners
Subject: printf

Hi, All!

I need to fix the width of some strings padding with leading
spaces if
necessary.  I wanted to use printf but I don't know if you can put a
variable in the format part the the printf statement.  For
example, if
I wanted to use the following format type:

printf OFILE "%7s\n", $str;

Yes you can.

printf OFILE "%*s\n",
$varlen,
$var;
If you have any questions and/or problems, please let me know.
Thanks.

Wags ;)
David R. Wagner
Senior Programmer Analyst
FedEx Freight Systems
1.719.484.2097 Tel
1.719.484.2419 Fax
1.408.623.5963 Cell
http://fedex.com/us



but instead of '7' have a variable such as $w there, how might I do
this?  Or is there a better function for what I'm trying to do?

Thanks,

Aimee Cardenas

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/






--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf with currency symbols

2009-10-26 Thread Bryan R Harris

> Robert Citek wrote:
>> Not sure if there is a better way.  My guess is that there is probably
>> some module to convert float to currency and then print it as a
>> string.  But a quick Google didn't turn up anything.
> 
> Here' why (extracted from `perldoc perllocale`):
> 
>Category LC_MONETARY: Formatting of monetary amounts
> 
>The C standard defines the "LC_MONETARY" category, but no
> function that is affected by its contents.  (Those with experience of
> standards committees will recognize that the working group decided to
> punt on the issue.)  Consequently, Perl takes no notice of it.  If you
> really want to use "LC_MONETARY", you can query its contents--see "The
> localeconv function"--and use the information that it returns in your
> application¹s own formatting of currency amounts.  However, you may well
> find that the information, voluminous and complex though it may be,
> still does not quite meet your requirements: currency formatting is a
> hard nut to crack.


That's what I needed to know -- thanks Shawn (and Jim and Robert).

- Bryan


-- 
Bryan Harris
Sr. Systems Engineer II
Huntsville Operations Analysis & System Performance
Missile Systems, Raytheon Company
b...@raytheon.com
256.542.4632




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf with currency symbols

2009-10-26 Thread Shawn H Corey
Robert Citek wrote:
> Not sure if there is a better way.  My guess is that there is probably
> some module to convert float to currency and then print it as a
> string.  But a quick Google didn't turn up anything.

Here' why (extracted from `perldoc perllocale`):

   Category LC_MONETARY: Formatting of monetary amounts

   The C standard defines the "LC_MONETARY" category, but no
function that is affected by its contents.  (Those with experience of
standards committees will recognize that the working group decided to
punt on the issue.)  Consequently, Perl takes no notice of it.  If you
really want to use "LC_MONETARY", you can query its contents--see "The
localeconv function"--and use the information that it returns in your
application’s own formatting of currency amounts.  However, you may well
find that the information, voluminous and complex though it may be,
still does not quite meet your requirements: currency formatting is a
hard nut to crack.



-- 
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf with currency symbols

2009-10-26 Thread Robert Citek
I see.  You want the output to look something like this:

$ perl -e 'for(my $total = 24.15; $total <3; $total *= 10) {
printf("Total:%10s\n", "\$" . sprintf("%.2f",$total)) ;} '
Total:$24.15
Total:   $241.50
Total:  $2415.00
Total: $24150.00

Not sure if there is a better way.  My guess is that there is probably
some module to convert float to currency and then print it as a
string.  But a quick Google didn't turn up anything.

Good luck and let us know how things go.

Regards,
- Robert

On Mon, Oct 26, 2009 at 11:57 AM, Bryan R Harris
 wrote:
> Is there a way to do this without getting all messy like this?
>
>  printf "Total:%10s\n", "\$".sprintf(%.2f,$total);

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf with currency symbols

2009-10-26 Thread Robert Citek
Is this what you are looking for:

$ perl -e '$total = 24.15 ; printf "Total: \$%.2f\n", $total; '

Regards,
- Robert

On Mon, Oct 26, 2009 at 11:57 AM, Bryan R Harris
 wrote:
> Is there a good way to do printf's with currency symbols?
>
> I've tried this:
>
>  printf "Total: \$%10.2f\n", $total;
>
> But it puts the dollar sign way out front (ugly).  I want it to look like:
>
>  Total:    $24.15
>
> Is there a way to do this without getting all messy like this?
>
>  printf "Total:%10s\n", "\$".sprintf(%.2f,$total);

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf with currency symbols

2009-10-26 Thread Jim Gibson
On 10/26/09 Mon  Oct 26, 2009  8:57 AM, "Bryan R Harris"
 scribbled:

> 
> 
> Is there a good way to do printf's with currency symbols?
> 
> I've tried this:
> 
>   printf "Total: \$%10.2f\n", $total;
> 
> But it puts the dollar sign way out front (ugly).  I want it to look like:
> 
>   Total:$24.15

You can add a minus sign to the format descriptor to left-justify the field:

%-10.2f

However, if you do this and print more than one line, the decimal points may
not line up.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf and zero padding

2009-10-24 Thread Harry Putnam
Jim Gibson  writes:

> At 4:02 PM -0500 10/24/09, Harry Putnam wrote:
>>With this little script, how would I manage to get the shorter
>>timestamps zero padded using printf?  I now how to get padded numbers
>>but not when I'm pushing off the right margin too.
>>
>>cat script.pl
>>
>>   #!/usr/local/bin/perl
>>   use strict;
>>   use warnings;
>>
>>   while (my $file = shift @ARGV){
>> my @stat = stat $file;
>> printf "%11d %s\n",$stat[9],  $file;
>>   }
>
> I am not clear on what you are asking. You would use the format
> conversion '%011d' to zero-pad a number to fill 11 characters, but you
> say you know that already. Can you give an example of your desired
> output?

Egad... I really intended to include a few lines of ouput.. I didn't
even notice I hadn't... sorry.

As it is I get this:

  stat.pl `ls`

[...]
 1232649333 man.pl
  994039516 mms.perl
 1227284469 modulo.pl
  994039516 n2mbox.pl
 1207227459 next_unless.pl
[...]

You see some of the files have been modified long enough ago that the
epochal time is enough earlier to be one digit shorter.

I wanted to see:

 1232649333 man.pl
 0994039516 mms.perl
 1227284469 modulo.pl
 0994039516 n2mbox.pl
 1207227459 next_unless.pl

And in the course of explaining it, as happens to me pretty often.. I
see my mistake... Somehow I'd gotten it into my head that since it
was 1 digit short I needed to pad 1 zero...
 
Well... thats' true.. but it needs to come at the start of an 11
character parking place so anyway, thanks for making me see
the error.

I seem to forget about 90% of what I know between scripts.


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf and zero padding

2009-10-24 Thread Jim Gibson

At 4:02 PM -0500 10/24/09, Harry Putnam wrote:

With this little script, how would I manage to get the shorter
timestamps zero padded using printf?  I now how to get padded numbers
but not when I'm pushing off the right margin too.

cat script.pl

  #!/usr/local/bin/perl
  use strict;
  use warnings;

  while (my $file = shift @ARGV){
my @stat = stat $file;
printf "%11d %s\n",$stat[9],  $file;
  }


I am not clear on what you are asking. You would use the format 
conversion '%011d' to zero-pad a number to fill 11 characters, but 
you say you know that already. Can you give an example of your 
desired output?



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: printf problem

2007-10-30 Thread Paul Lalli
On Oct 29, 8:22 pm, [EMAIL PROTECTED] (Julie A. Bolduc) wrote:
> I am trying to format some numbers so single digit numbers are converted to
> 2 digit numbers and here is the code I am working with right now.  For some
> reason, the very last number, $b5, ends up as a 3 digit number instead of a
> 2 digit number even if I start it with a single digit number.  If I add
> chop($bcol); to the code, it cuts the last digit off but what if I do not
> need to have that happen?  I am not exactly new to perl but this has me
> stumped.
>
> print "Content-type: text/html\n\n";
> $b1=11;
> $b2=2;
> $b3=5;
> $b4=1;
> $b5=12;
> $bcol=printf("%02d %02d %02d %02d %02d", $b1, $b2, $b3, $b4, $b5);
>
> #chop($bcol);
> print qq($bcol);
>
> Can anyone help?

printf() prints the string to the screen, and returns a true value (1)
if successful.
sprintf() returns the string

You are printing to the screen and storing the result (the 1) in
$bcol, and then printing $bcol.  Make up your mind.  Either change the
printf() to sprintf() or eliminate $bcol entirely.

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: printf problem

2007-10-30 Thread Andrew Curry
try using sprintf instead of printf.
i think the extra 1 you are getting on the end it just a true return.

-Original Message-
From: Julie A. Bolduc [mailto:[EMAIL PROTECTED]
Sent: 30 October 2007 00:23
To: beginners@perl.org
Subject: printf problem


I am trying to format some numbers so single digit numbers are converted to 
2 digit numbers and here is the code I am working with right now.  For some 
reason, the very last number, $b5, ends up as a 3 digit number instead of a 
2 digit number even if I start it with a single digit number.  If I add 
chop($bcol); to the code, it cuts the last digit off but what if I do not 
need to have that happen?  I am not exactly new to perl but this has me 
stumped.


print "Content-type: text/html\n\n";
$b1=11;
$b2=2;
$b3=5;
$b4=1;
$b5=12;
$bcol=printf("%02d %02d %02d %02d %02d", $b1, $b2, $b3, $b4, $b5);

#chop($bcol);
print qq($bcol);

Can anyone help?


Julie A. Bolduc
Owner, JPF Crochet Club
http://www.jpfun.com 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/



This e-mail is from the PA Group.  For more information, see
www.thepagroup.com.

This e-mail may contain confidential information.  Only the addressee is
permitted to read, copy, distribute or otherwise use this email or any
attachments.  If you have received it in error, please contact the sender
immediately.  Any opinion expressed in this e-mail is personal to the sender
and may not reflect the opinion of the PA Group.

Any e-mail reply to this address may be subject to interception or
monitoring for operational reasons or for lawful business practices.





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: printf

2007-01-18 Thread Dermot Paikkos
On 18 Jan 2007 at 16:25, Eugene Kosov wrote:

> Beginner wrote:
> > Hi all,
> > 
> > Sorry I am sure this is a lame question. 
> > 
> > I want to print out some column (with heading) and I want them
> > evenly spaced. I know this is a printf but the format eludes me.
> > 
> > printf("%c12", $var);  # prints $var12$var12
> 
> Try %12c (or %12s) instead of %c12 above.


Just the ticket, Thanx.
Dp.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: printf

2007-01-18 Thread Beginner
On 18 Jan 2007 at 7:19, Hal Wigoda wrote:

> for one thing, you need to add "\n" for newline.


> > printf("%c12", $var);  # prints $var12$var12
> >
> > %s seems to give me no output at all.
> >

I wouldn't want a newline in the middle of my column heading. I would 
like a 12 character spacing between $var 

so 

foreach (my $var) {
printf("%c.12", $var); # Does work.
}
print "\n";


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: printf

2007-01-18 Thread Eugene Kosov

Beginner wrote:

Hi all,

Sorry I am sure this is a lame question. 

I want to print out some column (with heading) and I want them evenly 
spaced. I know this is a printf but the format eludes me.


printf("%c12", $var);  # prints $var12$var12


Try %12c (or %12s) instead of %c12 above.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: printf

2007-01-18 Thread Hal Wigoda

for one thing, you need to add "\n" for newline.


On Jan 18, 2007, at 7:12 AM, Beginner wrote:


Hi all,

Sorry I am sure this is a lame question.

I want to print out some column (with heading) and I want them evenly
spaced. I know this is a printf but the format eludes me.

printf("%c12", $var);  # prints $var12$var12

%s seems to give me no output at all.

Any ideas?
TIA,
Dp.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: printf sprintf

2004-01-09 Thread david
R. Joseph Newton wrote:
 
> All the use one could imagine.  The printf function can print pretty much
> anywhere.  I believe that it preceded sprintf historically, but I wouldn't
> swear
>
> Many C coders have told me that it is the most efficient way to
> write files.

huh? please explain why printf is the most efficient way to write files?

david
-- 
sub'_{print"@_ ";* \ = * __ ,\ & \}
sub'__{print"@_ ";* \ = * ___ ,\ & \}
sub'___{print"@_ ";* \ = *  ,\ & \}
sub'{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: printf sprintf

2004-01-08 Thread R. Joseph Newton
Paul Kraus wrote:

> What is the difference. The only I see is that printf can take a filehandle?
> But what use would that be.

All the use one could imagine.  The printf function can print pretty much
anywhere.  I believe that it preceded sprintf historically, but I wouldn't swear
to it.  Many C coders have told me that it is the most efficient way to write
files.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: printf sprintf

2004-01-08 Thread Wiggins d Anconia


> Wiggins D Anconia wrote:
> >
> > Notice the difference in the docs:
> >
> > printf FILEHANDLE FORMAT, LIST
> > printf FORMAT, LIST
> >
> > In the first there is NO comma following the filehandle, this means it
> > is interpreted in a different manner than the rest of the argument list,
> > or probably to be more precise isn't an argument at all... I am sure one
> > of the gurus will chime in with the actual technical name of what this
> > spot is actually called in this context since I either don't know or it
> > escapes me currently.
> 
> Hi Wiggins.
> 
> The first instance is using 'printf' as a method call:
> 
>   printf STDOUT (FORMAT, LIST)
> 
> is the same as
> 
>   STDOUT->printf(FORMAT, LIST)
> 
> In the second case 'printf' is just a list operator.
> 
> HTH,
> 
> Rob
> 

Ah, that makes sense, like 'new' etc.  As a file handle is just a
special type of "object" anyways...

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: printf sprintf

2004-01-08 Thread Rob Dixon
Wiggins D Anconia wrote:
>
> Notice the difference in the docs:
>
> printf FILEHANDLE FORMAT, LIST
> printf FORMAT, LIST
>
> In the first there is NO comma following the filehandle, this means it
> is interpreted in a different manner than the rest of the argument list,
> or probably to be more precise isn't an argument at all... I am sure one
> of the gurus will chime in with the actual technical name of what this
> spot is actually called in this context since I either don't know or it
> escapes me currently.

Hi Wiggins.

The first instance is using 'printf' as a method call:

  printf STDOUT (FORMAT, LIST)

is the same as

  STDOUT->printf(FORMAT, LIST)

In the second case 'printf' is just a list operator.

HTH,
Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: printf sprintf

2004-01-08 Thread Rob Dixon
Wiggins D Anconia wrote:
>
> Notice the difference in the docs:
>
> printf FILEHANDLE FORMAT, LIST
> printf FORMAT, LIST
>
> In the first there is NO comma following the filehandle, this means it
> is interpreted in a different manner than the rest of the argument list,
> or probably to be more precise isn't an argument at all... I am sure one
> of the gurus will chime in with the actual technical name of what this
> spot is actually called in this context since I either don't know or it
> escapes me currently.

Hi Wiggins.

The first instance is using 'printf' as a method call:

  printf STDOUT (FORMAT, LIST)

is the same as

  STDOUT->printf(FORMAT, LIST)

In the second case 'printf' is just a list operator.

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: printf sprintf

2004-01-08 Thread Dan Muey

> > > What is the difference. The only I see is that printf can
> > 
> > One difference is printf prints it's output and sprintf 
> returns it';s
> value.
> > 
> > printf ...
> > my $formatted_goodies = sprintf ...
> > 
> > > take a filehandle? But what use would that be.
> > > 
> > 
> > To format the contents of it. For instance, you might have a user
> enter a dollar amount from the command line.
> > If you could printf STDIN the you could make sure 
> 123.4567890 came out
> as $123.46
> > 
> > Just one quick idea..
> > 
> > DMuey
> > 
> > >  Paul Kraus
> 
> Though you could try it, I did not, I don't think you can 
> printf STDIN since it is an inbound IO pipe as opposed to 
> outbound.  This is a good demonstation of the all important 
> comma operator.  Notice the difference in the docs:
> 
> printf FILEHANDLE FORMAT, LIST
> printf FORMAT, LIST
> 
> In the first there is NO comma following the filehandle, this 
> means it is interpreted in a different manner than the rest 

Oh right! I should have read that first before posting. Ooopss.

Sorry everyone!

> of the argument list, or probably to be more precise isn't an 
> argument at all... I am sure one of the gurus will chime in 
> with the actual technical name of what this spot is actually 
> called in this context since I either don't know or it 
> escapes me currently.
> 
http://danconia.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: printf sprintf

2004-01-08 Thread Wiggins d Anconia


> > What is the difference. The only I see is that printf can 
> 
> One difference is printf prints it's output and sprintf returns it';s
value.
> 
> printf ...
> my $formatted_goodies = sprintf ...
> 
> > take a filehandle? But what use would that be. 
> > 
> 
> To format the contents of it. For instance, you might have a user
enter a dollar amount from the command line.
> If you could printf STDIN the you could make sure 123.4567890 came out
as $123.46
> 
> Just one quick idea..
> 
> DMuey
> 
> >  Paul Kraus

Though you could try it, I did not, I don't think you can printf STDIN
since it is an inbound IO pipe as opposed to outbound.  This is a good
demonstation of the all important comma operator.  Notice the difference
in the docs:

printf FILEHANDLE FORMAT, LIST
printf FORMAT, LIST

In the first there is NO comma following the filehandle, this means it
is interpreted in a different manner than the rest of the argument list,
or probably to be more precise isn't an argument at all... I am sure one
of the gurus will chime in with the actual technical name of what this
spot is actually called in this context since I either don't know or it
escapes me currently.

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: printf sprintf

2004-01-08 Thread Wiggins d Anconia


> 
> What is the difference. The only I see is that printf can take a
filehandle?
> But what use would that be. 
> 

The filehandle tells printf where to print the result, aka which
filehandle, by default STDOUT.  Which doesn't have a purpose with
sprintf since it returns its value...

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: printf sprintf

2004-01-08 Thread Dan Muey
> What is the difference. The only I see is that printf can 

One difference is printf prints it's output and sprintf returns it';s value.

printf ...
my $formatted_goodies = sprintf ...

> take a filehandle? But what use would that be. 
> 

To format the contents of it. For instance, you might have a user enter a dollar 
amount from the command line.
If you could printf STDIN the you could make sure 123.4567890 came out as $123.46

Just one quick idea..

DMuey

>  Paul Kraus

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: printf

2003-12-22 Thread Steve Grazzini
On Mon, Dec 22, 2003 at 11:17:03AM -0800, Perl wrote:
> i know what " %9.1f" would have done in this case.
> but how does " %9.2g" round off ?

The *rounding* works like "%f", but there are some other
differences.

  a) the precision (".2") applies to significant digits, not
 digits after the decimal point

 % perl -e 'printf "%.2g\n", 2.25'
 2.2

  b) trailing zeros will be suppressed

 % perl -e 'printf "%.2g\n", 2.05'
 2

  c) the output will be given in scientific notation if there
 are more digits to the left of the decimal point than the
 specified number of significant digits

 % perl -e 'printf "%.2g\n", 225'
 2.2e+02

Perl doesn't actually implement %g itself, so check your
system's sprintf(3) or gconvert/gcvt manpages for a more
precise description.

And incidentally, when you do:

 % perl -le 'print 100/3'
 33.3

Perl uses "%g" (or gcvt()) to format the floating point value:

 % perl -le 'print sprintf "%.15g", 100/3'
 33.3

-- 
Steve

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: printf

2003-12-22 Thread wolf blaum
Am Montag, 22. Dezember 2003 20:17 schrieb Perl:

> i know what " %9.1f" would have done in this case.
> but how does " %9.2g" round off ?

the FORMAT notation used by prinf, sprintf, ... can be found on 
http://www.perldoc.com/perl5.8.0/pod/func/sprintf.html

which gives:
%e  a floating-point number, in scientific notation
%f  a floating-point number, in fixed decimal notation
%g  a floating-point number, in %e or %f notation

Just in case: scientific notation is something like XeY which means X times 10 
to the poxer of  Y (power first!)

HTH, wolf


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: printf

2003-12-22 Thread wolf blaum
Am Montag, 22. Dezember 2003 20:17 schrieb Perl:

> i know what " %9.1f" would have done in this case.
> but how does " %9.2g" round off ?

the FORMAT notation used by prinf, sprintf, ... can be found on 
http://www.perldoc.com/perl5.8.0/pod/func/sprintf.html

which gives:
%e  a floating-point number, in scientific notation
%f  a floating-point number, in fixed decimal notation
%g  a floating-point number, in %e or %f notation

Just in case: scientific notation is something like XeY which means X times 10 
to the poxer of  Y (power first!)

HTH, wolf


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: printf? RE: help building hash on-the-fly?

2003-11-05 Thread Wagner, David --- Senior Programmer Analyst --- WGO
McMahon, Chris wrote:
> Hi Wags...
>   So now an idle style question, if you don't mind...
> 
>   This syntax seems pretty obscure to me (I had to look up what printf
> was doing):
> 
> printf "%3d $file\n", $MyId++;
> 
>   It's efficient and all, but isn't this more readable for the same
> number of characters?
> 
> print "$MyId $file\n";
> $MyId++;
> 
>   I just wondered why you like printf in this circumstance.
> -Chris
I like things to look right and if you have more than 9 items, then you don't 
line up like I would like it.  I very seldom use just print and almost always use 
printf to keep things lined up as I would like them. It boils down to what you feel 
comfortable with.

Wags ;)

> 
> -Original Message-
> From: Wagner, David --- Senior Programmer Analyst --- WGO
> [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, November 05, 2003 3:36 PM
> To: McMahon, Chris; [EMAIL PROTECTED]
> Subject: RE: help building hash on-the-fly?
> 
> McMahon, Chris wrote:
>> Hello...
>> This is probably a very simple question, but I don't have
>> much experience using hashes...
>> I have a simple program that lists all of the files on a
>> directory: 
>> 
>> @files = ;
>> foreach $file (@files) {
>> print "$file\n";
>> }
>> 
>> which prints something like this:
>> /dir/file1
>> /dir/file2
>> /dir/file3
>> etc.
>> 
>> But an array doesn't suit my needs.  What I really need
>   You could but why. As part of your print you could do somehting
> like:
>  my $MyId = 1;
>  foreach $file (@files) {
>  printf "%3d $file\n", $MyId++;
>  }
> Which should print out just as you want.
> 
> Now the individual can enter say 3, and you can put from @files by
> subtracting 1 and you would have the file.  Using scalar(@files) you
> now have your Max files within your array, so if they enter 6. You
> subtract 1 and get 5 but scalar(@files) returns 4, you know they
> entered an invalid number.
> 
> Just a thought.
> Wags ;)
> 
> 
> **
> This message contains information that is confidential
> and proprietary to FedEx Freight or its affiliates.
> It is intended only for the recipient named and for
> the express purpose(s) described therein.
> Any other use is prohibited.
> 


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: printf? RE: help building hash on-the-fly?

2003-11-05 Thread Andrew Gaffney
Well, atleast I was right about the padding :)

LoBue, Mark wrote:
-Original Message-
From: Andrew Gaffney [mailto:[EMAIL PROTECTED]
Sent: Wednesday, November 05, 2003 3:21 PM
To: [EMAIL PROTECTED]
Subject: Re: printf? RE: help building hash on-the-fly?
I'm going to venture a guess here. Its probably because he 
needed the number printed as a 
3-digit number padded with zeroes, which print can't do.


To pad with zeroes, use %03d.  %3d pads with spaces.
--
Andrew Gaffney
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


RE: printf? RE: help building hash on-the-fly?

2003-11-05 Thread LoBue, Mark
> -Original Message-
> From: Andrew Gaffney [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, November 05, 2003 3:21 PM
> To: [EMAIL PROTECTED]
> Subject: Re: printf? RE: help building hash on-the-fly?
> 
> 
> I'm going to venture a guess here. Its probably because he 
> needed the number printed as a 
> 3-digit number padded with zeroes, which print can't do.

To pad with zeroes, use %03d.  %3d pads with spaces.

-Mark

> 
> McMahon, Chris wrote:
> > Hi Wags...
> > So now an idle style question, if you don't mind...
> > 
> > This syntax seems pretty obscure to me (I had to look 
> up what printf
> > was doing): 
> > 
> > printf "%3d $file\n", $MyId++;
> > 
> > It's efficient and all, but isn't this more readable 
> for the same
> > number of characters? 
> > 
> > print "$MyId $file\n"; 
> > $MyId++; 
> > 
> > I just wondered why you like printf in this circumstance. 
> 
> -- 
> Andrew Gaffney
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: printf? RE: help building hash on-the-fly?

2003-11-05 Thread Andrew Gaffney
I'm going to venture a guess here. Its probably because he needed the number printed as a 
3-digit number padded with zeroes, which print can't do.

McMahon, Chris wrote:
Hi Wags...
So now an idle style question, if you don't mind...
	This syntax seems pretty obscure to me (I had to look up what printf
was doing): 

printf "%3d $file\n", $MyId++;

	It's efficient and all, but isn't this more readable for the same
number of characters? 

print "$MyId $file\n"; 
$MyId++; 

	I just wondered why you like printf in this circumstance. 
--
Andrew Gaffney
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: printf question

2002-07-01 Thread Jeff 'japhy' Pinyan

On Jul 1, Jeff 'japhy' Pinyan said:

>On Jul 1, Ned Cunningham said:
>
>>Can anyone tell me how to print the following data so that it rounds up
>>even though it shouldn't
>
>Use the POSIX::ceil() function, instead of some crufty solution.
>
>  use POSIX 'ceil';
>  printf WRFILE "%11.2f", ceil($data);

My apologies, I didn't see you wanted the rounding to occur at a certain
decimal place.  Use something like

  ceil($data * 100) / 100

instead of just ceil($data).

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: printf question

2002-07-01 Thread Jeff 'japhy' Pinyan

On Jul 1, Ned Cunningham said:

>Can anyone tell me how to print the following data so that it rounds up
>even though it shouldn't

Use the POSIX::ceil() function, instead of some crufty solution.

  use POSIX 'ceil';
  printf WRFILE "%11.2f", ceil($data);

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf question

2002-07-01 Thread Timothy Johnson


You could always try adding .999 to the value before sprintf()ing it...

-Original Message-
From: Ned Cunningham [mailto:[EMAIL PROTECTED]]
Sent: Monday, July 01, 2002 11:32 AM
To: '[EMAIL PROTECTED]'
Subject: printf question


Hi,

Can anyone tell me how to print the following data so that it rounds up even
though it shouldn't


$data = "3.424";

Printf WRFILE ("%11.2f", $data);

My results are 

3.42

I need it to round up even if it is only .001 ?

TIA

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: printf question

2002-07-01 Thread Felix Geerinckx

on Mon, 01 Jul 2002 18:32:27 GMT, Ned Cunningham wrote:

> Can anyone tell me how to print the following data so that it rounds
> up even though it shouldn't
> $data = "3.424";
> [...]
> I need it to round up even if it is only .001 ?



use strict;
use POSIX qw(ceil);

sub roundup {
my $number = shift;
my $decimals = shift;
return undef unless defined($number) 
&& $number > 0 && $decimals >= 0;
return sprintf("%.${decimals}f", 
   ceil($number*10**$decimals)/10**$decimals);
}

print roundup(3.424,2), "\n"; # prints 3.43
print roundup(3.401,2), "\n"; # prints 3.41
print roundup(3.391,2), "\n"; # prints 3.40


-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf question

2002-07-01 Thread Ned Cunningham

The first one worked great thankyou

-Original Message-
From:   Nikola Janceski
[mailto:[EMAIL PROTECTED]]
Sent:   Monday, July 01, 2002 3:10 PM
To: Nikola Janceski; 'Ned Cunningham';
'[EMAIL PROTECTED]'
    Subject:RE: printf question

correction on my 4th line.

$data = sprintf ("%11.2f", $data + (scalar($data =~
/[1-9]$/) && 0.005) );

> -Original Message-
> From: Nikola Janceski
[mailto:[EMAIL PROTECTED]]
> Sent: Monday, July 01, 2002 2:55 PM
> To: 'Ned Cunningham'; '[EMAIL PROTECTED]'
> Subject: RE: printf question
> 
> 
> I don't know what accuracy you need.. but:
> 
> #my $data = "3.420";
> my $data = "3.424";
> $data = sprintf ("%11.3f", $data);
> $data = sprintf ("%11.2f", $data + scalar($data =~
/[1-9]$/) / 100 );
> 
> printf ("%11.2f\n", $data);
> 
> > -Original Message-
> > From: Ned Cunningham [mailto:[EMAIL PROTECTED]]
> > Sent: Monday, July 01, 2002 2:32 PM
> > To: '[EMAIL PROTECTED]'
> > Subject: printf question
> > 
> > 
> > Hi,
> > 
> > Can anyone tell me how to print the following data so
that it 
> > rounds up even
> > though it shouldn't
> > 
> > 
> > $data = "3.424";
> > 
> > Printf WRFILE ("%11.2f", $data);
> > 
> > My results are 
> > 
> > 3.42
> > 
> > I need it to round up even if it is only .001 ?
> > 
> > TIA
> > 
> > -- 
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > 
> 
>
--
> --
> 
> The views and opinions expressed in this email message are

> the sender's
> own, and do not necessarily represent the views and
opinions of Summit
> Systems Inc.
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 




The views and opinions expressed in this email message are
the sender's
own, and do not necessarily represent the views and opinions
of Summit
Systems Inc.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf question

2002-07-01 Thread Nikola Janceski

correction on my 4th line.

$data = sprintf ("%11.2f", $data + (scalar($data =~ /[1-9]$/) && 0.005) );

> -Original Message-
> From: Nikola Janceski [mailto:[EMAIL PROTECTED]]
> Sent: Monday, July 01, 2002 2:55 PM
> To: 'Ned Cunningham'; '[EMAIL PROTECTED]'
> Subject: RE: printf question
> 
> 
> I don't know what accuracy you need.. but:
> 
> #my $data = "3.420";
> my $data = "3.424";
> $data = sprintf ("%11.3f", $data);
> $data = sprintf ("%11.2f", $data + scalar($data =~ /[1-9]$/) / 100 );
> 
> printf ("%11.2f\n", $data);
> 
> > -Original Message-
> > From: Ned Cunningham [mailto:[EMAIL PROTECTED]]
> > Sent: Monday, July 01, 2002 2:32 PM
> > To: '[EMAIL PROTECTED]'
> > Subject: printf question
> > 
> > 
> > Hi,
> > 
> > Can anyone tell me how to print the following data so that it 
> > rounds up even
> > though it shouldn't
> > 
> > 
> > $data = "3.424";
> > 
> > Printf WRFILE ("%11.2f", $data);
> > 
> > My results are 
> > 
> > 3.42
> > 
> > I need it to round up even if it is only .001 ?
> > 
> > TIA
> > 
> > -- 
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > 
> 
> --
> --
> 
> The views and opinions expressed in this email message are 
> the sender's
> own, and do not necessarily represent the views and opinions of Summit
> Systems Inc.
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 



The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf question

2002-07-01 Thread Nikola Janceski

I don't know what accuracy you need.. but:

#my $data = "3.420";
my $data = "3.424";
$data = sprintf ("%11.3f", $data);
$data = sprintf ("%11.2f", $data + scalar($data =~ /[1-9]$/) / 100 );

printf ("%11.2f\n", $data);

> -Original Message-
> From: Ned Cunningham [mailto:[EMAIL PROTECTED]]
> Sent: Monday, July 01, 2002 2:32 PM
> To: '[EMAIL PROTECTED]'
> Subject: printf question
> 
> 
> Hi,
> 
> Can anyone tell me how to print the following data so that it 
> rounds up even
> though it shouldn't
> 
> 
> $data = "3.424";
> 
> Printf WRFILE ("%11.2f", $data);
> 
> My results are 
> 
> 3.42
> 
> I need it to round up even if it is only .001 ?
> 
> TIA
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 



The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: "printf" behaving oddly

2002-06-10 Thread Todd Wade


"Adrian Farrell" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
>
> $answer = printf ("\nThe area of the circle with a radius of $radius is
> %2.2f\n", ($area));
>
> print "$answer"

If you want to store the string in a var, use sprintf. printf returns wether
or not the print was sucessful, hence the 1 stored in $answer.

Todd W.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: "printf" behaving oddly

2002-06-10 Thread Chris Ball

> "Adrian" == Adrian Farrell <[EMAIL PROTECTED]> writes:

Adrian> The problem is that $answer returns a value of 1 and not the
Adrian> contents of the printf. I guess I've assigned it
Adrian> incorrectly. the other odd thing is that the -- print
Adrian> "$answer" -- line appears not t be neccesary as the
Adrian> preceding line prints to the screen anyway.

These aren't unrelated.  :-)  printf() writes to the screen, just as
print() does.  Capturing _the return code_ into a variable doesn't
change that; that's what your '1' is - a success code.

Your problems can be easily solved, though.  sprintf() returns the
string that printf() would have printed.  You can just replace the
printf() call with one to sprintf(), and all should work fine.  :)

Hope this helps,

- Chris.
-- 
$a="printf.net"; Chris Ball | chris@void.$a | www.$a | finger: chris@$a
As to luck, there's the old miners' proverb: Gold is where you find it.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf and archive questions

2002-03-15 Thread Hanson, Robert

I don't know of a searchable archive.

To format the number you can either roll your own, grab the solution in the
Perl Cookbook, or use Number::Format.

http://search.cpan.org/search?mode=module&query=Number%3A%3AFormat

use Number::Format;
my $commaNum = new Number::Format(-thousands_sep   => '.');
my $number = $commaNum->format_number(100);

Or roll your own (this from the Perl Cookbook):

my $number = commify(100);

sub commify {
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text;
}


Rob


-Original Message-
From: Brian Warn [mailto:[EMAIL PROTECTED]]
Sent: Friday, March 15, 2002 12:46 PM
To: [EMAIL PROTECTED]
Subject: printf and archive questions


Hi, two questions:  is there an easy way -- using printf, etc. -- to
format 100 such that it appears as 1,000,000?  Also, is there a
searchable archive of the beginner list anywhere?

Thanks, Brian


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: printf and archive questions

2002-03-15 Thread Jeff 'japhy' Pinyan

On Mar 15, Brian Warn said:

>Hi, two questions:  is there an easy way -- using printf, etc. -- to
>format 100 such that it appears as 1,000,000?  Also, is there a
>searchable archive of the beginner list anywhere?

As per commas, the Perl FAQ has the answer:

  perldoc -q commas

As for the archive, I'm not aware of a search utility.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf using a variable for the field size

2002-01-30 Thread McCollum, Frank

try ...

if ( ! $size ) { $size = 8 }

printf " %${size}s ", $yourVariableHere;


?frank

-Original Message-
From: Nikola Janceski [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, January 30, 2002 4:21 PM
To: '[EMAIL PROTECTED]'
Subject: printf using a variable for the field size


I am trying to make a 'pretty' text table printout.

I have variable that holds the size of the width of the column, so how do I
make it print the string the way I want it.

printf " %8s ", $_; ## works because I put the 8 in the code

but what if the variable $size has the size, how then do I use printf using
$size in place of the 8?


Nikola Janceski
Summit Systems, Inc.
212-896-3400

Make everything as simple as possible, but not simpler.  
-- Albert Einstein (1879-1955) 




The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


_ 
IMPORTANT NOTICES: 
  This message is intended only for the addressee. Please notify the
sender by e-mail if you are not the intended recipient. If you are not the
intended recipient, you may not copy, disclose, or distribute this message
or its contents to any other person and any such actions may be unlawful.

 Banc of America Securities LLC("BAS") does not accept time
sensitive, action-oriented messages or transaction orders, including orders
to purchase or sell securities, via e-mail.

 BAS reserves the right to monitor and review the content of all
messages sent to or from this e-mail address. Messages sent to or from this
e-mail address may be stored on the BAS e-mail system.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: printf using a variable for the field size

2002-01-30 Thread Jeff 'japhy' Pinyan

On Jan 30, Nikola Janceski said:

>printf " %8s ", $_; ## works because I put the 8 in the code
>
>but what if the variable $size has the size, how then do I use printf using
>$size in place of the 8?

Two ways come to mind:

  printf " %${size}s ", $_;

and

  printf " %*s ", $size, $_;

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf to convert 200010809 to 2001-08-09

2001-09-10 Thread Brett W. McCoy

On Mon, 10 Sep 2001, Derrick (Thrawn01) wrote:

> Now that I think about it it could be done like this
>
> @tary = unpack "a4a2a2", @data[0]->[17];
> $last_open = "$tary[0]-$tary[1]-$tary[2]";
>
> But wich would be faster ? would unpack be faster than the exp engine?
>
> Anyone's thoughts ?

I betcha unpack is faster!  I did a benchmark like this a while back, and
unpack was slightly faster than a regex.  When you bring a printf into the
equation, it's going to be less efficient also.

-- Brett
  http://www.chapelperilous.net/

Prunes give you a run for your money.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: printf to convert 200010809 to 2001-08-09

2001-09-10 Thread Randal L. Schwartz

> "Derrick" == Derrick  <[EMAIL PROTECTED]> writes:

Derrick> Now that I think about it it could be done like this
Derrick> @tary = unpack "a4a2a2", @data[0]->[17];

Can we please stop using the illegal syntax there?

$data[0][17] or $data[0]->[17] or ${$data[0]}[17], but not @data[0]->...

That's illegal syntax, that unfortunately is not YET caught by the
compiler.  Well, it's actually legal but broken, and it works badly
enough for people to use it for what they *think* it means. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf to convert 200010809 to 2001-08-09

2001-09-10 Thread Derrick (Thrawn01)

Now that I think about it it could be done like this

@tary = unpack "a4a2a2", @data[0]->[17];
$last_open = "$tary[0]-$tary[1]-$tary[2]";

But wich would be faster ? would unpack be faster than the exp engine?

Anyone's thoughts ?

-Original Message-
From: Brett W. McCoy [mailto:[EMAIL PROTECTED]]
Sent: Monday, September 10, 2001 11:03 AM
To: Derrick (Thrawn01)
Cc: Perl Beginners
Subject: Re: printf to convert 200010809 to 2001-08-09


On Mon, 10 Sep 2001, Derrick (Thrawn01) wrote:

> @data[0]->[17] contains "20010809"
>
> I've been tring to use printf to convert the 200010809 value out as
> 2001-09-08
>
> $last_open = sprintf "%4d-%2d-%2d",@data[0]->[17];
>
> However this does not work. I get "20010809- 0- 0"
> Any sugesstions on how I should make this converstion with out adding alot
> of overhead doing it ?

Use a regular expression.  printf isn't useable for what you are trying to
do:

my $data = 20010809;

$data =~ /(\d{1,4})(\d{1,2})(\d{1,2})/;

print "$1-$2-$3\n";

This prints:

2001-08-09

-- Brett
  http://www.chapelperilous.net/

All articles that coruscate with resplendence are not truly auriferous.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf to convert 200010809 to 2001-08-09

2001-09-10 Thread Tim Noll

Either of these will work -- I'll leave it up to the experts to let us know
which one (if any) is faster or more efficient:

$last_open = sprintf '%04d-%02d-%02d', $data[0]->[17] =~
/^(\d{4})(\d{2})(\d{2})$/;

$last_open = sprintf '%04d-%02d-%02d', unpack 'A4A2A2', $data[0]->[17];

Please note that you should be using $ instead of @ to refer to a scalar
value, and that you should use %02d as the format for a 2-digit number if
you want leading zeroes.


-Original Message-
From: Derrick (Thrawn01) [mailto:[EMAIL PROTECTED]]
Sent: Monday, September 10, 2001 3:57 PM
To: Perl Beginners
Subject: printf to convert 200010809 to 2001-08-09


@data[0]->[17] contains "20010809"

I've been tring to use printf to convert the 200010809 value out as
2001-09-08

$last_open = sprintf "%4d-%2d-%2d",@data[0]->[17];

However this does not work. I get "20010809- 0- 0"
Any sugesstions on how I should make this converstion with out adding alot
of overhead doing it ?



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf to convert 200010809 to 2001-08-09

2001-09-10 Thread Derrick (Thrawn01)

Thanks for the help, I think this will work out.

oh. Unusual site you have there.


-Original Message-
From: Brett W. McCoy [mailto:[EMAIL PROTECTED]]
Sent: Monday, September 10, 2001 11:03 AM
To: Derrick (Thrawn01)
Cc: Perl Beginners
Subject: Re: printf to convert 200010809 to 2001-08-09


On Mon, 10 Sep 2001, Derrick (Thrawn01) wrote:

> @data[0]->[17] contains "20010809"
>
> I've been tring to use printf to convert the 200010809 value out as
> 2001-09-08
>
> $last_open = sprintf "%4d-%2d-%2d",@data[0]->[17];
>
> However this does not work. I get "20010809- 0- 0"
> Any sugesstions on how I should make this converstion with out adding alot
> of overhead doing it ?

Use a regular expression.  printf isn't useable for what you are trying to
do:

my $data = 20010809;

$data =~ /(\d{1,4})(\d{1,2})(\d{1,2})/;

print "$1-$2-$3\n";

This prints:

2001-08-09

-- Brett
  http://www.chapelperilous.net/

All articles that coruscate with resplendence are not truly auriferous.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: printf to convert 200010809 to 2001-08-09

2001-09-10 Thread Andrea Holstein

Derrick schrieb:
> 
> @data[0]->[17] contains "20010809"
> 
> I've been tring to use printf to convert the 200010809 value out as
> 2001-09-08
> 
> $last_open = sprintf "%4d-%2d-%2d",@data[0]->[17];
> 
> However this does not work. I get "20010809- 0- 0"
> Any sugesstions on how I should make this converstion with out adding alot
> of overhead doing it ?

sprintf wants to print 3 numbers %4d, %2d and %2d.
sprintf expects them as 3 (!!) arguments, like sprintf
"%4d-%2d-%2d",2001,08,09;
but it receives something like sprintf "...",20010809,undef,undef.

You should use a RE to get the three different numbers:

sprintf "%4d-%2d-%2d", @data[0]-[17] =~ / (\d\d\d\d) (\d\d) (\d\d) /x;

Best Wishes,
Andrea

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: printf to convert 200010809 to 2001-08-09

2001-09-10 Thread Brett W. McCoy

On Mon, 10 Sep 2001, Derrick (Thrawn01) wrote:

> @data[0]->[17] contains "20010809"
>
> I've been tring to use printf to convert the 200010809 value out as
> 2001-09-08
>
> $last_open = sprintf "%4d-%2d-%2d",@data[0]->[17];
>
> However this does not work. I get "20010809- 0- 0"
> Any sugesstions on how I should make this converstion with out adding alot
> of overhead doing it ?

Use a regular expression.  printf isn't useable for what you are trying to
do:

my $data = 20010809;

$data =~ /(\d{1,4})(\d{1,2})(\d{1,2})/;

print "$1-$2-$3\n";

This prints:

2001-08-09

-- Brett
  http://www.chapelperilous.net/

All articles that coruscate with resplendence are not truly auriferous.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf to convert 200010809 to 2001-08-09

2001-09-10 Thread Wagner-David

make your %4d-%2d-%2d look like %4d-%02d-%02dThe zero with the number says 
to have zero fill instead of blank fill.

Wags ;)

-Original Message-
From: Derrick (Thrawn01) [mailto:[EMAIL PROTECTED]]
Sent: Monday, September 10, 2001 07:57
To: Perl Beginners
Subject: printf to convert 200010809 to 2001-08-09


@data[0]->[17] contains "20010809"

I've been tring to use printf to convert the 200010809 value out as
2001-09-08

$last_open = sprintf "%4d-%2d-%2d",@data[0]->[17];

However this does not work. I get "20010809- 0- 0"
Any sugesstions on how I should make this converstion with out adding alot
of overhead doing it ?


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Printf

2001-09-06 Thread John W. Krahn

Govinderjit Dhinsa wrote:
> 
> How could I please put the following example;
> 
> 1000.000
> 10.000
> 100.000
> 1.000
> To please print out, IN A LINE with the Decimal Place! and not from the
> first number, so all the decimal places would line up!
> example;
> 
> I have tried printf and had no luck


while (  ) {
printf "%20.7f\n", $_;
# ^ ^^
# | ||
# | |Floating point format
# | Number of decimal places
# Total length of field
}

__DATA__
1000.000
10.000
100.000
1.000


Output:
1000.000
  10.000
 100.000
   1.000



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Printf

2001-09-06 Thread Rob Dixon

You haven't been using printf correctly. Try

foreach (qw(
1000.000
10.000
100.000
1.000))

{
printf "%13.6f\n", $_;
}

The %13.6 gives a total field width of 13 and 6 decimal places. Same as C. The
default is to right-justify within the field.

HTH,

Rob


> -Original Message-
> From: Govinderjit Dhinsa [mailto:[EMAIL PROTECTED]]
> Sent: 06 September 2001 15:38
> To: '[EMAIL PROTECTED]'
> Cc: '[EMAIL PROTECTED]'
> Subject: Printf
>
>
> How could I please put the following example;
>
> 1000.000
> 10.000
> 100.000
> 1.000
> To please print out, IN A LINE with the Decimal Place! and not from the
> first number, so all the decimal places would line up!
> example;
>
> I have tried printf and had no luck
>
> Thanks
> GD
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Printf

2001-09-06 Thread System Administrator

Quoting from page 122 of O'Reillys "Programming Perl," which, by the way, is
a most excellent book:
Quote: "As an alternate form of right justification, you may also use #
characters (after an initial @ or ^, and with an optional ".") to specify a
numeric field. This way you can line up the decimal points."  Unquote


- Original Message -
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Thursday, September 06, 2001 9:43 AM
Subject: RE: Printf


> You could try something like this:
>
> ###
> use strict
>
> @a = qw(10.000 100.00 100.0 1000.000);
> $a = $b = "";
>
> foreach $val (@a) {
>   ($a,$b) = split(/\./,$val);
>   write;
> }
>
> format STDOUT =
> @>>>>>>>>>.@<<<<<<<<<
> $a,$b
> .
> ###
>
>
>
>
> > -Original Message-
> > From: Govinderjit Dhinsa [mailto:[EMAIL PROTECTED]]
> > Sent: Thursday, September 06, 2001 9:38 AM
> > To: '[EMAIL PROTECTED]'
> > Cc: '[EMAIL PROTECTED]'
> > Subject: Printf
> >
> >
> > How could I please put the following example;
> >
> > 1000.000
> > 10.000
> > 100.000
> > 1.000
> > To please print out, IN A LINE with the Decimal Place! and
> > not from the
> > first number, so all the decimal places would line up!
> > example;
> >
> > I have tried printf and had no luck
> >
> > Thanks
> > GD
> >
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Printf

2001-09-06 Thread pconnolly

You could try something like this:

###
use strict

@a = qw(10.000 100.00 100.0 1000.000);
$a = $b = "";

foreach $val (@a) {
  ($a,$b) = split(/\./,$val);
  write;
}

format STDOUT =
@>.@<
$a,$b
.
###




> -Original Message-
> From: Govinderjit Dhinsa [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, September 06, 2001 9:38 AM
> To: '[EMAIL PROTECTED]'
> Cc: '[EMAIL PROTECTED]'
> Subject: Printf
> 
> 
> How could I please put the following example;
> 
> 1000.000
> 10.000
> 100.000
> 1.000
> To please print out, IN A LINE with the Decimal Place! and 
> not from the
> first number, so all the decimal places would line up!
> example;
> 
> I have tried printf and had no luck
> 
> Thanks
> GD
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Printf

2001-09-06 Thread Brett W. McCoy

On Thu, 6 Sep 2001, Govinderjit Dhinsa wrote:

> How could I please put the following example;
>
> 1000.000
> 10.000
> 100.000
> 1.000
> To please print out, IN A LINE with the Decimal Place! and not from the
> first number, so all the decimal places would line up!
> example;
>
> I have tried printf and had no luck

What have you tried so far?

-- Brett
  http://www.chapelperilous.net/

FEELINGS are cascading over me!!!


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: printf

2001-07-12 Thread Aaron Craig

At 09:52 12.07.2001 +0100, Govinderjit Dhinsa wrote:
>Does any body know why the printf is not printing anything out!
>
>I tested the rest of the script by replacing the printf line with,
>##
>print "$line";
>##
>the file prints (with out any changes to the file, as expected). So
>everything else is working apart from the printf.
>
>Any help would be much appreciated.
>Thanks.
>GD

first of all, add the following line to the top of your script:

use strict;

which will immediately cause everything to stop working :)

However, it will force you to declare your variables in the scope you're 
using them in, which is a lot safer and will save you problems down the road.


>

Re: printf

2001-07-12 Thread Paul Johnson

On Thu, Jul 12, 2001 at 09:52:32AM +0100, Govinderjit Dhinsa wrote:
> Does any body know why the printf is not printing anything out!
> 
> I tested the rest of the script by replacing the printf line with,
> ##
> print "$line";
> ##
> the file prints (with out any changes to the file, as expected). So
> everything else is working apart from the printf.

> foreach $line(@newarray) {
> 
> $a = substr($_, 0, 18);
> $b = substr($_, 32, 1);
> $c = substr($_, 290, 10);
> printf "%18s %1s %10s\n",$a, $b, $c;
> }

You are iterating with $line, but using $_ in substr.  You need to
select one or the other.

  foreach (@newarray)

or

  $a = substr($line, 0, 18);

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net



RE: printf

2001-07-12 Thread Govinderjit Dhinsa

Does any body know why the printf is not printing anything out!

I tested the rest of the script by replacing the printf line with,
##
print "$line";
##
the file prints (with out any changes to the file, as expected). So
everything else is working apart from the printf.

Any help would be much appreciated.
Thanks.
GD



Re: Printf

2001-07-02 Thread Michael Fowler

On Mon, Jul 02, 2001 at 09:58:43AM +0100, Govinderjit Dhinsa wrote:
> $fields[0],$fields[5],$fields[70],$fields[71],$fields[72],$fields[75],$field
> s[76],$fields[77],$fields[78],$fields[79];

If I couldn't get away with this atrocious by-position form (a hash, with
sanely named and thus self-documenting keys is a much better way to go) I'd
say something like:

@fields[0, 5, 70..72, 75..79]


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--



Re: Printf

2001-07-02 Thread Maxim Berlin

Hello Govinderjit Dhinsa,

Monday, July 02, 2001, Govinderjit Dhinsa <[EMAIL PROTECTED]> wrote:

GD> printf
GD> sortcode"\n%6.6s1%20.20s%45.35s%30.30s%30.30s%30.30s%4.4s%4.4s%8.6s%8.8s",


GD> $fields[0],$fields[5],$fields[70],$fields[71],$fields[72],$fields[75],$field
GD> s[76],$fields[77],$fields[78],$fields[79];

GD> Q. My question was, is there a way to select two or more fields in
GD> one go instead of separate, for example (a guess);

GD> $fields[0 5 70]

@fields[0, 5, 70]

GD> Q. Is there a way of doing this properly!
Depends on your task.

Best wishes,
 Maximmailto:[EMAIL PROTECTED]





Re: Printf

2001-06-27 Thread Chas Owens

printf has the f on the end because it expects a format string.  If you
are on a unix box try typing  "man 3 printf".  This shows you the docs
for C's printf, but since perl's printf is does a straight pass thorugh
to C's printf that shouldn't matter.  Try this in your code:

printf sortcode "%s %s %s %s\n", $fields[0], $fields[5], $fields[70],
fields[77];

The big question I have is "why are you using printf?"  Printf should
only used when you care very much about how the output is formated (ie
printing only two decimal places on a number).


  Don't fall into the trap of using a "printf" when
  a simple "print" would do.  The "print" is more
  efficient and less error prone.


On 27 Jun 2001 13:11:46 +0100, Govinderjit Dhinsa wrote:
> I can not get the printf to print, using the following relevant line of
> code:
> printf sortcode $fields[0],$fields[5],$fields[70],fields[77];
> 
> I am missing something in-between;
> sortcode *$fields[0]
> 
> I have tried different things but had no luck!
> 
> PS Your help would be much appreciated.
> 
> Thanks,
> GD
> 
> 
> 
> 
> 
--
Today is Pungenday, the 32nd day of Confusion in the YOLD 3167
Frink!




RE: Printf

2001-06-27 Thread Richard_Cox

Govinderjit Dhinsa [mailto:[EMAIL PROTECTED]] wrote:
> I can not get the printf to print, using the following 
> relevant line of
> code:
> printf sortcode $fields[0],$fields[5],$fields[70],fields[77];
> 
> I am missing something in-between;
> sortcode *$fields[0]
> 

Is sort code a filehandle (i.e. obtained from open)?

If not then you need a comma (argument separator) and some prefix to
sortcode if it is a variable.

Or do you want:

printf "sortcode $fields[0]$fields[5]$fields[70]fields[77]";

?

Richard Cox 
Senior Software Developer 
Dell Technology Online 
All opinions and statements mine and do not in any way (unless expressly
stated) imply anything at all on behalf of my employer



Re: printf and other stuff

2001-06-05 Thread Rob Hanz

Michael Fowler wrote:
> On Tue, Jun 05, 2001 at 08:05:24AM -0400, Herb Hall wrote:
> > $min = "0" . $min if $min < 10;
> >
> > will pad your minutes with a 0. I have used both methods for various
> > reasons. You probably only need to use one or the other. I would use the
> > printf unless you have some need to have the variables padded with 0's.
>
> printf will happily pad with zeroes for you:
>
> printf("%03d\n", 4);
>
> outputs
>
> 004
>
> I see no real reason to use manual padding unless you're trying to be
> consistent in the code.

And even if you do need your variables to be padded, well, that's why
there's sprintf

$min = sprintf("%03d",$min);
print "$min\n";

outputs:

003




Re: printf and other stuff

2001-06-05 Thread Michael Fowler

On Tue, Jun 05, 2001 at 08:05:24AM -0400, Herb Hall wrote:
> $min = "0" . $min if $min < 10;
> 
> will pad your minutes with a 0. I have used both methods for various
> reasons. You probably only need to use one or the other. I would use the
> printf unless you have some need to have the variables padded with 0's.

printf will happily pad with zeroes for you:

printf("%03d\n", 4);

outputs
   
004

I see no real reason to use manual padding unless you're trying to be
consistent in the code.


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--



RE: printf and other stuff

2001-06-05 Thread Herb Hall

> ### get time
> my($sec, $min, $hour, $mday, $month, $year) = (localtime)[0..5];
> $year += 1900;
> $mday = "0" . $mday if $mday < 10;
> $month++; # perl counts from -1 on occasion
> $month = "0" . $month if $month < 10;
> 
>
> -- later in the same file --
>
> print TOFILE "On $month/$mday/$year At $hour:$min you wrote:\n\n";
>
> how do I use print to provide a leading '0' to $min, such that
> I get 5:01 and not 5:1

There were several correct responses utilizing printf. I just wanted to
mention that you have another option. You already have two lines of code
that look like this:

$month = "0" . $month if $month < 10;
$mday = "0" . $mday if $mday < 10;

adding

$min = "0" . $min if $min < 10;

will pad your minutes with a 0. I have used both methods for various
reasons. You probably only need to use one or the other. I would use the
printf unless you have some need to have the variables padded with 0's.

Herb Hall





Re: printf and other stuff

2001-06-01 Thread Timothy Kimball


David Gilden wrote:: ##this does not work
: 
: print ($sort_order) ? 'Newest First' : 'Oldest First';

Perl thinks you're doing this:

print($sort_order) ? 'Newest First' : 'Oldest First';

that is, it's taking $sort_order as an argument to print().

Either remove the parens:

print $sort_order ? 'Newest First' : 'Oldest First';

or put a plus in front of the opening paren, to show that you're just
using them for grouping and that print() shouldn't think that that's
its argument list:

print +($sort_order) ? 'Newest First' : 'Oldest First';

or you can specify STDOUT explicitly:

print STDOUT ($sort_order) ? 'Newest First' : 'Oldest First';

: printf question--
: 
: ### get time
: my($sec, $min, $hour, $mday, $month, $year) = (localtime)[0..5];
: $year += 1900;
: $mday = "0" . $mday if $mday < 10;
: $month++; # perl counts from -1 on occasion
: $month = "0" . $month if $month < 10;
: 
: 
: -- later in the same file --
: 
: print TOFILE "On $month/$mday/$year At $hour:$min you wrote:\n\n";
: 
: how do I use print to provide a leading '0' to $min, such that
: I get 5:01 and not 5:1

use printf with a specification of %02d (which means "2 digits padded
with leading zeros"):

printf TOFILE "On %02d/%02d/%4d At %02d:%02d you wrote:\n\n",
$month, $day, $year, $hour, $min;

-- tdk



Re: printf and other stuff

2001-06-01 Thread Christian Campbell

David Gilden wrote:
> Is there a way to combine the last two statements?
[...] 
> $sort_type = ($sort_order) ? 'Newest First' : 'Oldest First';
> # are the () optional?
> 
> print $sort_type;

print $sort_order ? 'Newest First' : 'Oldest First';

or

print( $sort_order ? 'Newest First' : 'Oldest First' );

(or

print( ($sort_order) ? 'Newest First' : 'Oldest First' );

if you're bent on putting parens around your guard.)
 
> ##this does not work
> 
> print ($sort_order) ? 'Newest First' : 'Oldest First';

The ()'s are taken to be the parameter list for print (print binds
more tightly than ?).
 
> printf question--
> 
> ### get time
> my($sec, $min, $hour, $mday, $month, $year) = (localtime)[0..5];
> $year += 1900;
> $mday = "0" . $mday if $mday < 10;
> $month++; # perl counts from -1 on occasion
> $month = "0" . $month if $month < 10;
> 
> 
> -- later in the same file --
> 
> print TOFILE "On $month/$mday/$year At $hour:$min you wrote:\n\n";
> 
> how do I use print to provide a leading '0' to $min, such that
> I get 5:01 and not 5:1

printf "On $month/$mday/$year At $hour:%02d you wrote:\n\n",
$min;

Christian

__
117 NW 15th Street # S107[EMAIL PROTECTED]
Gainesville, FL  32603-1973 (352) 392-0851



Re: printf and other stuff

2001-06-01 Thread Me

I'll just answer the 'other stuff'.

> ##this does not work
> 
> print ($sort_order) ? 'Newest First' : 'Oldest First';

Perl sees this as a function() call followed by the rest.

Instead, do:

> print $sort_order ? 'Newest First' : 'Oldest First';

In general, Perl works to minimize the number of
brackets you need.

> $sort_type = ($sort_order) ? 'Newest First' : 'Oldest First';
> # are the () optional?

Yes.

The issue is 'precedence'.

When in doubt, I usually switch to a command prompt
and enter a few perl one liners, eg I just tried

perl -e 'print $foo ? 1 : 2'

to confirm that I was right about one of the above points.




Re: printf and other stuff

2001-06-01 Thread Jeff Pinyan

On Jun 1, David Gilden said:

>$sort_order =0;
>
>$sort_type = ($sort_order) ? 'Newest First' : 'Oldest First';
># are the () optional?

In this case, yes, you don't need those parens.

>print ($sort_order) ? 'Newest First' : 'Oldest First';

Remove the parens, or add them around the entire argument list to print:

  print( ($sort_order) ? '...' : '...' );


>printf question--
>how do I use print to provide a leading '0' to $min, such that
>I get 5:01 and not 5:1

You want to use printf() or sprintf():

  printf "%d:%02d\n",   5, 1;  # "5:01";
  printf "%02d:%02d\n", 5, 1;  # "05:01";

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
**  Manning Publications, Co, is publishing my Perl Regex book  **




Re: printf and other stuff

2001-06-01 Thread John Joseph Trammell

On Fri, Jun 01, 2001 at 03:05:59PM -0400, David Gilden wrote:
[snip -- was there a question about '?:' ?]

> printf question--
> 
> ### get time
> my($sec, $min, $hour, $mday, $month, $year) = (localtime)[0..5];
> $year += 1900;
> $mday = "0" . $mday if $mday < 10;
> $month++; # perl counts from -1 on occasion
> $month = "0" . $month if $month < 10;
> 
> 
> -- later in the same file --
> 
> print TOFILE "On $month/$mday/$year At $hour:$min you wrote:\n\n";
> 
> how do I use print to provide a leading '0' to $min, such that
> I get 5:01 and not 5:1
> 

Maybe you can just use the output from localtime() in scalar context?

 [ ~ ] perl -e 'print scalar localtime,"\n"'
 Fri Jun  1 14:13:15 2001
 [ ~ ]

Otherwise something like 

 printf "%02d:%02d:%02d", $hour, $min, $sec;

is likely what you're looking for.

-- 
Rule #0: Spam is theft.



Re: printf and other stuff

2001-06-01 Thread Ken

Remove the ()'s and it works.

For the 0's:
printf "On %02d/%02d/%04d At %02d:%02d you wrote:\n\n", $month, $mday,
$year, $hour, $min;
see perldoc -f printf for more info.
- Original Message -
From: "David Gilden" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, June 01, 2001 1:05 PM
Subject: printf and other stuff


> Good afternoon,
>
> Is there a way to combine the last two statements?
>
> #!/usr/bin/perl -w
>
> $sort_order =0;
>
> $sort_type = ($sort_order) ? 'Newest First' : 'Oldest First';
> # are the () optional?
>
> print $sort_type;
>
>
> ##this does not work
>
> print ($sort_order) ? 'Newest First' : 'Oldest First';
>
> printf question--
>
> ### get time
> my($sec, $min, $hour, $mday, $month, $year) = (localtime)[0..5];
> $year += 1900;
> $mday = "0" . $mday if $mday < 10;
> $month++; # perl counts from -1 on occasion
> $month = "0" . $month if $month < 10;
> 
>
> -- later in the same file --
>
> print TOFILE "On $month/$mday/$year At $hour:$min you wrote:\n\n";
>
> how do I use print to provide a leading '0' to $min, such that
> I get 5:01 and not 5:1
>
>
> Thanks!
>
> Dave G.
>
>