Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Pater matching and backreference

Reply
Thread Tools

Pater matching and backreference

 
 
Josef Moellers
Guest
Posts: n/a
 
      08-22-2005
Hi,

I am trying to escape spaces but apparently get into a naming conflict:

#! /usr/bin/perl

use warnings;
use strict;

open(OGG, "ogginfo \"$ARGV[0]\" |") or die "$0: cannot open $ARGV[0]\n";
while (<OGG>) {
last if /^User comments section follows/;
}
while (<OGG>) {
chomp;
if (/^\s+title=(.*)/) {
my $title = $1;
$title =~ s/\s/\\$1/g;
print " --tt $title";
next;
}
if (/^\s+artist=(.*)/) {
my $artist = $1;
$artist =~ s/\s/\\$1/g;
print " --ta $artist";
next;
}
}
close OGG;
exit 0;

If the artist or title name contains spaces, I get the error message
Use of uninitialized value in concatenation (.) or string at ./xinfo
line 14, <OGG> line 14.
once for each space.
I assume this is due to the fact that $1 is defined when the substitute
is compiled.

How can I do the substiture?

Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-22-2005
Josef Moellers <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi,
>
> I am trying to escape spaces but apparently get into a naming conflict:
>
> #! /usr/bin/perl
>
> use warnings;
> use strict;
>
> open(OGG, "ogginfo \"$ARGV[0]\" |") or die "$0: cannot open $ARGV[0]\n";
> while (<OGG>) {
> last if /^User comments section follows/;
> }
> while (<OGG>) {
> chomp;
> if (/^\s+title=(.*)/) {
> my $title = $1;
> $title =~ s/\s/\\$1/g;


$title =~ s/(\s)/\\$1/g;

You forgot to capture.

> print " --tt $title";
> next;
> }
> }
> close OGG;
> exit 0;
>
> If the artist or title name contains spaces, I get the error message
> Use of uninitialized value in concatenation (.) or string at ./xinfo
> line 14, <OGG> line 14.
> once for each space.


Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      08-22-2005
Anno Siegel wrote:
> Josef Moellers <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> $title =~ s/\s/\\$1/g;

> $title =~ s/(\s)/\\$1/g;
>
> You forgot to capture.


Indeed, thanks. I apparently mixed $& with $1:
$title =~ s/\s/\\$&/g;
would have worked, too (TMTOWTDI).

Thanks again,

Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      08-22-2005
Also sprach Josef Moellers:

> I am trying to escape spaces but apparently get into a naming conflict:
>
> #! /usr/bin/perl
>
> use warnings;
> use strict;
>
> open(OGG, "ogginfo \"$ARGV[0]\" |") or die "$0: cannot open $ARGV[0]\n";
> while (<OGG>) {
> last if /^User comments section follows/;
> }
> while (<OGG>) {
> chomp;
> if (/^\s+title=(.*)/) {
> my $title = $1;
> $title =~ s/\s/\\$1/g;


During this match the previous value of $1 (the one you saved in $title)
is being reset because the match succeeds. But since you don't capture
any of the matches, you end up with undef in $1. As I understand you,
you want to escape all spaces in $title. In order to refer to the found
spaces with $1, you have to capture them:

$title =~ s/(\s)/\\$1/g;

[...]

> If the artist or title name contains spaces, I get the error message
> Use of uninitialized value in concatenation (.) or string at ./xinfo
> line 14, <OGG> line 14.
> once for each space.
> I assume this is due to the fact that $1 is defined when the substitute
> is compiled.


No, $1 is defined during matching time. Once a pattern match succeeds
previous values of ${1|2|...} are lost. They remain their old value only
if the new match fails. This happens regardless of whether your second
match is capturing or not.

Tassilo
--
use bigint;
$n=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      08-22-2005
Josef Moellers wrote:

>> Josef Moellers <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >> $title =~ s/\s/\\$1/g;

>> $title =~ s/(\s)/\\$1/g;
>>
>> You forgot to capture.

>
>Indeed, thanks. I apparently mixed $& with $1:
>$title =~ s/\s/\\$&/g;
>would have worked, too (TMTOWTDI).


Except use of $& would slow *every* regex in your script, and in all
modules, down.
--
Bart.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bug? concatenate a number to a backreference: re.sub(r'(zzz:)xxx',r'\1'+str(4444), somevar) abdulet Python 2 10-23-2009 12:27 PM
re.sub() backreference bug? jemminger@gmail.com Python 4 08-18-2006 12:47 AM
backreference in regexp Fredrik Lundh Python 2 01-31-2006 03:02 PM
Newbie backreference question paulm Python 6 06-30-2005 11:00 PM
Backreference and Substitution problems Krishna Srinivasan Perl Misc 1 01-22-2004 10:48 AM



Advertisments