print"Enter the number of Cricketers: ";
$list=<STDIN>;
chomp $list;
until($list<=0)
{
print"Enter Cricketer name: ";
$name=<STDIN>;
print"Enter a number: ";
$number=<STDIN>;
chomp $number;
$list--;
push(@names,$name);
push(@numbers,$number);
}
print"Cricketer's name and the corresponding number are\n";
format STDOUT=@<<<< @>>>>
"@numbers", "@names"
.
I have this code. The question is "Write a program that asks the user for a list of cricketer’s names and then a number. Print the cricketer’s name that is equivalent to the number as index."
I hope you can understand what I need. I need that code to print result in a formatted style like:
1 Sachin
2 Sehwag
3 Yuvraj

Answers to dhakshin@live.com are also welcome...
Thank you in advance........

Once you have the data in your arrays you can print them like this:

#!/usr/bin/perl -w
use strict;
my @numbers = (1, 2, 3);
my @names = ("Sachin", "Sehwag", "Yuvraj");

print "Cricketer's name and the corresponding number are\n";
while (@numbers) {
    my $num = shift(@numbers);
    my $name = shift(@names);
    print "$num $name\n";
}

You may need to sort the data by number first, if that's the output you want. I think, however, that you should be reading in a sequence of names, assigning a number to each name automatically: so the first name entered will become 1, the second 2, and so on. Then you prompt for a number, and print the name with that index.

This is just one way that you might go about this. The first part of the code is pretty much the same as yours: it just gets all of the names from the user. The difference is that I'm not reading in a number for each as you are. The next part just prints the names, much as d5e5's code does. The final part of the code just reads in a number, and prints the name at that index (after converting the input to a number with int()).

I should mention that some care should be taken if you want the first name to be indicated with an index of 1, because Perl's arrays are indexed beginning at 0 (just like most sane programming languages . . .).

#!/usr/bin/perl

use strict;

# get names from the user
my @names = ();
print "Enter a list of names; a blank line finishes.\n";
while(my $name = <STDIN>) {
    chomp $name;
    last if $name eq '';
    push(@names, $name);
}

# print the names, numbered
for(my $n = 0; $n < @names; $n ++) {
    print $n + 1, " $names[$n]\n";
}

# ask for a specific name
print "Enter name number to lookup: ";
my $number = int <STDIN>;
print "Name number $number = \"", $names[$number - 1], "\"\n";

Example use:

$ ./names.pl
Enter a list of names; a blank line finishes.
nice
to
meet
you

1 nice
2 to
3 meet
4 you
Enter name number to lookup: 3
Name number 3 = "meet"
$

Once you have the data in your arrays you can print them like this:

#!/usr/bin/perl -w
use strict;
my @numbers = (1, 2, 3);
my @names = ("Sachin", "Sehwag", "Yuvraj");

print "Cricketer's name and the corresponding number are\n";
while (@numbers) {
    my $num = shift(@numbers);
    my $name = shift(@names);
    print "$num $name\n";
}

Thank You very much....

An alternate option is to use a hash table lookup, thus you just would pass in the name of the cricketeer and it would return a number, I.E.

%Cricketeers =(
"PersonA" => 1);
$tempString = "PersonA";
print $Cricketeers{$tempString} . " " . $tempString . "\n";

This will output:
1 Person A

I don't have a book or whatever on me but you can add users and automatically assign the number's (using a loop for example), also it will sort I believe too, but you can use the same kind of call each time and it will return the number, this can be a nice thing because the array is indexed by the name not the number, so the "value" can actually be anything, it doesn't have to be numbers. it's just a "key value pair" the key being the cricketeer and the value being what you assigned to it.

Good luck

This article has been dead for over six months. Start a new discussion instead.