From mikeflan at att.net Wed Jun 17 16:04:10 2020 From: mikeflan at att.net (Mike Flannigan) Date: Wed, 17 Jun 2020 18:04:10 -0500 Subject: [pm-h] Perl Code References: Message-ID: Recently I found this bit of code that I wrote long ago.? I am a little surprised this works ($1 and $2 get transferred to the scalars). I don't write that way anymore, but I am leaving it that way in this program. use strict; use warnings; my $test = 'abcdefgh'; my ($latm, $longm) = $test =~ /(ab).*(fg)/i; print "$latm, $longm\n"; Prints this: ab, fg Mike From uri at stemsystems.com Wed Jun 17 23:13:00 2020 From: uri at stemsystems.com (Uri Guttman) Date: Thu, 18 Jun 2020 02:13:00 -0400 Subject: [pm-h] Perl Code In-Reply-To: References: Message-ID: <0adb5f2f-bd00-73da-4001-d7b182dfbad5@stemsystems.com> On 6/17/20 7:04 PM, Mike Flannigan wrote: > > Recently I found this bit of code that I wrote > long ago.? I am a little surprised this works > ($1 and $2 get transferred to the scalars). > I don't write that way anymore, but I am > leaving it that way in this program. > > > use strict; > use warnings; > > my $test = 'abcdefgh'; > my ($latm, $longm) = $test =~ /(ab).*(fg)/i; > print "$latm, $longm\n"; > > > Prints this: > ab, fg > that is perfectly normal perl behavior. grabs in a regex return those grabs when in a list context. the two scalars will get the grabbed strings. that is a very common style and i am not sure why you would want to change it. the only comment i could make is that checking if a regex worked is usually important too. but you could check the definedness of one of the scalars for that. uri From jellyson at gmail.com Thu Jun 18 22:19:19 2020 From: jellyson at gmail.com (John Ellyson) Date: Fri, 19 Jun 2020 00:19:19 -0500 Subject: [pm-h] Perl Code In-Reply-To: References: Message-ID: Mike, The behavior that you are describing is covered in Perl regular expressions tutorial at https://metacpan.org/pod/distribution/perl/pod/perlretut.pod#Extracting-matches . I personally leverage this a lot in the code that I write - especially to capture all occurrences of something into an array. In fact, I have gotten into the habit of trying to write my code in such a way to try to avoid using the regex variables like $1 and $2. And Uri has a very good point about needing to include some way to verify that the regex actually matched and returned values. Not having that has caused me headaches with trying to figure out what went wrong with my code. John Ellyson On Wed, Jun 17, 2020 at 6:04 PM Mike Flannigan wrote: > > Recently I found this bit of code that I wrote > long ago. I am a little surprised this works > ($1 and $2 get transferred to the scalars). > I don't write that way anymore, but I am > leaving it that way in this program. > > > use strict; > use warnings; > > my $test = 'abcdefgh'; > my ($latm, $longm) = $test =~ /(ab).*(fg)/i; > print "$latm, $longm\n"; > > > Prints this: > ab, fg > > > > Mike > _______________________________________________ > Houston mailing list > Houston at pm.org > https://mail.pm.org/mailman/listinfo/houston > Website: http://houston.pm.org/ > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mrdvt92 at yahoo.com Fri Jun 19 15:34:43 2020 From: mrdvt92 at yahoo.com (Michael R. Davis) Date: Fri, 19 Jun 2020 22:34:43 +0000 (UTC) Subject: [pm-h] Perl Code In-Reply-To: References: Message-ID: <1746711845.789281.1592606083994@mail.yahoo.com> PM,>And Uri has a very good point about needing to include some way to verify that the regex actually matched and returned values. I always? - put the regexp in a condition? - use the optional "m"? - use m{} if the regexp has a "/" in it.? - use \A for ^? - use \Z for $? - use?(?:)? when group not needed in $N ? - and assign $N to well named scoped variables before anything else. if ($string =~ m/\A(.)(?:.)(.)\Z/) { ? my $char1 = $1;? my $char3 = $2; ? dosomething($char1, $char3);} else { ? die("handle string not what expected");} Michael R. Davis -------------- next part -------------- An HTML attachment was scrubbed... URL: