PerlJam2015 has asked for the wisdom of the Perl Monks concerning the following question:
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 bolduse warnings; use strict; $_ = '1: A silly sentence (495,a) *BUT* one which will be useful. (3)' +; my $pattern = <STDIN>;
followed by aif (/$pattern/){
( 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 justprint "words $pattern\n"; print "\$1 morewords\n"; print "\$2 morewords\n"; print "\$seewhereimgoing\n"; }
EDIT For all the $nif /$pattern/{ print "$$i\n"; $i++; }
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, $nI 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...
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: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:
Thanks AnomalousMonk for the hint / warning. Now it's time for me to meditate, I suppose.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: exist backreference variable list?
by choroba (Cardinal) on Jan 09, 2015 at 11:16 UTC | |
|
Re: exist backreference variable list?
by Discipulus (Canon) on Jan 09, 2015 at 11:14 UTC | |
|
Re: exist backreference variable list?
by ww (Archbishop) on Jan 09, 2015 at 13:15 UTC | |
by PerlJam2015 (Novice) on Jan 09, 2015 at 22:01 UTC | |
|
Re: exist backreference variable list?
by Corion (Patriarch) on Jan 09, 2015 at 13:33 UTC | |
|
Re: exist backreference variable list?
by AnomalousMonk (Archbishop) on Jan 10, 2015 at 00:26 UTC | |
|
Re: exist backreference variable list?
by AnomalousMonk (Archbishop) on Jan 09, 2015 at 23:53 UTC | |
|
Re: exist backreference variable list?
by Anonymous Monk on Jan 09, 2015 at 12:09 UTC |