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/




printf

2013-03-04 Thread Chris Stinemetz
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


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/




printf

2012-08-31 Thread 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.
The perl version is 5.10.1 on debian.

Regards

-- 
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/




printf explicitly

2011-03-18 Thread Chris Stinemetz
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,
);

my @records;

my @lines = read_file( $filepath );

chomp @lines;

foreach my $line ( @lines ) {

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 ;

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) ;



--
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/




printf

2010-07-12 Thread newsense
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 /:/ ));
}

-- 
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/




printf

2009-10-27 Thread Aimee Cardenas

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?


Thanks,

Aimee Cardenas

--
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/




printf with currency symbols

2009-10-26 Thread Bryan R Harris


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);

- Bryan



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




AW: Building a fmt line for printf with a carriage return

2009-10-26 Thread Thomas Bätzler
Hi,

Wagner, David --- Senior Programmer Analyst --- CFS  
wrote:
>   Here is the sample script I was playing with:
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> my $MyLine1 = q[%2d  %5s  %6s];
> my $MyLine2 = q[%2d  %5s \n%6s];

The q// operator is equivalent to single quotes, so escape sequences like \n 
are not interpolated.

If you want \n to be expanded to a newline character, you need to use qq or 
double quotes, or a combination of q// and qq// or quotes and text 
concatenation.

HTH,
Thomas

--
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/




printf and zero padding

2009-10-24 Thread Harry Putnam
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;
  }


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




Re: Building a fmt line for printf with a carriage return

2009-10-23 Thread Shawn H Corey
Wagner, David --- Senior Programmer Analyst --- CFS wrote:
>   I thought I had done this before, but I guess not. I build a formt line 
> for printf like:
>   q[%-3s%-4s%5s%6s];
>   But I want to insert a carriage return after say %-4s( I have a nubmer 
> of fields and depending on the size, it is not a constant after column 2, but 
> could be column 3 or 23.
>   
>   I have tried:
>   q[%-3s%-4s] . qq[\\n] . q[%5s%6s]

q[%-3s%-4s] . qq[\n] . q[%5s%6s]

Also:

qq[\%-3s\%-4s\n\%5s\%6s];


-- 
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/




Building a fmt line for printf with a carriage return

2009-10-23 Thread Wagner, David --- Senior Programmer Analyst --- CFS
I thought I had done this before, but I guess not. I build a formt line 
for printf like:
q[%-3s%-4s%5s%6s];
But I want to insert a carriage return after say %-4s( I have a nubmer 
of fields and depending on the size, it is not a constant after column 2, but 
could be column 3 or 23.

I have tried:
q[%-3s%-4s] . qq[\\n] . q[%5s%6s]
or
q[%-3s%-4s\n%5s%6s]

But no matter what I do, it does not generate the carriage return. 
Shows up in the line. I know I am missing some very basic point, but has 
escaped me.

Here is the sample script I was playing with:
#!/usr/bin/perl

use strict;
use warnings;

my $MyLine1 = q[%2d  %5s  %6s];
my $MyLine2 = q[%2d  %5s \n%6s];

printf "${MyLine1}\n",
2,
q[abc],
q[def];
        
printf "$MyLine2\n",
2,
q[abc],
q[def];

 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 



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




Re: How does printf able to receive a pointer when passing it a string constant?

2009-05-13 Thread Jim Gibson
On 5/13/09 Wed  May 13, 2009  4:48 AM, "Michael Alipio"
 scribbled:

> I have a c code that looks like this:
> 
> #include
> 
> main (){
> char girl[] = "anna";
> char boy[] = "jude";
> stringcopy(boy, girl); /* copy boy to girl */
> printf("%s", girl);
> 
> }
> 
> void stringcopy(char *b, char *g){
> 
> while ((*g++ = *b++) != '\0')
> ;
> }
> 
> 
> It prints fine...
> However if I replace the stringcopy call arguments with "jude", "anna"
> it compiles fine but i get segmentation fault when running.
> 
> 
> How come printf can accept variable names as well as constant strings such as:
> 
> printf ("%s", girl);
> 
> and
> 
> printf ("Hello World\n");

Because printf does not attempt to change its arguments.

> My stringcopy function only accepts pointers. Shouldn't I be passing pointer
> to the first element of "anna" when passing the string constant "anna"?? )

stringcopy modifies its second argument. Your compiler is not letting you
modify a string "constant". That way, different parts of your program can
share the same string constant without one part being affected by what
another part does.

> How does printf print a string constant then?

Easily, because it does not attempt to modify it.

May I ask you a question? Why are you posting C questions to a Perl mailing
list?



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




Re: How does printf able to receive a pointer when passing it a string constant?

2009-05-13 Thread Chas. Owens
On Wed, May 13, 2009 at 07:48, Michael Alipio  wrote:
>
>
> I have a c code that looks like this:

And C isn't Perl, perhaps you should ask these sort of questions on a
C list or newsgroup[1]?  Or maybe Stack Overflow[2]?

snip
> However if I replace the stringcopy call arguments with "jude", "anna"
> it compiles fine but i get segmentation fault when running.
snip

Because they are string constants and you are trying to modify the
second string.  You aren't allowed to do that.

snip
> How come printf can accept variable names as well as constant strings such as:
>
> printf ("%s", girl);
>
> and
>
> printf ("Hello World\n");
snip

Because printf only reads from the pointer, it doesn't modify it.

snip
> My stringcopy function only accepts pointers. Shouldn't I be passing pointer 
> to the first element of "anna" when passing the string constant "anna"?? )
snip

You can point to it, but you can't modify it.

1. http://groups.google.com/group/comp.lang.c/topics
2. http://www.stackoverflow.com

-- 
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/




How does printf able to receive a pointer when passing it a string constant?

2009-05-13 Thread Michael Alipio


I have a c code that looks like this:

#include

main (){
char girl[] = "anna";
char boy[] = "jude";
stringcopy(boy, girl); /* copy boy to girl */
printf("%s", girl); 

}

void stringcopy(char *b, char *g){

while ((*g++ = *b++) != '\0')
;
}


It prints fine...
However if I replace the stringcopy call arguments with "jude", "anna"
it compiles fine but i get segmentation fault when running.


How come printf can accept variable names as well as constant strings such as: 

printf ("%s", girl); 

and

printf ("Hello World\n");


My stringcopy function only accepts pointers. Shouldn't I be passing pointer to 
the first element of "anna" when passing the string constant "anna"?? )


How does printf print a string constant then?



  

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




Re: how to print % when using printf

2009-02-07 Thread Dr.Ruud
itshardtogetone wrote:

> How do I print the % sign when using printf.

RTFM. See for example: perldoc -f printf, which should point you to the
sprintf doc.
It is even the first mentioned conversion there.
The doc is on a thingy called "the web" too:
http://perldoc.perl.org/functions/sprintf.html

-- 
Ruud

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




Re: how to print % when using printf

2009-02-06 Thread Jeff Peng
2009/2/7 itshardtogetone :
> Hi,
> How do I print the % sign when using printf.


use another % to escape it.


-- 
Jeff Peng
Office: +86-20-38350822
AIM: jeffpang
www.dtonenetworks.com

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




how to print % when using printf

2009-02-06 Thread itshardtogetone
Hi,
How do I print the % sign when using printf.
I did the following which produced the following error "illegal conversion in 
printf "%)" at line 185"

printf ("Total $total_number_of_bets\(%4.2f\%\) 
times.\n",$percentage_total_number_of_bets);

Thanks



Re: using (dot) . with printf does not incur the results I expected

2008-12-24 Thread John W. Krahn

Richard wrote:

what's wrong w/ this ?
I used (dot) . to indicate the maximum length of each element in the 
print but last one does not print out in alinged format..

What am i missing?

use warnings;
use strict;

my $yabal = 'truncated';
my $never = 'sai';
my $noway = 'han1';

my %never = qw(hi how are you today fine i am good and everyday12345678 
never12345689);


printf "%-.5s %-15s %-.2s\n", $yabal, $never, $noway;

for (keys %never) {
 printf "%.2s %.10s\n", $_, $never{$_};


It looks like you want this instead:

printf "%-2.2s %-10.10s\n", $_, $never{$_};



}

r...@myserver ~> ././yahoo2
trunc sai ha
hi how
go and
to fine
ar you
ev never12345
i am



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

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




Re: using (dot) . with printf does not incur the results I expected

2008-12-24 Thread Richard

Richard wrote:

what's wrong w/ this ?
I used (dot) . to indicate the maximum length of each element in the 
print but last one does not print out in alinged format..

What am i missing?

use warnings;
use strict;

my $yabal = 'truncated';
my $never = 'sai';
my $noway = 'han1';

my %never = qw(hi how are you today fine i am good and 
everyday12345678 never12345689);


printf "%-.5s %-15s %-.2s\n", $yabal, $never, $noway;

for (keys %never) {
 printf "%.2s %.10s\n", $_, $never{$_};
}

r...@myserver ~> ././yahoo2
trunc sai ha
hi how
go and
to fine
ar you
ev never12345
i am



I guess I was missing the 2.2 option...


my %never = qw(hi how are you today fine i am good and everyday12345678 
never12345689);



for (keys %never) {
 printf "%-2.2s %-10.10s\n", $_, $never{$_};
}

r...@myserver  ~> ./!$
././yahoo2
trunc sai ha
hi how  
go and  
to fine 
ar you  
ev never12345
i  am 




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




using (dot) . with printf does not incur the results I expected

2008-12-24 Thread Richard

what's wrong w/ this ?
I used (dot) . to indicate the maximum length of each element in the 
print but last one does not print out in alinged format..

What am i missing?

use warnings;
use strict;

my $yabal = 'truncated';
my $never = 'sai';
my $noway = 'han1';

my %never = qw(hi how are you today fine i am good and everyday12345678 
never12345689);


printf "%-.5s %-15s %-.2s\n", $yabal, $never, $noway;

for (keys %never) {
 printf "%.2s %.10s\n", $_, $never{$_};
}

r...@myserver ~> ././yahoo2
trunc sai ha
hi how
go and
to fine
ar you
ev never12345
i am


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




Re: uninitialized value in printf

2008-02-02 Thread obdulio santana
Thaks listers, your comments helped a lot.

everything was fixed.

regards


Re: uninitialized value in printf

2008-02-02 Thread asmith
Hi John
You'd make life easier for everyone if you prefixed each line of your
program with the line number. The Linux command
cat -n < file> will do that for you.
Andrew in Edinburgh,Scotland

John W. Krahn wrote:
> obdulio santana wrote:
>> I must mix 3 files, and produce a little report but in line 23 and 31
>> is a
>> warning of uninitalized value I  really don't see  the mistake.
>>
>>
>> use warnings;
>
> use strict;
>
>> @lfile0 = ;
>> chomp @lfile0;
>> @meses = qw(ene feb mar abr may jun jul ago sep oct nov dic);
>> @files= glob "78*";
>> my %textos;
>> for (@files){
>> open FILE,"<$_";
>
> You should *always* verify that the file opened correctly:
>
>   open FILE, '<', $_ or die "Cannot open '$_' $!";
>
>
>> $textos{$_}=[];
>> chomp @{$textos{$_}};
>> s/.{5}// for @{$textos{$_}};
>> }
>> ($day,$month,$year) = (localtime)[3..5];
>> $dec = $day /10;
>> $month++;
>> $dec = 3 if $dec < 1;
>> $year+=1900;
>> $file = sprintf "vcl%02d%02d%4d.txt",$day,$month,$year;
>> open FILEOUT, ">$file";
>
> You should *always* verify that the file opened correctly:
>
> open FILEOUT, '>', $file or die "Cannot open '$file' $!";
>
>
>> print FILEOUT "Resumen decadal \n" ;
>> printf FILEOUT "$meses[$month-1]/$year;#%d \n",$dec  ;
>> $form = "%13s" x @files ;
>> printf FILEOUT "%23s" . "$form\n",sort keys %textos ;
>
> You have one more printf format then you have keys in %textos:
>
> printf FILEOUT '%23s' . ( '%13s' x ( @files - 1 ) ) . "\n", sort keys
> %textos ;
>
>
>> for $line (5..50){
>> @str=();
>> for (sort keys %textos){
>> push @str,${$textos{$_}}[$line];
>> }
>> $form = "%13s" x @files ;
>> $form = "%-10s".$form."\n";
>> printf FILEOUT $form,$lfile0[$line-6],@str;
>
> $line starts out with a value of 5.  5 - 6 == -1.  $lfile0[ -1 ] is
> the *last* element of @lfile0.  Did you really want to start with the
> last element?
>
> When $line contains 48, 49 or 50 the value of $lfile0[$line-6] is undef.
>
>
>> }
>> close FILEOUT;
>> __END__
>
>
>
> John

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




Re: uninitialized value in printf

2008-02-01 Thread John W. Krahn

obdulio santana wrote:

I must mix 3 files, and produce a little report but in line 23 and 31 is a
warning of uninitalized value I  really don't see  the mistake.


use warnings;


use strict;


@lfile0 = ;
chomp @lfile0;
@meses = qw(ene feb mar abr may jun jul ago sep oct nov dic);
@files= glob "78*";
my %textos;
for (@files){
open FILE,"<$_";


You should *always* verify that the file opened correctly:

  open FILE, '<', $_ or die "Cannot open '$_' $!";



$textos{$_}=[];
chomp @{$textos{$_}};
s/.{5}// for @{$textos{$_}};
}
($day,$month,$year) = (localtime)[3..5];
$dec = $day /10;
$month++;
$dec = 3 if $dec < 1;
$year+=1900;
$file = sprintf "vcl%02d%02d%4d.txt",$day,$month,$year;
open FILEOUT, ">$file";


You should *always* verify that the file opened correctly:

open FILEOUT, '>', $file or die "Cannot open '$file' $!";



print FILEOUT "Resumen decadal \n" ;
printf FILEOUT "$meses[$month-1]/$year;#%d \n",$dec  ;
$form = "%13s" x @files ;
printf FILEOUT "%23s" . "$form\n",sort keys %textos ;


You have one more printf format then you have keys in %textos:

printf FILEOUT '%23s' . ( '%13s' x ( @files - 1 ) ) . "\n", sort keys 
%textos ;




for $line (5..50){
@str=();
for (sort keys %textos){
push @str,${$textos{$_}}[$line];
}
$form = "%13s" x @files ;
$form = "%-10s".$form."\n";
printf FILEOUT $form,$lfile0[$line-6],@str;


$line starts out with a value of 5.  5 - 6 == -1.  $lfile0[ -1 ] is the 
*last* element of @lfile0.  Did you really want to start with the last 
element?


When $line contains 48, 49 or 50 the value of $lfile0[$line-6] is undef.



}
close FILEOUT;
__END__




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

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




Re: uninitialized value in printf

2008-02-01 Thread Gunnar Hjalmarsson

obdulio santana wrote:

I must mix 3 files, and produce a little report but in line 23 and 31 is a
warning of uninitalized value I  really don't see  the mistake.

use warnings;


use strict;


@lfile0 = ;
chomp @lfile0;
@meses = qw(ene feb mar abr may jun jul ago sep oct nov dic);
@files= glob "78*";
my %textos;
for (@files){
open FILE,"<$_";
$textos{$_}=[];
chomp @{$textos{$_}};
s/.{5}// for @{$textos{$_}};
}
($day,$month,$year) = (localtime)[3..5];
$dec = $day /10;
$month++;
$dec = 3 if $dec < 1;
$year+=1900;
$file = sprintf "vcl%02d%02d%4d.txt",$day,$month,$year;
open FILEOUT, ">$file";
print FILEOUT "Resumen decadal \n" ;
printf FILEOUT "$meses[$month-1]/$year;#%d \n",$dec  ;
$form = "%13s" x @files ;
printf FILEOUT "%23s" . "$form\n",sort keys %textos ;


The number of conversions exceeds the number of keys by 1.


for $line (5..50){


for $line ( 5 .. 5 + $#lfile0 ) {


@str=();
for (sort keys %textos){
push @str,${$textos{$_}}[$line];
}
$form = "%13s" x @files ;
$form = "%-10s".$form."\n";
printf FILEOUT $form,$lfile0[$line-6],@str;

-^


}
close FILEOUT;




--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




uninitialized value in printf

2008-02-01 Thread obdulio santana
I must mix 3 files, and produce a little report but in line 23 and 31 is a
warning of uninitalized value I  really don't see  the mistake.

Thank you in advance




use warnings;
@lfile0 = ;
chomp @lfile0;
@meses = qw(ene feb mar abr may jun jul ago sep oct nov dic);
@files= glob "78*";
my %textos;
for (@files){
open FILE,"<$_";
$textos{$_}=[];
chomp @{$textos{$_}};
s/.{5}// for @{$textos{$_}};
}
($day,$month,$year) = (localtime)[3..5];
$dec = $day /10;
$month++;
$dec = 3 if $dec < 1;
$year+=1900;
$file = sprintf "vcl%02d%02d%4d.txt",$day,$month,$year;
open FILEOUT, ">$file";
print FILEOUT "Resumen decadal \n" ;
printf FILEOUT "$meses[$month-1]/$year;#%d \n",$dec  ;
$form = "%13s" x @files ;
printf FILEOUT "%23s" . "$form\n",sort keys %textos ;
for $line (5..50){
@str=();
for (sort keys %textos){
push @str,${$textos{$_}}[$line];
}
$form = "%13s" x @files ;
$form = "%-10s".$form."\n";
printf FILEOUT $form,$lfile0[$line-6],@str;
}
close FILEOUT;
__END__
1.A)
B)
C)
D)
E)
F)
2.A)
B)
C)
3.A)
4.A)
B)
C)
D)
5.A)
B)
C)



5.D)
6.A)
7.A)
8.A)
B)
C)
D)
9.A)
10.A)
B)
C)
11.A)
B)
C)
D)
E)
12.A)
B)
C)
D)
E)
F)


__

and example of the file to mix

_ this is the top
Resumen decadal
78343 - Ene/2008;#2

 1.A)  tres
   B)  23.8
   C)   32.6-19
   D)   15.0-14
   E)  29.9
   F)  17.8
 2.A)   49.5-17-18
   B)   12.5-14
   C)  27.8
 3.A)  26.0
 4.A)   5.0
   B)   20.9-19
   C)0.2-13
   D)  14.3
 5.A) 110.6
   B)  110.1-20
   C) 2
  1
  1
  1
 5.D) 8
 6.A)10
 7.A) 41.75
 8.A) E
   B)   12.8-16
   C)   8.9
   D)   1.4
 9.A) X
10.A) 3
   B) 1
   C) 5
11.A)81
   B)54
   C)100-13
   D) 33-19
   E) 5
12.A) 7
   B) 2
   C) 0
   D) X
   E) 0
   F) X






___this is the bottom


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/




printf problem

2007-10-30 Thread Julie A. Bolduc
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/




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/




printf

2007-01-18 Thread Beginner
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/




Re: sprintf and printf in Perl

2006-09-14 Thread jeffhua
 Hello,
 
The simple difference is,'sprintf' format the strings and pass the result to a 
variable.
 
$ perl -e 'my $var=sprintf "%30s\n","hello,world";print $var'
   hello,world
 
While 'printf' print the result directly to screen.
 
$ perl -e 'printf "%30s\n","hello,world"'
   hello,world
 
 Hope it helps.
 
-jeff
 
-Original Message-
From: [EMAIL PROTECTED]
To: beginners@perl.org
Sent: Thu, 14 Sep 2006 9:44 PM
Subject: sprintf and printf in Perl


Hi all,

First of all I want to thank you all for reading and
replying my previous posts.

Now I come across to another problem:

I want to print out a AoA. If I use printf I get what
I expect. If I use sprintf for string I get error
messages but the remaining still look OK. But If use
sprintf also for numbers I get nothing only error
messages. I look at the POD. It looks like sprintf and
printf are changeable to me. Any comments?

Thanks,

Li

#!c:/Perl/bin/perl.exe

use warnings;
use strict;
use Data::Dumper;

my @array=(
['A',1,2,3,4,5,6,7],
['B',1,2,3,4,5,6,7],
['C',1,2,3,4,5,6,7],
    
   );   

foreach my $r(@array){  
foreach (@$r){
if (/\d+/){printf "%10.2f",$_ ;
  }else{printf "%10s",$_;}

  } 
  print "
";
}

