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...:
print numbers from 1 to 100 and a to z as index by using hashes

 



vikasmetla
Novice

Apr 23, 2014, 11:37 PM

Post #1 of 7 (10779 views)
print numbers from 1 to 100 and a to z as index by using hashes Can't Post

hi perl guru's

i need help ..

i want to a program that print 1 to 100 numbers and a to z alphabets parallel...after completing a to z of 26 numbers and repeat the alphabets again then start 'a' with 27 number and ....
and repeat up to 100...the program want write with using hashes only...

i tried with some code..plz check and modifiy it ...



Code
#!/usr/bin/perl 

my @string=(a..z);
my @numbers=(1..100);
@hash{("a".."z")}=(1..100);
foreach $value(sort
keys %hash)
{
print "$value: $hash{$value}\n";
}
o/p:-

a:1
b:2
c:3
.
.
.
.
x:24
y:25
z:26


but i want output like this........

output:-

a:1
b:2
c:3.
.
.
.
.
.
x:24
y:25
z:26
a:27
b:28
c:29
.
.
.
.
.
.
.
.
?:100


(This post was edited by vikasmetla on Apr 23, 2014, 11:49 PM)


Zhris
Enthusiast

Apr 24, 2014, 2:20 AM

Post #2 of 7 (10773 views)
Re: [vikasmetla] print numbers from 1 to 100 and a to z as index by using hashes [In reply to] Can't Post

Hey,

This looks like its homework. My hint is to roughly pre-build the list of 100 letters before constructing the hash.


Code
my @lets = ( 'A' .. 'Z' ) x round_up( 100 / 26 );  

sub round_up { ... }


Chris


(This post was edited by Zhris on Apr 24, 2014, 2:25 AM)


Laurent_R
Veteran / Moderator

Apr 24, 2014, 2:10 PM

Post #3 of 7 (10751 views)
Re: [vikasmetla] print numbers from 1 to 100 and a to z as index by using hashes [In reply to] Can't Post

A possible Perl one-liner:


Code
$ perl -le 'my @letters; for my $i (1..100) { @letters = "A".."Z" unless  @letters; print $i, ":", shift @letters;}' 
1:A
2:B
3:C
4:D
5:E
6:F
7:G
8:H
9:I
...
92:N
93:O
94:P
95:Q
96:R
97:S
98:T
99:U
100:V



Zhris
Enthusiast

Apr 24, 2014, 4:34 PM

Post #4 of 7 (10747 views)
Re: [Laurent_R] print numbers from 1 to 100 and a to z as index by using hashes [In reply to] Can't Post

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)


BillKSmith
Veteran

Apr 24, 2014, 9:46 PM

Post #5 of 7 (10738 views)
Re: [vikasmetla] print numbers from 1 to 100 and a to z as index by using hashes [In reply to] Can't Post

You problem is more easily done with an array. If a hash is reqired, use a hash with integer keys to simulate the array.

Code
use strict; 
use warnings;
my @array = ('a' .. 'z');
for (1..100){
my $i = (($_-1) % 26);
print "$array[$i]:$_\n"
}

my %hash;
@hash{0..25} = 'a' .. 'z';
for (1..100){
my $i = (($_-1) % 26);
print "$hash{$i}:$_\n"
}


UPDATE: Changed hash initialization to use OP's hash sclice.
Good Luck,
Bill

(This post was edited by BillKSmith on Apr 25, 2014, 7:35 AM)


Zhris
Enthusiast

Apr 25, 2014, 12:38 PM

Post #6 of 7 (10600 views)
Re: [BillKSmith] print numbers from 1 to 100 and a to z as index by using hashes [In reply to] Can't Post

Another nice solution, great use of modulus.

Chris


vikasmetla
Novice

Apr 28, 2014, 2:05 AM

Post #7 of 7 (9690 views)
Re: [Zhris] print numbers from 1 to 100 and a to z as index by using hashes [In reply to] Can't Post

Thanks for every one ....for ur replays Smile

 
 


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

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