Bryan R Harris wrote:
> I'm having trouble with this, and I'm sure someone out there has a
> really clever solution--
> 
> I have a hash, %data, with, say, 32 elements.
> 
> From that, I need an array going from 01 to 32, i.e. 01, 02, 03, 04,
> etc. 
> 
> But if my hash had 750 elements, I'd want it to go from 001 to 750.
> 
> I thought this should be easy, but I'm really struggling with it--
> 
> This is the best I've come up with:
> 
> @allkeys = ('0' x (length(keys(%data)) - 1) . '1' .. keys(%data));

To generate a list from 1 to n, where the values are padded with zeroes to
the length of the longest value, you can do something like:

   my $n = keys %data;
   my $l = length $n;
   my @arr = map sprintf("%0*d", $l, $_), 1 .. $n;

But @arr is just a list of numbers. It has no particular correspondence to
the actual keys in %data, so what are you trying to do?

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


Reply via email to