#
Output1 from screen (use printf only and expected)

  A  1.00  2.00
  B  1.00  2.00
  C  1.00  2.00

Output2 from screen (use sprintf for string)

Useless use of sprintf in void context at math9.pl
line 18
  1.00  2.00
  1.00  2.00
  1.00  2.00

Output3 from screen (use sprintf for both)
Useless use of sprintf in void context at math9.pl
line 18.
Useless use of sprintf in void context at math9.pl
line 18.




__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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

Check out the new AOL.  Most comprehensive set of free safety and security 
tools, free access to millions of high-quality videos from across the web, free 
AOL Mail and more.


sprintf and printf in Perl

2006-09-14 Thread chen li
Hi all,

First of all I want to thank you all for reading and
replying my previous posts.

Now I come across to another problem:

I want to print out a AoA. If I use printf I get what
I expect. If I use sprintf for string I get error
messages but the remaining still look OK. But If use
sprintf also for numbers I get nothing only error
messages. I look at the POD. It looks like sprintf and
printf are changeable to me. Any comments?

Thanks,

Li

#!c:/Perl/bin/perl.exe

use warnings;
use strict;
use Data::Dumper;

my @array=(
['A',1,2,3,4,5,6,7],
['B',1,2,3,4,5,6,7],
['C',1,2,3,4,5,6,7],

   );   

foreach my $r(@array){  
foreach (@$r){
if (/\d+/){printf "%10.2f",$_ ;
  }else{printf "%10s",$_;}

  } 
  print "\n";
}

#####
Output1 from screen (use printf only and expected)

  A  1.00  2.00
  B  1.00  2.00
  C  1.00  2.00

Output2 from screen (use sprintf for string)

Useless use of sprintf in void context at math9.pl
line 18
  1.00  2.00
  1.00  2.00
  1.00  2.00

Output3 from screen (use sprintf for both)
Useless use of sprintf in void context at math9.pl
line 18.
Useless use of sprintf in void context at math9.pl
line 18.




__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




Re: hex conversion to dec - print and printf differences

2006-04-11 Thread Offer Kaye
On 4/11/06, John W. Krahn wrote:
> > This is probably trivial, but I couldn't find a mention of this
> > anywhere - why do the following 2 code lines produce different
> > results?
> >>perl -e 'printf "%d\n" ,0x_'
> > -1
> >>perl -e 'print 0x_ , "\n"'
> > 4294967295
>
> perldoc perlnumber
>

Hi John,
Thanks for the pointer, I guess you wanted me to see the "All the
operators which need an argument in the integer format treat the
argument as in modular arithmetic" sentence.

But why is "print" behaving differently? I.e., why is the 0x_
not treated as a number?

Thanks,
--
Offer Kaye

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




Re: hex conversion to dec - print and printf differences

2006-04-11 Thread John W. Krahn
Offer Kaye wrote:
> On 4/11/06, John W. Krahn wrote:
>>>This is probably trivial, but I couldn't find a mention of this
>>>anywhere - why do the following 2 code lines produce different
>>>results?
>>>>perl -e 'printf "%d\n" ,0x_'
>>>-1
>>>>perl -e 'print 0x_ , "\n"'
>>>4294967295
>>perldoc perlnumber
> 
> Thanks for the pointer, I guess you wanted me to see the "All the
> operators which need an argument in the integer format treat the
> argument as in modular arithmetic" sentence.

I thought that that might be the documentation that you were looking for.

> But why is "print" behaving differently? I.e., why is the 0x_
> not treated as a number?

0x_ is treated as a number because it is a number, the number
4294967295 as an unsigned integer and -1 as a signed integer.

perldoc -f sprintf
[snip]
  %d   a signed integer, in decimal
  %u   an unsigned integer, in decimal


Use the %u format if you want the unsigned integer representation.



John
-- 
use Perl;
program
fulfillment

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




Re: hex conversion to dec - print and printf differences

2006-04-11 Thread John W. Krahn
Offer Kaye wrote:
> Hi,

Hello,

> This is probably trivial, but I couldn't find a mention of this
> anywhere - why do the following 2 code lines produce different
> results?
>>perl -e 'printf "%d\n" ,0x_'
> -1
>>perl -e 'print 0x_ , "\n"'
> 4294967295

perldoc perlnumber


John
-- 
use Perl;
program
fulfillment

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




hex conversion to dec - print and printf differences

2006-04-11 Thread Offer Kaye
Hi,
This is probably trivial, but I couldn't find a mention of this
anywhere - why do the following 2 code lines produce different
results?
> perl -e 'printf "%d\n" ,0x_'
-1
> perl -e 'print 0x_ , "\n"'
4294967295

Even stranger:
> perl -e 'printf "%d\n" ,0x8000_'
-2147483648
 > perl -e 'print 0x8000_ , "\n"'
2147483648
> perl -e 'print 0x8000_ - 0x1 , "\n"'
2147483647
> perl -e 'printf "%d\n" ,0x8000_ - 0x1'
2147483647

This is on a 32bit Linux OS.

Thanks,
--
Offer Kaye

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




RE: Replacing printf by print

2005-07-22 Thread pradeep.goel

Thanks a million.

Best Regards,
Pradeep

-Original Message-
From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]
Sent: Friday, July 22, 2005 6:00 PM
To: Pradeep Goel (WT01 - TELECOM SOLUTIONS)
Cc: beginners@perl.org
Subject: RE: Replacing printf by print

On Jul 22, [EMAIL PROTECTED] said:

> I am replacing it since there are some % sign in the strings being
> passed making the program crash. Replacement solves the problem as
such,
> but wanted to countercheck, If these replacements could create any
> problem anywhere else! , or lator in the program?

The only problem there might be is that print() is susceptible to to the

$, and $\ variables.  The $, variable defines what gets printed in
between
the list of strings you pass to print(), and $\ defines what gets
printed
after the strings you pass to print().  Both default to the empty
string,
but if you were to change them, for some reason, print()s output would
be
affected (but printf()s would not).

   print "a", "b", "c";  # abc

   $, = "!";
   print "a", "b", "c";  # a!b!c

   $\ = "?";
   print "a", "b", "c";  # a!b!c?

But this is not likely to happen, methinks.

--
Jeff "japhy" Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart



Confidentiality Notice

The information contained in this electronic message and any attachments to 
this message are intended
for the exclusive use of the addressee(s) and may contain confidential or 
privileged information. If
you are not the intended recipient, please notify the sender at Wipro or [EMAIL 
PROTECTED] immediately
and destroy all copies of this message and any attachments.

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




Re: [SPAM DETECT] Re: Replcaing printf by print

2005-07-22 Thread Xavier Noria

On Jul 22, 2005, at 14:00, Jeff 'japhy' Pinyan wrote:


On Jul 22, [EMAIL PROTECTED] said:



Does replacing printf by print make any difference in the program?
Especially at places where there is no format string passed or  
just $_

is passed?



It probably doesn't have a noticeable difference unless you compare  
the running of it many, MANY times.  But using printf() needlessly  
is silly, and in some cases, dangerous.  If you have a % sign in  
your string, printf will expect an argument to go with it.


Just for the record, another formal difference is that printf does  
not add $\, which might not be a problem anyway.


I second the remark of japhy, be specific, use print when you mean  
print.


-- fxn

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




RE: Replacing printf by print

2005-07-22 Thread Jeff 'japhy' Pinyan

On Jul 22, [EMAIL PROTECTED] said:


I am replacing it since there are some % sign in the strings being
passed making the program crash. Replacement solves the problem as such,
but wanted to countercheck, If these replacements could create any
problem anywhere else! , or lator in the program?


The only problem there might be is that print() is susceptible to to the 
$, and $\ variables.  The $, variable defines what gets printed in between 
the list of strings you pass to print(), and $\ defines what gets printed 
after the strings you pass to print().  Both default to the empty string, 
but if you were to change them, for some reason, print()s output would be 
affected (but printf()s would not).


  print "a", "b", "c";  # abc

  $, = "!";
  print "a", "b", "c";  # a!b!c

  $\ = "?";
  print "a", "b", "c";  # a!b!c?

But this is not likely to happen, methinks.

--
Jeff "japhy" Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

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




RE: Replacing printf by print

2005-07-22 Thread pradeep.goel

That's the whole problem.
I am replacing it since there are some % sign in the strings being
passed making the program crash. Replacement solves the problem as such,
but wanted to countercheck, If these replacements could create any
problem anywhere else! , or lator in the program?
(Although I don't think it should).

Thanks and regards,
Pradeep
-Original Message-
From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]
Sent: Friday, July 22, 2005 5:31 PM
To: Pradeep Goel (WT01 - TELECOM SOLUTIONS)
Cc: beginners@perl.org
Subject: Re: Replcaing printf by print

On Jul 22, [EMAIL PROTECTED] said:

> Does replacing printf by print make any difference in the program?
> Especially at places where there is no format string passed or just $_
> is passed?

It probably doesn't have a noticeable difference unless you compare the
running of it many, MANY times.  But using printf() needlessly is silly,

and in some cases, dangerous.  If you have a % sign in your string,
printf
will expect an argument to go with it.

--
Jeff "japhy" Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart



Confidentiality Notice

The information contained in this electronic message and any attachments to 
this message are intended
for the exclusive use of the addressee(s) and may contain confidential or 
privileged information. If
you are not the intended recipient, please notify the sender at Wipro or [EMAIL 
PROTECTED] immediately
and destroy all copies of this message and any attachments.

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




Re: Replcaing printf by print

2005-07-22 Thread Jeff 'japhy' Pinyan

On Jul 22, [EMAIL PROTECTED] said:


Does replacing printf by print make any difference in the program?
Especially at places where there is no format string passed or just $_
is passed?


It probably doesn't have a noticeable difference unless you compare the 
running of it many, MANY times.  But using printf() needlessly is silly, 
and in some cases, dangerous.  If you have a % sign in your string, printf 
will expect an argument to go with it.


--
Jeff "japhy" Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

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




Replcaing printf by print

2005-07-22 Thread pradeep.goel


Hi,

Does replacing printf by print make any difference in the program?
Especially at places where there is no format string passed or just $_
is passed?

Regards,
Pradeep



