Sorry, the title is complex, but I didn't know how to put it into easier terms, but it will look simpler when written in code. I'm trying to read files and as I read them, set up a hash of a hash, and the values of the inner hash keys will be gotten by regular expressions on $_ while reading the file. So, the code would look like this:
use strict; my $file = shift @ARGV; open (INPUT, "<$file"); my @ids = qw(1234 2345 3456); my %hash; foreach my $id (@ids) { while (<INPUT>) { $hash{$id} = { headline => sub { return /.*headline: (.*)/ }, byline => sub { return /.*byline: (.*)/}, }; } print $hash{$id}->{byline}->()."\n"; } close INPUT;
I keep getting a '1' as my output. I tried a couple different things but nothing got me any good results, outside of writing a subroutine outside of the hash and calling it as a reference in each value in place of the anonymous subroutines. Any help you guys can provide would be great. Thanks

In reply to anonymous subroutines assigning regexes to hash keys by donkeykong

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.