<?xml version="1.0" encoding="windows-1252"?>
<node id="1006174" title="Re: subroutine help" created="2012-11-29 02:05:10" updated="2012-11-29 02:05:10">
<type id="11">
note</type>
<author id="971084">
frozenwithjoy</author>
<data>
<field name="doctext">
&lt;p&gt;A few things to help you out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your code doesn't compile, because there is a semi-colon missing at the end of &lt;c&gt;@array = $form =~ /[A-Z][a-z]?|\d+/ig&lt;/c&gt;
&lt;li&gt;You assign the result of &lt;c&gt;getweight()&lt;/c&gt;, which is in list context, to a scalar. As a result, &lt;c&gt;$weight&lt;/c&gt; just equals 6 (the number of elements in the array). If I were you, I'd skip the subroutine and array and make weight a hash directly from the get-go:
&lt;/ul&gt;

&lt;c&gt;
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use Data::Printer;

print "Enter a chemical formula: ";
chomp( my $form = &lt;&gt; );

my %weight = $form =~ /[A-Z][a-z]?|\d+/ig;
p %weight;

__END__

Enter a chemical formula: C12H22O11
{
    C   12,
    H   22,
    O   11
}
&lt;/c&gt;</field>
<field name="root_node">
1006165</field>
<field name="parent_node">
1006165</field>
</data>
</node>