Confidentiality Notice

The information contained in this electronic message and any attachments to 
this message are intended
for the exclusive use of the addressee(s) and may contain confidential or 
privileged information. If
you are not the intended recipient, please notify the sender at Wipro or [EMAIL 
PROTECTED] immediately
and destroy all copies of this message and any attachments.

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




Re: assigning printf statement to an array

2005-06-07 Thread DBSMITH
Please help on how to use the functionality of my date_manip1 subroutine to
pass it the days old to
get "the past date dd/mm/yy" as such:

ABDELFATTAH__FAITH_M702515438:  101   dd/mm/yy


thank you,
derek


Here is my code:

use strict;
use warnings;
$ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);

my $hdir = qq(/heartlab/db studies/);
my $fdir = qq(/fuji/original/RMH/);
$^T=time;
our $now = time();
use constant ONE_DAY => 86400;




#sub date_manip1
#   {
#my $days = (shift);
#my ($d, $m, $y) = (localtime($now - ($days * ONE_DAY)))
[3..5];
#sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100));
#}

#my $dmm = &date_manip1($0);
#chomp $dm;
#chomp $dmm;



#sub secondsold
#{
opendir (HDH, $hdir) or die "unable to open directory: $hdir $!";

my ($d, $m, $y) = (localtime($now - (map {$_->[0]} *
ONE_DAY))) [3..5];
printf ("%02d/%02d/%04d", $m+1,$d,($y % 100));
grep {$_->[0] > 100}
map { [ sprintf ("%.0f",($now - ( stat( "${hdir}$_" ) )[9]
) * ONE_DAY ),

$_
] }
grep {$_ ne "." and $_ ne ".."}
sort readdir HDH;
#print @a,"\n";

closedir (HDH) or warn "unable to close HDH: $hdir $!";
#   }

___END CODE___

___BEGIN DATA___

Days old of HL image files are: 0_LEARY_ALANRA002848:   150
Days old of HL image files are: ABDELFATTAH__FAITH_M702515438:  101







   
 [EMAIL PROTECTED] 
 h.com 
To 
 05/24/2005 09:18  beginners@perl.org  
 PM cc 
   
       Subject 
   Re: assigning printf statement to   
   an array
   
   
   
   
   
   




operdir .

1> print
2> map {$_->[1].": ".$_->[0]."\n"} # create entity output
3> grep {$_->[0] > 100} # exclude age <= 100
4> map { [
5>  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
6>  $_
7> ] } # create aref [age, filename]
8> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
9> sort readdir DH; # get all files

closedir ...


In the block of code  :

   how does the perl compiler load this, from line 9 up or opendir down?
   And what is the correct way to read it from top down or bottom up?
   what is the purpose of an array reference in this code and what is it
   advantages?
   on pg 253 of the camel book it states "if the next thing after the -> is
   a bracket, then the left operand ($_ ) is treated as a reference to an
   array to be subscripted on the right [0] or [1] .  So is this array
   declared/created dynamically?  How?
   on line 2 why is the period after [1] needed and what does it represent
   , string concatenation?  If so why are multiple ones needed on line 2?
   so [1] contains the dir name and [0] contains the my days old
   calculation, correct?
   how does map know to create this array b/c I thought map expects a list
   that is already created?
   from the previous question... is this list the opendir?  I guess am not
   totally grasping the map function.
   in lines 5 and 6 $_ is the dir, but why is it needed at  the very end at
   line 6 after the comma?
   to the end, output of days old > 100 I am now trying to use my
   date_manip1 subroutine to pass it the days old to get "the past date
   dd/mm/yy" as such:
  line 3 turns into &date_manip1(grep {$_->[0] > 100} ) so I have
output as :

ABDELFATTAH__FAITH_M702515438:  101   dd/mm/yy
I am on the right track?

thank you,

derek



 John Doe
   To
     

Re: assigning printf statement to an array

2005-05-26 Thread DBSMITH
DITTO!


THANKS Men!

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams




   
 John Doe  
   To 
   beginners@perl.org  
 05/25/2005 04:48   cc 
 PM
   Subject 
   Re: assigning printf statement to   
   an array
   
   
   
   
   
   




Am Mittwoch, 25. Mai 2005 04.10 schrieb Jeff 'japhy' Pinyan:

[...]

Jeff, thanks a lot for taking over.
My explanation would not have been so understandable and with this
fullness.

joe

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





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




Re: assigning printf statement to an array

2005-05-25 Thread Jeff 'japhy' Pinyan

On May 25, John Doe said:


Am Mittwoch, 25. Mai 2005 04.10 schrieb Jeff 'japhy' Pinyan:

Jeff, thanks a lot for taking over.
My explanation would not have been so understandable and with this fullness.


Glad to be of service.

--
Jeff "japhy" Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

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




Re: assigning printf statement to an array

2005-05-25 Thread John Doe
Am Mittwoch, 25. Mai 2005 04.10 schrieb Jeff 'japhy' Pinyan:

[...]

Jeff, thanks a lot for taking over. 
My explanation would not have been so understandable and with this fullness.

joe

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




Re: assigning printf statement to an array

2005-05-24 Thread Jeff 'japhy' Pinyan

On May 24, [EMAIL PROTECTED] said:


1> print
2> map {$_->[1].": ".$_->[0]."\n"} # create entity output
3> grep {$_->[0] > 100} # exclude age <= 100
4> map { [
5>  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
6>  $_
7> ] } # create aref [age, filename]
8> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
9> sort readdir DH; # get all files



  how does the perl compiler load this, from line 9 up or opendir down?


Well, Perl reads it left-to-right.  It just happens that it's a bunch of 
functions whose arguments are the return values of other functions.  It's 
the same as


  print get_output(
get_old_ones(
  make_age_filename(
exclude_dot_and_dotdot(
  sort(
readdir(DIR)
  )
)
  )
)
  );

where those functions do what you have above.


  And what is the correct way to read it from top down or bottom up?


Humans should probably read it from the bottom up.  The raw data are the 
arguments to the right-most function, and then it goes through many 
filters until it is in its final form and printed.



  what is the purpose of an array reference in this code and what is it
  advantages?


The array reference is just a way of holding two pieces of data at once.


  on pg 253 of the camel book it states "if the next thing after the -> is
  a bracket, then the left operand ($_ ) is treated as a reference to an
  array to be subscripted on the right [0] or [1] .  So is this array
  declared/created dynamically?  How?


The array reference is created in lines 4-7.


  on line 2 why is the period after [1] needed and what does it represent
  , string concatenation?  If so why are multiple ones needed on line 2?


That's the '.' operator, used for string concatenation.

  $_->[1] . ": " . $_->[0] . "\n"

could just be written as

  "$_->[1]: $_->[0]\n"


  so [1] contains the dir name and [0] contains the my days old
  calculation, correct?


Yes.


  how does map know to create this array b/c I thought map expects a list
  that is already created?


map() takes a list and returns a list.  The elements of the list it 
receives are filenames, the elements of the list it returns are array 
REFERENCES.



  from the previous question... is this list the opendir?  I guess am not
  totally grasping the map function.


Then you should read 'perldoc -f map'.  map() is like a for loop that 
returns a list.


  @out = map { ...code... } @in;

is like:

  @out = ();
  for (@in) {
push @out, ...code...;
  }

Here's a simple example:

  @even_numbers = map { $_ * 2 } (1 .. 10);

That creates @even_numbers = (2, 4, 6, 8, 10, 12, 14, 16, 18, 20).  The 
same result can be gotten with a for loop:


  @even_numbers = ();
  for (1 .. 10) {
push @even_numbers, $_ * 2;
  }


  in lines 5 and 6 $_ is the dir, but why is it needed at  the very end at
  line 6 after the comma?


Because lines 4-7 are creating an array reference.  The first element is 
the age of the file, and the second element is the file, $_.


  map { [ get_age($_), $_ ] } ...

The moral of the story is, you can do things all at once using map() and 
grep(), or you can do them piecemeal with for loops:



1> print
2> map {$_->[1].": ".$_->[0]."\n"} # create entity output
3> grep {$_->[0] > 100} # exclude age <= 100
4> map { [
5>  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
6>  $_
7> ] } # create aref [age, filename]
8> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
9> sort readdir DH; # get all files


Could be:

  # line 9
  my @all_files = sort readdir DH;

  # line 8
  my @files;
  for (@all_files) { push @files, $_ if $_ ne "." and $_ ne ".." }

  # lines 4-7
  my @files_and_ages;
  for (@files) {
push @files_and_ages, [
  sprintf(...), $_
];
  }

  # line 3
  my @old_files;
  for (@files_and_ages) {
push @old_files, $_ if $_->[0] > 100;
  }

  # line 2
  my @strings;
  for (@old_files) {
push @strings, "$_->[1]: $_->[0]\n";
  }

  # line 1
  print @strings;

--
Jeff "japhy" Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

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




Re: assigning printf statement to an array

2005-05-24 Thread DBSMITH
operdir .

1> print
2> map {$_->[1].": ".$_->[0]."\n"} # create entity output
3> grep {$_->[0] > 100} # exclude age <= 100
4> map { [
5>  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
6>  $_
7> ] } # create aref [age, filename]
8> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
9> sort readdir DH; # get all files

closedir ...


In the block of code  :

   how does the perl compiler load this, from line 9 up or opendir down?
   And what is the correct way to read it from top down or bottom up?
   what is the purpose of an array reference in this code and what is it
   advantages?
   on pg 253 of the camel book it states "if the next thing after the -> is
   a bracket, then the left operand ($_ ) is treated as a reference to an
   array to be subscripted on the right [0] or [1] .  So is this array
   declared/created dynamically?  How?
   on line 2 why is the period after [1] needed and what does it represent
   , string concatenation?  If so why are multiple ones needed on line 2?
   so [1] contains the dir name and [0] contains the my days old
   calculation, correct?
   how does map know to create this array b/c I thought map expects a list
   that is already created?
   from the previous question... is this list the opendir?  I guess am not
   totally grasping the map function.
   in lines 5 and 6 $_ is the dir, but why is it needed at  the very end at
   line 6 after the comma?
   to the end, output of days old > 100 I am now trying to use my
   date_manip1 subroutine to pass it the days old to get "the past date
   dd/mm/yy" as such:
  line 3 turns into &date_manip1(grep {$_->[0] > 100} ) so I have
