On Sat, Oct 11, 2008 at 20:56, AndrewMcHorney <[EMAIL PROTECTED]> wrote:
> I have a line of code that appears to not be returning what I am looking
> for. I am attemting to add a number which has commas in it. It represents
> the number of bytes of a file obtained for the dir command. At the end of
> the run the number of bytes does not come close to what I expect. I am doing
> a $TotalBytes = $TotalBytes + $FileBytes where $FileBytes has commas in it.
> The variable $TotalBytes does not.
snip

Well, you could remove the commas with tr///* or s///**

#!/usr/bin/perl

use strict;
use warnings;

my $with_commas = "1,000,000";
my $without_commas = "1000000";

$with_commas =~ tr/,//d;
#or $with_commas =~ s/,//g;

print $without_commas + $with_commas, "\n";

But I it would probably be better to not use the dir command in the
first place.  The stat function*** will return the size of a file
(along with a whole bunch of other metadata about the file):

#!/usr/bin/perl

use strict;
use warnings;

my $file = "/Users/cowens/z.pl";
my $size = (stat $file)[7];
print $size + 1_000_000, "\n";

This also has the benefit of not spawning another process and forcing
you to parse its output.

* http://perldoc.perl.org/perlop.html#tr/SEARCHLIST/REPLACEMENTLIST/cds
** http://perldoc.perl.org/perlop.html#s/PATTERN/REPLACEMENT/msixpogce
*** http://perldoc.perl.org/functions/stat.html

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

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


Reply via email to