Pater matching and backreference

Discussion in 'Perl Misc' started by Josef Moellers, Aug 22, 2005.

  1. 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
     
    Josef Moellers, Aug 22, 2005
    #1
    1. Advertising

  2. Josef Moellers

    Anno Siegel Guest

    Josef Moellers <> 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.
     
    Anno Siegel, Aug 22, 2005
    #2
    1. Advertising

  3. Anno Siegel wrote:
    > Josef Moellers <> 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
     
    Josef Moellers, Aug 22, 2005
    #3
  4. 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=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Aug 22, 2005
    #4
  5. Josef Moellers

    Bart Lateur Guest

    Josef Moellers wrote:

    >> Josef Moellers <> 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.
     
    Bart Lateur, Aug 22, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. paulm

    Newbie backreference question

    paulm, Jun 30, 2005, in forum: Python
    Replies:
    6
    Views:
    386
    paulm
    Jul 1, 2005
  2. Fredrik Lundh

    backreference in regexp

    Fredrik Lundh, Jan 31, 2006, in forum: Python
    Replies:
    2
    Views:
    355
    =?ISO-8859-1?Q?Sch=FCle_Daniel?=
    Jan 31, 2006
  3. Replies:
    4
    Views:
    636
    jeff emminger
    Aug 18, 2006
  4. abdulet
    Replies:
    2
    Views:
    543
    abdulet
    Oct 23, 2009
  5. Krishna Srinivasan

    Backreference and Substitution problems

    Krishna Srinivasan, Jan 22, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    94
    Anno Siegel
    Jan 22, 2004
Loading...

Share This Page