gwen smith
gwen smith

Reputation: 21

Finding position of elements in perl

I am having the hardest time with the question. I am new to programming and I can not figure out how to select the amino acid at the position the user asks. Here is my question:

This is my array:

@newarray = ('Phe', 'Val', 'Asn', 'Gln', 'His',
             'Leu', 'Cys', 'Asp', 'Ser', 'His'); 

The Question asks to Ask the user to enter a number between 1 and the number of amino acids in the polypeptide, and print the amino acid in that position (e.g. if the user enters "4" the program should print "Gln"

This is what I have so far:

@newarray = ('Phe', 'Val', 'Asn', 'Gln', 'His',
             'Leu', 'Cys', 'Asp', 'Ser', 'His'); 

print "Please print a number between 1 and 10\n";

chomp ($variable_name = <STDIN>)

Upvotes: 2

Views: 247

Answers (1)

H&#229;kon H&#230;gland
H&#229;kon H&#230;gland

Reputation: 40748

To print the value at index ($variable_name - 1) from the array @newarray, you can do:

print $newarray[$variable_name - 1], "\n"; 

See perlintro for more information.

Upvotes: 2

Related Questions