In this code, the user types in an element, and the code searches the array for that element then takes the weight of that element and uses it in calculations. No matter what chemical I give it, the code always chooses the first row of the array. Here's the code in it's entirety. Thanks for any assistance. The problem is in sub findMass. No matter what chemical is entered, that if statement activates.
## Grams to Moles use 5.14.2; no strict 'refs'; my ($grams, $chemWeight, $moles); #integers my $chemical; #strings my @periodicTable; sub main { fillArray(); setChemical(); setGrams(); findMass(); } main(); sub fillArray { @periodicTable = ( [1 , 'H' , 'Hydrogen' , 1], [2 , 'He', 'Helium',4], [3,'Li', 'Lithium',6], [4,'Be', 'Beryllium',9], [5,'B', 'Boron',10], [6,'C', 'Carbon',12], [7,'N', 'Nitrogen',14], [8,'O', 'Oxygen',15], [9,'F', 'Fluorine',18], [10,'Ne', 'Neon',20], [11,'Na', 'Sodium',22], [12,'Mg', 'Magnesium',24], [13,'Al', 'Aluminum',26], [14,'Si', 'Silicon',28], [15,'P', 'Phosphorus',30], [16,'S', 'Sulfur', 32], [17,'Cl', 'Chlorine', 35], [18,'Ar', 'Argon', 39], [19,'K', 'Potassium',39], [20,'Ca', 'Calcium',40], [21,'Sc', 'Scandium',44], [22,'Ti', 'Titanium',47], [23,'V', 'Vanadium',50], [24,'Cr', 'Chromium',51], [25,'Mn', 'Manganese',54], [26,'Fe', 'Iron' ,55], [27,'Co', 'Cobalt' ,58], [28,'Ni', 'Nickel' ,59], [29,'Cu', 'Copper' ,63], [30,'Zn', 'Zinc' ,65], [31,'Ga', 'Gallium' , 69], [32,'Ge', 'Germanium' , 72], [33,'As', 'Arsenic' , 74], [34,'Se', 'Selenium' , 78], [35,'Br', 'Bromine' , 79], [36,'Kr', 'Krypton' , 83], [37,'Rb', 'Rubidium' , 85], [38,'Sr', 'Strontium' , 87], [39,'Y', 'Yttrium' , 88], [40,'Zr', 'Zirconium' , 91], [41,'Nb', 'Niobium' , 92], [42,'Mo', 'Molybdenum' , 95], [43,'Tc', 'Technetium' , 98], [44,'Ru', 'Ruthenium' , 101], [45,'Rh', 'Rhodium' , 102], [46,'Pd', 'Palladium' , 106], [47,'Ag', 'Silver' , 107], [48,'Cd', 'Cadmium' , 112], [49,'In', 'Indium' , 114], [50,'Sn', 'Tin' , 118], [51,'Sb', 'Antimony' , 121], [52,'Te', 'Tellurium' , 127], [53,'I', 'Iodine' , 127], [54,'Xe', 'Xenon' , 131], [55,'Cs', 'Cesium' , 132], [56,'Ba', 'Barium' , 137], [57,'La', 'Lanthanum' , 138], [58,'Ce', 'Cerium' , 140], [59,'Pr', 'Praseodymium' , 140], [60,'Nd', 'Neodymium' , 144], [61,'Pm', 'Promethium' , 145], [62,'Sm', 'Samarium' , 150], [63,'Eu', 'Europium' , 151], [64,'Gd', 'Gadolinium' , 157], [65,'Tb', 'Terbium' , 159], [66,'Dy', 'Dysprosium' , 162], [67,'Ho', 'Holmium' , 165], [68,'Er', 'Erbium' , 167], [69,'Tm', 'Thulium' , 169], [70,'Yb', 'Ytterbium' , 173], [71,'Lu', 'Lutetium' , 175], [72,'Hf', 'Hafnium' , 178], [73,'Ta', 'Tantalum' , 181], [74,'W', 'Tungsten' , 184], [75,'Re', 'Rhenium' , 186], [76,'Os', 'Osmium' , 190], [77,'Ir', 'Iridium' , 192], [78,'Pt', 'Platinum' , 195], [79,'Au', 'Gold' , 197], [80,'Hg', 'Mercury' , 201], [81,'Tl', 'Thallium' , 204], [82,'Pb', 'Lead' , 207], [83,'Bi', 'Bismuth' , 209], [84,'Po', 'Polonium' , 209], [85,'At', 'Astatine' , 210], [86,'Rn', 'Radon' , 222], [87,'Fr', 'Francium' , 223], [88,'Ra', 'Radium' , 226], [89,'Ac', 'Actinium' , 227], [90,'Th', 'Thorium' , 232], [91,'Pa', 'Protactinium' , 231], [92,'U', 'Uranium' , 238], [93,'Np', 'Neptunium' , 237], [94,'Pu', 'Plutonium' , 244], [95,'Am', 'Americium' , 243], [96,'Cm', 'Curium' , 247], [97,'Bk', 'Berkelium' , 247], [98,'Cf', 'Californium' , 251], [99,'Es', 'Einsteinium' , 252], [100,'Fm', 'Fermium' , 257], [101,'Md', 'Mendelevium' , 258], [102,'No', 'Nobelium' , 259], [103,'Lr', 'Lawrencium' , 262], [104,'Rf', 'Rutherfordium' , 267], [105,'Db', 'Dubnium' , 268], [106,'Sg', 'Seaborgium' , 271], [107,'Bh', 'Bohrium' , 272], [108,'Hs', 'Hassium' , 270], [109,'Mt', 'Meitnerium' , 276], [110,'Ds', 'Darmstadtium' , 281], [111,'Rg', 'Roentgenium' , 280], [112,'Cn', 'Copernicium' , 285], [113,'Uut', 'Ununtrium' , 284], [114,'Uuq', 'Ununquadium' , 289], [115,'Uup', 'Ununpentium' , 288], [116,'Uuh', 'Ununhexium' , 293], [117,'Uus', 'Ununseptium' , 294], [118,'Uuo', 'Ununoctium' , 294], ); } sub setChemical { print "Chemical(1-2 Letter Abbreviation):"; chomp ($chemical = <STDIN>); } sub setGrams { print "Mass in Grams: "; chomp ($grams = <STDIN>); } sub findMass { for (my $i = 0; $i < 118; $i++) { if ($chemical = $periodicTable[$i][1]) { #always activates $chemWeight = $periodicTable[$i][3]; calculateMoles(); } } } sub calculateMoles { $moles = $grams/$chemWeight; printResults(); } sub printResults { print "\n Final Weight: $chemWeight"; print "\t$chemical weighs $moles Moles\n\n"; die "Finished.."; }

In reply to Array problems. by thearkranger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.