output as :

ABDELFATTAH__FAITH_M702515438:  101   dd/mm/yy
I am on the right track?

thank you,

derek


   
 John Doe  
   To 
   beginners@perl.org  
 05/24/2005 03:27   cc 
 PM    
   Subject 
   Re: assigning printf statement to   
   an array
   
   
   
   
   
   




Hi

Am Dienstag, 24. Mai 2005 18.05 schrieb [EMAIL PROTECTED]
with funny quoting:

[John:]
> my @a=
> map {$hdir.$_->[1]} # select filename
> grep {$_->[0] > 100} # exclude age <= 100
> map { [
>   sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
>   $_
>  ] } # create aref [age, filename]
> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
> sort readdir DH; # get all files
>
>
> The map-grep-map-construct is quite common for problems like yours
> ("But I cannot use $_ b/c $_ is the dir name.").


[Derek:]
> This is working but it not printing the calculation "days old" for each
> filename???

No, obviously not... I wanted to demonstrate the principle where
some kind of additional attributes (here: age) of a list of entities
(files)
is created on the fly and kept together (in an aref), and taken as base for

further treatment (selection). [ Hope this english is understandable :-) ]

> I want to print the aref.
> I verified that it is indeed only printing those files over 100 days old,
> but I need output such as,
>
> ABDELFATTAH__FAITH_M702515438:  101
>
> as opposed to
>
> ABDELFATTAH__FAITH_M702515438

Ok, then some minimal changes in the first two lines are necessary
(although
there are other possibilities to do what you want; my proposal is possibliy

not the most performant, but splits the problem into smaller parts that can

be chaned)
[also untested...]

print
map {$_->[1].": ".$_->[0]."\n"} # create entity output
grep {$_->[0] > 100} # exclude age <= 100
map { [
  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
  $_
 ] } # create aref [age, filename]
grep {$_ ne "." and $_ ne ".."} # exclude unwanted
sort readdir DH; # get all files

> I will 

Re: assigning printf statement to an array

2005-05-24 Thread John Doe
Hi

Am Dienstag, 24. Mai 2005 18.05 schrieb [EMAIL PROTECTED] 
with funny quoting:

[John:]
> my @a=
> map {$hdir.$_->[1]} # select filename
> grep {$_->[0] > 100} # exclude age <= 100
> map { [
>   sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
>   $_
>  ] } # create aref [age, filename]
> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
> sort readdir DH; # get all files
>
>
> The map-grep-map-construct is quite common for problems like yours
> ("But I cannot use $_ b/c $_ is the dir name.").


[Derek:]
> This is working but it not printing the calculation "days old" for each
> filename???  

No, obviously not... I wanted to demonstrate the principle where 
some kind of additional attributes (here: age) of a list of entities (files) 
is created on the fly and kept together (in an aref), and taken as base for 
further treatment (selection). [ Hope this english is understandable :-) ]

> I want to print the aref. 
> I verified that it is indeed only printing those files over 100 days old,
> but I need output such as,
>
> ABDELFATTAH__FAITH_M702515438:  101
>
> as opposed to
>
> ABDELFATTAH__FAITH_M702515438

Ok, then some minimal changes in the first two lines are necessary (although 
there are other possibilities to do what you want; my proposal is possibliy 
not the most performant, but splits the problem into smaller parts that can 
be chaned)
[also untested...]

print
map {$_->[1].": ".$_->[0]."\n"} # create entity output
grep {$_->[0] > 100} # exclude age <= 100
map { [
  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
  $_
 ] } # create aref [age, filename]
grep {$_ ne "." and $_ ne ".."} # exclude unwanted
sort readdir DH; # get all files

> I will also follow up with a line by line question of the code.

Just go on, questions are half of the answers :-)

joe


[...]
[maybe I should have snipped the rest?]
> Am Montag, 23. Mai 2005 20.05 schrieb [EMAIL PROTECTED]:
> > Hi all.
> >
> > This is a continuation from my original question, but with a twist.  I am
> > now using sprintf to assign to an array, but I only want to assign to
>
> this
>
> > array of I find a number over 100 from my calculation.  In the array/hash
> > also needs to be the directory name.  Here is my code and the sample
> > output:
> > So in the end my goal is to store the data in this array if the number is
> > over 100 and if it is over 100 store the directory name and its
>
> associative
>
> > "days old calc."
> > I have tried my @a =(); my $e =0;
> > $a[$e++] = sprintf ("%.0f",( $now - ( stat( "${hdir}${file}" ) )[9] ) /
> > ONE_DAY ) if ??? > 100;
> >
> > But I cannot use $_ b/c $_ is the dir name.
> >
> > thank you,
> >
> > derek
> >
> >
> >
> >
> > use strict;
> > use warnings;
> > $ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
> >
> > my $hdir   = qq(/heartlab/db studies/);
> > #my $fdirs =
> > qq(/fuji/original/RMH/,/fuji/original/GMC/,/fuji/clinical/GMC/,/fuj
> > i/clinical/RMH/);
> > $^T=time;
> > our $now = time();
> > use constant ONE_DAY => 86400;
> >
> >
> > sub date_manip
> > {
> > my ($month, $day, $year) = (localtime)[4,3,5];
> > sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year %
>
> 100));
>
> > }
> >
> > my $dm = &date_manip;
> >
> > sub date_manip1
> > {
> > my $days = (shift);
> > my ($d, $m, $y) = (localtime($now - $days * ONE_DAY))
> > [3..5];
> > sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100));
> > }
> >
> > #my $dmm = &date_manip1(1);
> > #chomp $dm;
> > #chomp $dmm;
> >
> >
> >
> > opendir (DH, $hdir) or die "unable to open directory: $hdir $!";
> >
> > my @a =();
> > my $e =0;
> > foreach my $file (sort readdir (DH))
> > {
> > next if $file eq "." or $file eq "..";
> > print "\n";
> > print "Days old of HL image files are:\t",$file,":\t";
> > #printf ("%.0f", ( $now - ( stat( "${hdir}${file}" ) )[9]
>
> )
>
> > / ONE_DAY );
> >   

Fw: assigning printf statement to an array

2005-05-24 Thread DBSMITH
- Forwarded by Derek Smith/Staff/OhioHealth on 05/24/2005 03:47 PM
-
   
 Derek 
 Smith/Staff/OhioH 
 ealth  To 
   John Doe
 05/24/2005 12:05  <[EMAIL PROTECTED]>  
 PM cc 
   beginners@perl.org  
   Subject 
   Re: assigning printf statement to   
   an array(Document link: Derek   
   Bellner Smith)  
   
   
   
   
   
   



my @a=
map {$hdir.$_->[1]} # select filename
grep {$_->[0] > 100} # exclude age <= 100
map { [
  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
  $_
 ] } # create aref [age, filename]
grep {$_ ne "." and $_ ne ".."} # exclude unwanted
sort readdir DH; # get all files


The map-grep-map-construct is quite common for problems like yours
("But I cannot use $_ b/c $_ is the dir name.").



This is working but it not printing the calculation "days old" for each
filename???  I want to print the aref.
I verified that it is indeed only printing those files over 100 days old,
but I need output such as,

ABDELFATTAH__FAITH_M702515438:  101

as opposed to

ABDELFATTAH__FAITH_M702515438

I will also follow up with a line by line question of the code.
thanks again!
derek




Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams




   
 John Doe  
   To 
   beginners@perl.org  
 05/24/2005 01:34   cc 
 AM
       Subject 
   Re: assigning printf statement to   
   an array
   
   
   
   
   
   




Am Montag, 23. Mai 2005 20.05 schrieb [EMAIL PROTECTED]:
> Hi all.
>
> This is a continuation from my original question, but with a twist.  I am
> now using sprintf to assign to an array, but I only want to assign to
this
> array of I find a number over 100 from my calculation.  In the array/hash
> also needs to be the directory name.  Here is my code and the sample
> output:
> So in the end my goal is to store the data in this array if the number is
> over 100 and if it is over 100 store the directory name and its
associative
> "days old calc."
> I have tried my @a =(); my $e =0;
> $a[$e++] = sprintf ("%.0f",( $now - ( stat( "${hdir}${file}" ) )[9] ) /
> ONE_DAY ) if ??? > 100;
>
> But I cannot use $_ b/c $_ is the dir name.
>
> thank you,
>
> derek
>
>
>
>
> use strict;
> use warnings;
> $ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
>
> my $hdir   = qq(/heartlab/db studies/);
> #my $fdirs =
> qq(/fuji/original/RMH/,/fuji/original/GMC/,/fuji/clinical/GMC/,/fuj
> i/clinical/RMH/);
> $^T=time;
> our $now = time();
> use constant ONE_DAY => 86400;
>
>
> sub date_manip
> {
> my ($month, $day, $year) = (localtime)[4,3,5];
> sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year %
100));
> }
>
> my $dm = &dat

Re: assigning printf statement to an array

2005-05-24 Thread DBSMITH
my @a=
map {$hdir.$_->[1]} # select filename
grep {$_->[0] > 100} # exclude age <= 100
map { [
  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
  $_
 ] } # create aref [age, filename]
grep {$_ ne "." and $_ ne ".."} # exclude unwanted
sort readdir DH; # get all files


The map-grep-map-construct is quite common for problems like yours
("But I cannot use $_ b/c $_ is the dir name.").



This is working but it not printing the calculation "days old" for each
filename???  I want to print the aref.
I verified that it is indeed only printing those files over 100 days old,
but I need output such as,

ABDELFATTAH__FAITH_M702515438:  101

as opposed to

ABDELFATTAH__FAITH_M702515438

I will also follow up with a line by line question of the code.
thanks again!
derek




Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams




   
 John Doe  
   To 
   beginners@perl.org  
 05/24/2005 01:34   cc 
 AM
   Subject 
   Re: assigning printf statement to   
   an array
   
   
   
   
   
   




