I'd like to start off by saying this is literally my first post on this website, so if there are some conventions I'm not following that are immediately and clearly irritating you, kindly inform me so that I might edit this post to follow the "monastery's" standards.

I'm learning Perl on my own with Simon Cozen's free version of "Beginning Perl" available online. It's pretty dated, and truth be told I've already begun to get a little frustrated with it since in chapter 3 one of the exercises required knowledge of the  chomp function before he introduced it.

It's my first language, and I'm learning it over my winter break in the short period before classes resume at my college (I'm a chemical engineering major and I have an interest in de-novo protein design which will require a data pipeline involving at least three languages, Perl or Python being the midway between C and R (CPR data pipeline. Go figure)) and frankly have fallen in love with the language's ways of evoking variables and especially the regular expressions (I can feel the power!) but I'm in a rut.

I'm not asking anyone to do my homework for me. There is no instructor. I'm just genuinely confused to the point of complete and utter stagnation. I've already spent hours looking at the documentation (it mentions grep which confused me even more), and a few posts from 2005. (One had a solution involving a subroutine which normally I'd be ready to jump right into and use, but I'd like to figure out how to do this with an  until loop. or something)

So getting right into it... (TLDR)

Earlier in the chapter it notes that when using a Regular Expression with grouping, the sucker "eats up" hits and stores them in incrementally changing variables of $1, $2... Immediately it showed an example like the following

EDIT: Edited parts in bold
use warnings; use strict; $_ = '1: A silly sentence (495,a) *BUT* one which will be useful. (3)' +; my $pattern = <STDIN>;
if (/$pattern/){
followed by a
print "words $pattern\n"; print "\$1 morewords\n"; print "\$2 morewords\n"; print "\$seewhereimgoing\n"; }
( https://drive.google.com/viewerng/viewer?url=http://blob.perl.org/books/beginning-perl/3145_Chap05.pdf for those interested) Now, as soon as I saw that I realized "There's probably a way to set some $i so that I can just
if /$pattern/{ print "$$i\n"; $i++; }
EDIT For all the $n

But for the life of me, I figure I'm reading the question wrong because it seems (from the way the author describes it) that there's a list generated that has the set of all these that I could just print or refer to in order to make this process a lot less head-scratching.

"3. When we use groups, the // operator returns a list of all the text strings that have been matched. Modify our example program matchtest2.plx, so that it produces its output from this list, rather than using special variables."

Am I completely insane here? Is there a magic  $wizardextract that stores the series of  $1, $2,.. $lots that I managed to overlook?

EDIT: Specifically, if the original data being looked at is hardcoded, and your regular expression is  <STDIN> how could you write your program such that it prints out ALL the  $1, $2, $n

I appreciate the heck out of any non-subroutine tips on this, and odds are if the consensus is "lol just go to chapter 6 (subroutines) and go back" I probably will.

-Mark

UPDATE:

So now, I've got this...

$_ = '1: A silly sentence (495,a) *BUT* one which will be useful. (3)' +; print "Enter a regular expression: "; my $pattern = <STDIN>; chomp $pattern; if (my @matches = /$pattern/) { for my $i (0..@-) { print "$i: $matches[$i]\n"; } }

Thanks to choroba for the means of capturing them in an array, and Corion for the @- series!

Now, the goal is to be able to print out all the variables that match all the groupings in a regular expression, for the example I'll be using: (a-z+?)(.*?)(a-z+?). Right now the output is...

Enter a regular expression: ([a-z]+?)(.*?)([a-z]+?) 0: s 1: 2: i Use of unitialized value in concatenation (.) or string at exp3.pl lin +e 14, <STDIN> line 1. 3. Use of unitialized value in concatenation (.) or string at exp3.pl lin +e 14, <STDIN> line 1. 4:
The goal is achieved, but now I'm confused as to what the error message means and how to deal with it. I'm working on that now. Re-update:

Thanks AnomalousMonk for the hint / warning. Now it's time for me to meditate, I suppose.


In reply to exist backreference variable list? by PerlJam2015

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.