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


Views: 17290
print numbers from 1 to 100 and a to z as index by using hashes

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


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

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


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

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


Views: 17258
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)


BillKSmith
Veteran

Apr 24, 2014, 9:46 PM


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

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


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

Another nice solution, great use of modulus.

Chris


vikasmetla
Novice

Apr 28, 2014, 2:05 AM


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

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