Am Montag, 23. Mai 2005 20.05 schrieb [EMAIL PROTECTED]:
> Hi all.
>
> This is a continuation from my original question, but with a twist.  I am
> now using sprintf to assign to an array, but I only want to assign to
this
> array of I find a number over 100 from my calculation.  In the array/hash
> also needs to be the directory name.  Here is my code and the sample
> output:
> So in the end my goal is to store the data in this array if the number is
> over 100 and if it is over 100 store the directory name and its
associative
> "days old calc."
> I have tried my @a =(); my $e =0;
> $a[$e++] = sprintf ("%.0f",( $now - ( stat( "${hdir}${file}" ) )[9] ) /
> ONE_DAY ) if ??? > 100;
>
> But I cannot use $_ b/c $_ is the dir name.
>
> thank you,
>
> derek
>
>
>
>
> use strict;
> use warnings;
> $ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
>
> my $hdir   = qq(/heartlab/db studies/);
> #my $fdirs =
> qq(/fuji/original/RMH/,/fuji/original/GMC/,/fuji/clinical/GMC/,/fuj
> i/clinical/RMH/);
> $^T=time;
> our $now = time();
> use constant ONE_DAY => 86400;
>
>
> sub date_manip
> {
> my ($month, $day, $year) = (localtime)[4,3,5];
> sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year %
100));
> }
>
> my $dm = &date_manip;
>
> sub date_manip1
> {
> my $days = (shift);
> my ($d, $m, $y) = (localtime($now - $days * ONE_DAY))
> [3..5];
> sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100));
> }
>
> #my $dmm = &date_manip1(1);
> #chomp $dm;
> #chomp $dmm;
>
>
>
> opendir (DH, $hdir) or die "unable to open directory: $hdir $!";
>
> my @a =();
> my $e =0;
> foreach my $file (sort readdir (DH))
> {
> next if $file eq "." or $file eq "..";
> print "\n";
> print "Days old of HL image files are:\t",$file,":\t";
> #printf ("%.0f", ( $now - ( stat( "${hdir}${file}" ) )[9]
)
> / ONE_DAY );
> $a[$e++] = sprintf ("%.0f",( $now - ( stat(
> "${hdir}${file}" ) )[9] ) / ONE_DAY );
> #date_manip1($1);
> }


instead of the foreach loop you could use a single statement [untested]:
(read from bottom to top)

my @a=
map {$hdir.$_->[1]} # select filename
grep {$_->[0] > 100} # exclude age <= 100
map { [
  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),

Re: assigning printf statement to an array

2005-05-23 Thread John Doe
Am Montag, 23. Mai 2005 20.05 schrieb [EMAIL PROTECTED]:
> Hi all.
>
> This is a continuation from my original question, but with a twist.  I am
> now using sprintf to assign to an array, but I only want to assign to this
> array of I find a number over 100 from my calculation.  In the array/hash
> also needs to be the directory name.  Here is my code and the sample
> output:
> So in the end my goal is to store the data in this array if the number is
> over 100 and if it is over 100 store the directory name and its associative
> "days old calc."
> I have tried my @a =(); my $e =0;
> $a[$e++] = sprintf ("%.0f",( $now - ( stat( "${hdir}${file}" ) )[9] ) /
> ONE_DAY ) if ??? > 100;
>
> But I cannot use $_ b/c $_ is the dir name.
>
> thank you,
>
> derek
>
>
>
>
> use strict;
> use warnings;
> $ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
>
> my $hdir   = qq(/heartlab/db studies/);
> #my $fdirs =
> qq(/fuji/original/RMH/,/fuji/original/GMC/,/fuji/clinical/GMC/,/fuj
> i/clinical/RMH/);
> $^T=time;
> our $now = time();
> use constant ONE_DAY => 86400;
>
>
> sub date_manip
> {
> my ($month, $day, $year) = (localtime)[4,3,5];
> sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year % 100));
> }
>
> my $dm = &date_manip;
>
> sub date_manip1
> {
> my $days = (shift);
> my ($d, $m, $y) = (localtime($now - $days * ONE_DAY))
> [3..5];
> sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100));
> }
>
> #my $dmm = &date_manip1(1);
> #chomp $dm;
> #chomp $dmm;
>
>
>
> opendir (DH, $hdir) or die "unable to open directory: $hdir $!";
>
> my @a =();
> my $e =0;
> foreach my $file (sort readdir (DH))
> {
> next if $file eq "." or $file eq "..";
> print "\n";
> print "Days old of HL image files are:\t",$file,":\t";
> #printf ("%.0f", ( $now - ( stat( "${hdir}${file}" ) )[9] )
> / ONE_DAY );
> $a[$e++] = sprintf ("%.0f",( $now - ( stat(
> "${hdir}${file}" ) )[9] ) / ONE_DAY );
> #date_manip1($1);
> }


instead of the foreach loop you could use a single statement [untested]:
(read from bottom to top)

my @a=
map {$hdir.$_->[1]} # select filename
grep {$_->[0] > 100} # exclude age <= 100
map { [
  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ), 
  $_
 ] } # create aref [age, filename]
grep {$_ ne "." and $_ ne ".."} # exclude unwanted
sort readdir DH; # get all files


The map-grep-map-construct is quite common for problems like yours
("But I cannot use $_ b/c $_ is the dir name.").

(Schwarz'sche Transformation in german).


hth, joe


> closedir (DH) or warn "unable to close DH: $hdir $!;"
>
> ___END CODE___
>
> ___BEGIN_DATA___
>
> Days old of HL image files are: 0_LEARY_ALANRA002848:   68
> Days old of HL image files are: AARON_YAZAWA_CHRISTEHD006871:   48
> Days old of HL image files are: AARON_YAZAWA_CHRISTERF015357:   64
> Days old of HL image files are: ABBOTT_ANNA_MAERF014496:49
> Days old of HL image files are: ABBOTT_CONSTANCEMK003126:   69
> Days old of HL image files are: ABBOTT_DONALDHE011252:  67
> Days old of HL image files are: ABBOTT_FLORENCE068148_15857:20
> Days old of HL image files are: ABBOTT_HARRYRA002652:   68
> Days old of HL image files are: ABBOTT_THOMASMK61:  66
> Days old of HL image files are: ABBOTT__FLORENCEHC008241:   20
> Days old of HL image files are: ABBRUZZESE_BETTYMK004528:   78
> Days old of HL image files are: ABBRUZZESE_BETTYML002169:   80
> Days old of HL image files are: ABDALLA__PATRICIAHA014365:  56
> Days old of HL image files are: ABDELFATTAH_FAITH0702515438:98
> Days old of HL image files are: ABDELFATTAH__FAITH_M702515438:  101
> Days old of HL image files are: ABDULLAHI__ABDIRAHMAMJ003339:   62
> Days old of HL image files are: ABELL_MARKRF015325: 66
> Days old of HL image files are: ABEL_HUBERTMJ003654:21
> Days old of HL image files are: ABEL_RICHARDMK003922:   82
> Days old of HL image files are: ABEL_STEVENHD007640:75
> Days old of HL image files are: ABEL_STEVENRA003253:75
> Days old of HL image files are: ABEL__LARRYRF013486:73
>
>
>
>
> Derek B. Smith
> OhioHealth IT
> UNIX / TSM / EDM Teams

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




Re: assigning printf statement to an array

2005-05-23 Thread DBSMITH
Hi all.

This is a continuation from my original question, but with a twist.  I am
now using sprintf to assign to an array, but I only want to assign to this
array of I find a number over 100 from my calculation.  In the array/hash
also needs to be the directory name.  Here is my code and the sample
output:
So in the end my goal is to store the data in this array if the number is
over 100 and if it is over 100 store the directory name and its associative
"days old calc."
I have tried my @a =(); my $e =0;
$a[$e++] = sprintf ("%.0f",( $now - ( stat( "${hdir}${file}" ) )[9] ) /
ONE_DAY ) if ??? > 100;

But I cannot use $_ b/c $_ is the dir name.


thank you,

derek




use strict;
use warnings;
$ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);

my $hdir   = qq(/heartlab/db studies/);
#my $fdirs =
qq(/fuji/original/RMH/,/fuji/original/GMC/,/fuji/clinical/GMC/,/fuj
i/clinical/RMH/);
$^T=time;
our $now = time();
use constant ONE_DAY => 86400;


sub date_manip
{
my ($month, $day, $year) = (localtime)[4,3,5];
sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year % 100));
}

my $dm = &date_manip;

sub date_manip1
{
my $days = (shift);
my ($d, $m, $y) = (localtime($now - $days * ONE_DAY))
[3..5];
sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100));
}

#my $dmm = &date_manip1(1);
#chomp $dm;
#chomp $dmm;



opendir (DH, $hdir) or die "unable to open directory: $hdir $!";

my @a =();
my $e =0;
foreach my $file (sort readdir (DH))
{
next if $file eq "." or $file eq "..";
print "\n";
print "Days old of HL image files are:\t",$file,":\t";
#printf ("%.0f", ( $now - ( stat( "${hdir}${file}" ) )[9] )
/ ONE_DAY );
$a[$e++] = sprintf ("%.0f",( $now - ( stat(
"${hdir}${file}" ) )[9] ) / ONE_DAY );
#date_manip1($1);
}
closedir (DH) or warn "unable to close DH: $hdir $!;"

___END CODE___

___BEGIN_DATA___

Days old of HL image files are: 0_LEARY_ALANRA002848:   68
Days old of HL image files are: AARON_YAZAWA_CHRISTEHD006871:   48
Days old of HL image files are: AARON_YAZAWA_CHRISTERF015357:   64
Days old of HL image files are: ABBOTT_ANNA_MAERF014496:49
Days old of HL image files are: ABBOTT_CONSTANCEMK003126:   69
Days old of HL image files are: ABBOTT_DONALDHE011252:  67
Days old of HL image files are: ABBOTT_FLORENCE068148_15857:20
Days old of HL image files are: ABBOTT_HARRYRA002652:   68
Days old of HL image files are: ABBOTT_THOMASMK61:  66
Days old of HL image files are: ABBOTT__FLORENCEHC008241:   20
Days old of HL image files are: ABBRUZZESE_BETTYMK004528:   78
Days old of HL image files are: ABBRUZZESE_BETTYML002169:   80
Days old of HL image files are: ABDALLA__PATRICIAHA014365:  56
Days old of HL image files are: ABDELFATTAH_FAITH0702515438:98
Days old of HL image files are: ABDELFATTAH__FAITH_M702515438:  101
Days old of HL image files are: ABDULLAHI__ABDIRAHMAMJ003339:   62
Days old of HL image files are: ABELL_MARKRF015325: 66
Days old of HL image files are: ABEL_HUBERTMJ003654:21
Days old of HL image files are: ABEL_RICHARDMK003922:   82
Days old of HL image files are: ABEL_STEVENHD007640:75
Days old of HL image files are: ABEL_STEVENRA003253:75
Days old of HL image files are: ABEL__LARRYRF013486:73




Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams



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




