CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...: Re: [Laurent_R] print numbers from 1 to 100 and a to z as index by using hashes: Edit Log



Zhris
Enthusiast

Apr 24, 2014, 4:34 PM


Views: 17079
Re: [Laurent_R] print numbers from 1 to 100 and a to z as index by using hashes

Nice logic, it hadn't crossed my mind to shift off each letter and (re)build when empty.

Might as well post my actual code with the OP's hash requirement in mind, although your solution is nicer:


Code
my @nums = (1..100); 
my @lets = ('A'..'Z');

my %hash;
@hash{@nums} = (@lets) x round_up(@nums/@lets);

print "$hash{$_}:$_\n" for (sort { $a <=> $b } keys %hash);

sub round_up { return int $_[0] == $_[0] ? $_[0] : int $_[0] + 1; }


Chris


(This post was edited by Zhris on Apr 24, 2014, 4:39 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Apr 24, 2014, 4:37 PM
Post edited by Zhris (Enthusiast) on Apr 24, 2014, 4:38 PM
Post edited by Zhris (Enthusiast) on Apr 24, 2014, 4:39 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives