Wagner, David --- Senior Programmer Analyst --- WGO wrote:
John W. Krahn wrote:

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

... but it doesn't work.

Any ideas?

my $num = keys %data;

my @allkeys = map sprintf( '%0*d', length( $num ), $_ ), 1 .. $num;


Sorry, but I don't see how it works. I know it works because I tried it. Something is going on with the '%0*d', but it is by me and not even sure where I would look to see why it is working.

perldoc -f sprintf

So please what magic is happening here?

The '*' character in the format string uses the next argument from the list so:

sprintf( '%0*d', length( $num ), $_ )

Is equivalent to:

sprintf( '%0' . length( $num ) . 'd', $_ )

But is safer.


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>




Reply via email to