Re: assigning printf statement to an array

2005-05-21 Thread Offer Kaye
On 5/21/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Here is my code, but I am not seeing the elements being populated as I say
> show me the data in element 1
> Line 28 is not working after executing line 30.
> Any ideas?
> 

You probably want to use "sprintf" instead of "printf". Read "perldoc
-f printf" and "perldoc -f sprintf" from your command-line, or online:
http://perldoc.perl.org/functions/printf.html
http://perldoc.perl.org/functions/sprintf.html

HTH,
-- 
Offer Kaye

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




Re: using printf on specified fields of record

2004-11-17 Thread Joseph Paish
On Wednesday 17 November 2004 09:52, Bob Showalter wrote:
> Joseph Paish wrote:
> > following is a sample of a record generated by a perl script i have
> > written ( i have put each field on a separate line in this email to
> > prevent word-wrapping) :
> >
> >
> > 12/05/2003
> > sold
> > 8000
> > widget1
> > 0.055
> > 29.95
> > 30500
> > 0
> > 410.05
> > -173.655333717579 << change to 2 decimal places displayed
> > 2225.37658479827  << change to 2 decimal places displayed
> > 0.0729631667146973  << change to 4 decimal places displayed
> > -173.655333717579 << change to 2 decimal places displayed
> > sample_note_here
> >
> >
> > the first 9 fields and the last field are output just the way i want
> > them using a simple "print" statement, but the 4 long ones near the
> > end are not.
> >
> > is there some way of using printf formatting on only certain fields
> > to modify the way that they are displayed and leave the others alone,
> > since the way they are displayed is ok already?
>
> Use sprintf() on those elements you want specially formatted.
>
> So instead of
>
>print $x, $y, $z;
>
> You can use
>
>print $x, $y, sprintf('%.02f', $z)
>
> This will print $z with two places after the decimal, but leave $x and $y
> alone.

it worked!!

thanks

joe


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




RE: using printf on specified fields of record

2004-11-17 Thread Bob Showalter
Joseph Paish wrote:
> following is a sample of a record generated by a perl script i have
> written ( i have put each field on a separate line in this email to
> prevent word-wrapping) :
> 
> 
> 12/05/2003
> sold
> 8000
> widget1
> 0.055
> 29.95
> 30500
> 0
> 410.05
> -173.655333717579 << change to 2 decimal places displayed
> 2225.37658479827  << change to 2 decimal places displayed
> 0.0729631667146973  << change to 4 decimal places displayed
> -173.655333717579 << change to 2 decimal places displayed
> sample_note_here
> 
> 
> the first 9 fields and the last field are output just the way i want
> them using a simple "print" statement, but the 4 long ones near the
> end are not. 
> 
> is there some way of using printf formatting on only certain fields
> to modify the way that they are displayed and leave the others alone,
> since the way they are displayed is ok already?

Use sprintf() on those elements you want specially formatted.

So instead of

   print $x, $y, $z;

You can use

   print $x, $y, sprintf('%.02f', $z)

This will print $z with two places after the decimal, but leave $x and $y
alone.

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




using printf on specified fields of record

2004-11-17 Thread Joseph Paish
following is a sample of a record generated by a perl script i have written ( 
i have put each field on a separate line in this email to prevent 
word-wrapping) :


12/05/2003 
sold 
8000 
widget1 
0.055 
29.95 
30500 
0 
410.05 
-173.655333717579 << change to 2 decimal places displayed 
2225.37658479827  << change to 2 decimal places displayed
0.0729631667146973  << change to 4 decimal places displayed
-173.655333717579 << change to 2 decimal places displayed
sample_note_here


the first 9 fields and the last field are output just the way i want them 
using a simple "print" statement, but the 4 long ones near the end are not. 

is there some way of using printf formatting on only certain fields to modify 
the way that they are displayed and leave the others alone, since the way 
they are displayed is ok already?

thanks

joe


 

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




Re: problem printf

2004-10-26 Thread deny

Jenda
P.S.: What the heck is find.pl? You should be using File::Find!
 

thanks for your help
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: problem printf

2004-10-26 Thread Flemming Greve Skovengaard
deny wrote:

Try to add this lines somewhere the top of the file:
use strict;
use warnings;
and report the errors, if any.
Global symbol "$md5" requires explicit package name at ./checksum.pl 
line 11.
Global symbol "@dirs" requires explicit package name at ./checksum.pl 
line 12.
Global symbol "$dir" requires explicit package name at ./checksum.pl 
line 14.


Good, now you're on the right track. Declare those variables ( with my ) and
read the reply by John W. Krahn.
--
Flemming Greve SkovengaardThe prophecy of the holy Norns
a.k.a Greven, TuxPowerA tale of death and doom
<[EMAIL PROTECTED]>   Odin saw the final sign
4112.38 BogoMIPS  The end is coming soon
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: problem printf

2004-10-25 Thread deny

Try to add this lines somewhere the top of the file:
use strict;
use warnings;
and report the errors, if any.
Global symbol "$md5" requires explicit package name at ./checksum.pl 
line 11.
Global symbol "@dirs" requires explicit package name at ./checksum.pl 
line 12.
Global symbol "$dir" requires explicit package name at ./checksum.pl 
line 14.

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



Re: problem printf

2004-10-25 Thread deny
Owen a écrit :
On Mon, 25 Oct 2004 05:17 pm, deny wrote:
 

