develooper Front page | perl.beginners | Postings from February 2002

Re: convert array to integer

Thread Previous
From:
Jon Molin
Date:
February 22, 2002 04:08
Subject:
Re: convert array to integer
Message ID:
3C76343A.825DB291@resfeber.se
kitti wrote:
> 
> how to convert array to integer
> 
> $array[0]=5
> $array[1]=6
> $array[2]=7
> $array[3]=8
> 

one way to do it is:

my @array = (5, 6, 7, 8);
my $some_val;
$some_val .= $_ for (@array);

another is:

my @array = (5, 6, 7, 8);
my $some_val = "@array";
$some_val =~ s/[^\d]//g;

a third is

my @array = (5, 6, 7, 8);
my $some_val = 0;
my $i = 1;
for (reverse @array)
{
$some_val += $i *  $_;
$i *= 10;
}


a fourth, is problay both better, quicker, more efficent and shorter but
i leave that to someone else :)

/Jon



> change to integer 5678 for calculate 5678+2=5680
> 
> thanks,

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About