Hello everyone,
Hoping someone can help me/point me in the right direction with the following problem. I am using the following code to search for a bunch of regex's/strings then print which matches came from which headers (headers in this case defined by the lines that start with ***\s). Below is the code that I am using along with the output that I am currently seeing.
#!/usr/bin/perl use strict; use warnings; my %header = (); my $within_body = ''; my @search_strings = qw(stringa stringb stringc); while (<DATA>) { chomp; if ($_ =~ /^\*{3} (.+?)$/) { $within_body = $1; } else { foreach my $search_strings (@search_strings) { if (/$search_strings/) { push (@{$header{$within_body}}, $search_strings); } } } } foreach my $section (sort keys %header) { print "Found the following strings within body *** $section\n"; print join("\n", @{$header{$section}}); print "\n"; } __DATA__ *** 3993.2399 stringa stringb stringc *** 3993.23384j stringc junk data junk data *** 3993.23993k stringa stringb junk data junk data
The script print out and matches what I want for the most part. The problem is that I want to do if statements for each of the regexes that match and print a different message depending on which regex matched instead of just printing the name of the regex that matched. Sample code below of what I am trying to do. I am not quite sure to search/match the values stored in @{$header{$section}} and then printing the results based off the match.
if ( @{$header{$section}} = /stringa/gm ) { print "matched on stringa\n"; } if ( @{$header{$section}} = /stringb/gm ) { print "regex on string b\n"; } if ( @{$header{$section}} = /stringc/gm ) { print "C match\n"; }
CURRENT OUTPUT
Found the following strings within body *** 3993.23384j stringc Found the following strings within body *** 3993.2399 stringa stringb stringc Found the following strings within body *** 3993.23993k stringa stringb
DESIRED OUTPUT
Found the following strings within body *** 3993.23384j C match Found the following strings within body *** 3993.2399 matched on stringa regex on string b C match Found the following strings within body *** 3993.23993k matched on stringa regex on string b

In reply to Regex matching against hash values by learningperl01

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.