Front page | perl.beginners | Postings from February 2002

## RE: convert array to integer

From:
Aaron Shurts
Date:
February 22, 2002 09:37
Subject:
RE: convert array to integer
Message ID:
1047D74709F06345A829829080D6AE400101C459@COURIER1.corp.bluelight.com
```This may not be a better way, but another way is:

my(@array) = (5, 6, 7, 8);
my(\$integer) = sprintf("%1d%1d%1d%1d", @array[0], @array[1], @array[2],
@array[3]);

-_-Aaron

-----Original Message-----
From: Jon Molin [mailto:Jon.Molin@resfeber.se]
Sent: Friday, February 22, 2002 4:06 AM
To: kitti
Cc: beginners@perl.org
Subject: Re: convert array to integer

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,

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org