foreach $name ( sort @files) {
   ($uid,$gid) = (stat $nane)[4,5];
   

stat $nane or $name?
 

$name ,
error for me ,the result is the same
if someone want the script for test
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: problem printf

2004-10-25 Thread John W. Krahn
deny wrote:

   That isn't only perl code, it's incomplete, and it
doesn't use 'printf'.  Show us more code and explain
your problem fully.
thanks for your help
here is the complete code
#!/usr/bin/perl  
use warnings;
use strict;
use MD5;
use Digest::MD5;
require 'find.pl';
use File::Find;
$md5 = new MD5;
my $md5 = Digest::MD5->new;
@dirs = @ARGV;
foreach $dir ( @dirs ) { find($dir); }
my @files;
find( \&wanted, @ARGV );
sub wanted { push @files, $name; }
You need to push the complete path into the array:
sub wanted { push @files, $File::Find::name }
foreach $name ( sort @files) {
   ($uid,$gid) = (stat $nane)[4,5];
 my ( $uid, $gid ) = ( stat $name )[ 4, 5 ];
   $stat = sprintf "%0o", (stat_)[2];
 ^
If you had strictures enabled (use strict) then perl would have informed you 
of this error and refused to compile your code.

 my $stat = sprintf '%o', (stat _)[2];
   unless( -f $name ) {
 unless( -f _ ) {
   printf "$stat\t$uid $gid\t\t\t\t\t\t$name\n";
 print "$stat\t$uid $gid\t\t\t\t\t\t$name\n";
   next;
   }
 $md5->reset();
   open FILE, $name or print(STDERR "can't open file $name\n"), next;
You should include the $! variable in the error message so you know *why* it 
failed.

   $md5->addfile(FILE);
   close FILE;
 $checksum - $md5->hexdigest();
   printf "$stat\t$uid $gid $checksum\t$name\n";
 print "$stat\t$uid $gid $checksum\t$name\n";
}  

John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



Re: problem printf

2004-10-25 Thread Jenda Krynicky
From: deny <[EMAIL PROTECTED]>
> here is the complete code
> 
> #!/usr/bin/perl   
> 
> use MD5;
> require 'find.pl';
> 
> $md5 = new MD5;
> @dirs = @ARGV;
> 
> foreach $dir ( @dirs ) { find($dir); }
> sub wanted { push @files, $name; }
> 
> foreach $name ( sort @files) {
> ($uid,$gid) = (stat $nane)[4,5];
> $stat = sprintf "%0o", (stat_)[2];
> unless( -f $name ) {
> printf "$stat\t$uid $gid\t\t\t\t\t\t$name\n";

You should either be using print():

print "$stat\t$uid $gid\t\t\t\t\t\t$name\n";

or use printf() the way it was meant tu be. The first argument to 
printf should be "format", then you should specify the variables:

printf "%d\t%d %d\t\t\t\t\t\t%s\n", $stat, $uid, $gid, $name; 

I think the other problem is that you only store the names of the 
files, not paths in the @files array. So stat() is not able to find 
the files.

If would probably be best to do the stat() and print the result 
directly from the wanted() subroutine.

Jenda
P.S.: What the heck is find.pl? You should be using File::Find!

= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


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




Re: problem printf

2004-10-25 Thread Flemming Greve Skovengaard
deny wrote:

   That isn't only perl code, it's incomplete, and it
doesn't use 'printf'.  Show us more code and explain
your problem fully.
 

thanks for your help
here is the complete code
#!/usr/bin/perl  
use MD5;
require 'find.pl';

$md5 = new MD5;
@dirs = @ARGV;
foreach $dir ( @dirs ) { find($dir); }
sub wanted { push @files, $name; }
foreach $name ( sort @files) {
   ($uid,$gid) = (stat $nane)[4,5];
   $stat = sprintf "%0o", (stat_)[2];
   unless( -f $name ) {
   printf "$stat\t$uid $gid\t\t\t\t\t\t$name\n";
   next;
   }
 $md5->reset();
   open FILE, $name or print(STDERR "can't open file $name\n"), next;
   $md5->addfile(FILE);
   close FILE;
 $checksum - $md5->hexdigest();
   printf "$stat\t$uid $gid $checksum\t$name\n";
}  

it aim to calcute sum permission on the dir in @ARGV;
and with diff ,you can see  if files was modified
but the result isnt fine as you can see below
[EMAIL PROTECTED] cgi-bin]$ ./checksum.pl /bin
0   /bin
0   /bin/arch
0   /bin/awk
0   /bin/basename
0   /bin/bash
0   /bin/bash2
0   /bin/cat
0   /bin/chgrp
0   /bin/chmod
thanks

Try to add this lines somewhere the top of the file:
use strict;
use warnings;
and report the errors, if any.
--
Flemming Greve Skovengaard   Just a few small tears between
a.k.a Greven, TuxPower   Someone happy and one sad
<[EMAIL PROTECTED]>  Just a thin line drawn between
4112.38 BogoMIPS Being a genius or insane
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



Re: problem printf

2004-10-25 Thread Owen
On Mon, 25 Oct 2004 05:17 pm, deny wrote:
> foreach $name ( sort @files) {
>     ($uid,$gid) = (stat $nane)[4,5];

stat $nane or $name?

Owen

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




Re: problem printf

2004-10-25 Thread deny

   That isn't only perl code, it's incomplete, and it
doesn't use 'printf'.  Show us more code and explain
your problem fully.
 

thanks for your help
here is the complete code
#!/usr/bin/perl   

use MD5;
require 'find.pl';
$md5 = new MD5;
@dirs = @ARGV;
foreach $dir ( @dirs ) { find($dir); }
sub wanted { push @files, $name; }
foreach $name ( sort @files) {
   ($uid,$gid) = (stat $nane)[4,5];
   $stat = sprintf "%0o", (stat_)[2];
   unless( -f $name ) {
   printf "$stat\t$uid $gid\t\t\t\t\t\t$name\n";
   next;
   }
  
   $md5->reset();
   open FILE, $name or print(STDERR "can't open file $name\n"), next;
   $md5->addfile(FILE);
   close FILE;
  
   $checksum - $md5->hexdigest();
   printf "$stat\t$uid $gid $checksum\t$name\n";
}   

it aim to calcute sum permission on the dir in @ARGV;
and with diff ,you can see  if files was modified
but the result isnt fine as you can see below
[EMAIL PROTECTED] cgi-bin]$ ./checksum.pl /bin
0   /bin
0   /bin/arch
0   /bin/awk
0   /bin/basename
0   /bin/bash
0   /bin/bash2
0   /bin/cat
0   /bin/chgrp
0   /bin/chmod
thanks
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



RE: problem printf

2004-10-24 Thread Charles K. Clarkson
deny <[EMAIL PROTECTED]> wrote:

: i ve found a perl script to  calculate the sum of the
: permissions  my principal files 
: 
: the line which is writing in my file is
: 
: foreach $name ( sort @files) {
:($uid,$gid) = (stat $nane)[4,5];
:ici   la ligne $stat = sprintf "%Oo",
: (stat_)[2];// 


That isn't only perl code, it's incomplete, and it
doesn't use 'printf'.  Show us more code and explain
your problem fully.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


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




problem printf

2004-10-24 Thread deny
hello
i ve found a perl script to  calculate the sum of the permissions  my 
principal files

the line which is writing in my file is
foreach $name ( sort @files) {
  ($uid,$gid) = (stat $nane)[4,5];
  ici   la ligne $stat = sprintf "%Oo", 
(stat_)[2];//

and when i check the result with
./checksum.pl /bin
i ve number not valid
instead of sums
0o  /bin/uname
0o  /bin/unicode_start
0o  /bin/unlink
0o  /bin/usleep
0o  /bin/vi
0o  /bin/view
0o  /bin/vim
0o  /bin/vim-minimal
0o  /bin/ypdomainname
0o  /bin/zcat

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



Re: variable FORMAT in printf?

2004-01-19 Thread Bryan Harris




>> I've tried everything I can think of, but I feel like a 6th grader trying to
>> solve a 7th grade math problem:
>> 
>> I'm trying to build a "pretty-fier" for any tab-delimited text file
>> (basically space-pad the columns so the decimals line up).  I search through
>> the columns finding the longest field with and highest precision level for
>> each column, then create a format string for the printf command.  The guts
>> of it looks like this:
>> 
>> **
>> $formatstr = "";
>> $index = 0;
>> foreach (@lwid) {
>>   $formatstr .= "%$lwid[$index].$lprec[$index]f".'\t';
>>   $index++;
>> }
> 
> In idiomatic perl that would be written as:
> 
> foreach my $index ( 0 .. $#lwid ) {
>   $formatstr .= "%$lwid[$index].$lprec[$index]f\t";
> }

Nice!  I wouldn't have thought to use a foreach on an on-the-spot (?) array
like that.


>> substr($formatstr,-2,2) = "";
>> $formatstr .= '\n';
> 
> Why not use join() instead of adding an extra "\t" and removing it
> later:
> 
> my $formatstr = join( "\t", map "%$lwid[$_].$lprec[$_]f", 0 .. $#lwid )
> . "\n";

Wow.  I would've never thought of that.


> Also, instead of using two arrays for width and precision, it might be
> simpler to use an array of arrays.

Good idea, I wouldn't have thought of that either.


>> open(FILE, ">$file$ext") || die("Couldn't create $file$ext: $!\n");
>> 
>> foreach (@content) {
>> @fields = split;
> 
> I assume that you are split()ing the input in order to measure the width
> of each field so you could store the contents in an array of arrays to
> avoid split()ing the data again here.  Also you could pre-format the
> data and avoid this foreach loop altogether.

Yes, absolutely right.


>> if (@fields) { printf FILE $formatstr, @fields; }
>> else { print FILE $_; }
>> }
>> 
>> close(FILE);
>> **
>> 
>> ... but that "printf" spits out actual "\t" and "\n"s.  How can I get the
>> printf to interpret those into tabs and newlines?
> 
> Put escape sequences in double quoted strings, not single quoted
> strings, to have them interpolated properly.

I'll try it.

Thanks for the reply, John, you always manage to clear things up for me.

- Bryan


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




Re: variable FORMAT in printf?

2004-01-19 Thread John W. Krahn
Bryan Harris wrote:
> 
> Hi,

Hello,

> I've tried everything I can think of, but I feel like a 6th grader trying to
> solve a 7th grade math problem:
> 
> I'm trying to build a "pretty-fier" for any tab-delimited text file
> (basically space-pad the columns so the decimals line up).  I search through
> the columns finding the longest field with and highest precision level for
> each column, then create a format string for the printf command.  The guts
> of it looks like this:
> 
> **
> $formatstr = "";
> $index = 0;
> foreach (@lwid) {
>   $formatstr .= "%$lwid[$index].$lprec[$index]f".'\t';
>   $index++;
> }

In idiomatic perl that would be written as:

foreach my $index ( 0 .. $#lwid ) {
$formatstr .= "%$lwid[$index].$lprec[$index]f\t";
}


> substr($formatstr,-2,2) = "";
> $formatstr .= '\n';

Why not use join() instead of adding an extra "\t" and removing it
later:

my $formatstr = join( "\t", map "%$lwid[$_].$lprec[$_]f", 0 .. $#lwid )
. "\n";


Also, instead of using two arrays for width and precision, it might be
simpler to use an array of arrays.


> open(FILE, ">$file$ext") || die("Couldn't create $file$ext: $!\n");
> 
> foreach (@content) {
> @fields = split;

I assume that you are split()ing the input in order to measure the width
of each field so you could store the contents in an array of arrays to
avoid split()ing the data again here.  Also you could pre-format the
data and avoid this foreach loop altogether.


> if (@fields) { printf FILE $formatstr, @fields; }
> else { print FILE $_; }
> }
> 
> close(FILE);
> **
> 
> ... but that "printf" spits out actual "\t" and "\n"s.  How can I get the
> printf to interpret those into tabs and newlines?

Put escape sequences in double quoted strings, not single quoted
strings, to have them interpolated properly.


John
-- 
use Perl;
program
fulfillment

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




Re: variable FORMAT in printf?

2004-01-19 Thread Tassilo von Parseval
On Sun, Jan 18, 2004 at 09:53:56PM -0700 Bryan Harris wrote:

> I've tried everything I can think of, but I feel like a 6th grader trying to
> solve a 7th grade math problem:
> 
> I'm trying to build a "pretty-fier" for any tab-delimited text file
> (basically space-pad the columns so the decimals line up).  I search through
> the columns finding the longest field with and highest precision level for
> each column, then create a format string for the printf command.  The guts
> of it looks like this:
> 
> 
> **
> $formatstr = "";
> $index = 0;
> foreach (@lwid) {
>   $formatstr .= "%$lwid[$index].$lprec[$index]f".'\t';
>   $index++;
> }
> substr($formatstr,-2,2) = "";
> $formatstr .= '\n';
> 
> open(FILE, ">$file$ext") || die("Couldn't create $file$ext: $!\n");
> 
> foreach (@content) {
> @fields = split;
> if (@fields) { printf FILE $formatstr, @fields; }
> else { print FILE $_; }
> }
> 
> close(FILE);
> **
> 
> 
> ... but that "printf" spits out actual "\t" and "\n"s.  How can I get the
> printf to interpret those into tabs and newlines?

This is a typical data versus program problem. You have to give perl a
hint that these are escapes and not literal strings. So turn your above
foreach-loop into 

foreach (@lwid) {
$formatstr .= "%$lwid[$index].$lprec[$index]f\t";
$index++;
}

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~;eval


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




variable FORMAT in printf?

2004-01-19 Thread Bryan Harris

Hi,

I've tried everything I can think of, but I feel like a 6th grader trying to
solve a 7th grade math problem:

I'm trying to build a "pretty-fier" for any tab-delimited text file
(basically space-pad the columns so the decimals line up).  I search through
the columns finding the longest field with and highest precision level for
each column, then create a format string for the printf command.  The guts
of it looks like this:


**
$formatstr = "";
$index = 0;
foreach (@lwid) {
  $formatstr .= "%$lwid[$index].$lprec[$index]f".'\t';
  $index++;
}
substr($formatstr,-2,2) = "";
$formatstr .= '\n';

open(FILE, ">$file$ext") || die("Couldn't create $file$ext: $!\n");

foreach (@content) {
@fields = split;
if (@fields) { printf FILE $formatstr, @fields; }
else { print FILE $_; }
}

close(FILE);
******


... but that "printf" spits out actual "\t" and "\n"s.  How can I get the
printf to interpret those into tabs and newlines?

TIA.

- Bryan



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




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]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




  1   2   >