diamondsandperls has asked for the wisdom of the Perl Monks concerning the following question:

my @patterns = ( qr/Record contentId\=\"\d+\"/ ); my $final_file = "C:/Users/$sso/Desktop/archer_$searchid.txt"; open(my $final_fh, '>', $final_file) or die "Failed to open $final_file - $!"; open(my $input_fh, '<', $output_file) or die "Failed to open $output_file: $!"; while (<$input_fh>) { foreach my $pattern (@patterns) { print {$final_fh} $pattern, "\n"; } } close $final_fh;

With the following code when printing I get the following output. I am thinking I am doing something wrong with the following in storing regex matches, but not sure.

my @patterns = ( qr/Record contentId\=\"\d+\"/ );

(?-xism:Record contentId\=\"\d+\")
(?-xism:Record contentId\=\"\d+\")
(?-xism:Record contentId\=\"\d+\")
(?-xism:Record contentId\=\"\d+\")
(?-xism:Record contentId\=\"\d+\")
(?-xism:Record contentId\=\"\d+\")
(?-xism:Record contentId\=\"\d+\")
(?-xism:Record contentId\=\"\d+\")
(?-xism:Record contentId\=\"\d+\")
(?-xism:Record contentId\=\"\d+\")
(?-xism:Record contentId\=\"\d+\")
(?-xism:Record contentId\=\"\d+\")

Replies are listed 'Best First'.
Re: Storing regex values in an array and processing the array
by choroba (Cardinal) on Jun 07, 2013 at 17:48 UTC
    If you want to print the matches rather than the regular expressions, you have to tell Perl to do so:
    print {$final_fh} /$pattern/, "\n";

    This will probably still not do what you want, though. Read Regexp Quote Like Operators for details on what the match returns in various contexts and with various options (e.g. /g).

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Storing regex values in an array and processing the array
by 2teez (Vicar) on Jun 07, 2013 at 18:18 UTC

    Hi diamondsandperls,
    Just some thoughts:
    With this: (From your OP),

    my @patterns = ( qr/Record contentId\=\"\d+\"/ );
    You are creating an array of just one element. Which of course would be a better if you just use a scalar variable, like so:
    my $patterns = qr/Record contentId\=\"\d+\"/;
    and then use the modified code print {$final_fh} /$pattern/, "\n"; by choroba.

    Secondly, in your while loop, you are not using the data in the variable $_ at all, which are been read from your filehanle.
    And ofcourse, you still have to "match" the pattern as showed by the previous answer.
    Hope this helps.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Storing regex values in an array and processing the array
by Laurent_R (Canon) on Jun 07, 2013 at 18:52 UTC

    Not entirely sure of what you want to do, but your program certainly does not do what you want.

    Basically, your program reads an input file but does not use it, except as a line count in a sense: for each line in the input file, your program prints the regular expressions that you have stored in the @patterns array. But, as already pointed out, this array contains only one pattern, so that in the end, you are basically only printing the pattern count times, count being the number of lines in the input file.

    EDIT: fixed typo.
      sorry i should have stated there are many regex matches that will eventually be added is the plan im still not sure how to formulate this