Replace scalar in another scalar

Discussion in 'Perl Misc' started by Mark, Jan 27, 2005.

  1. Mark

    Mark Guest

    Hello,

    I have 2 scalars. I want to remove all occurences of one within the other.

    I've tried this:

    $delim = '|';
    $value = "||||||||T||||||e||||||||||s|||||||||t||||||||!||||||";

    $value =~ s/$delim//g;

    print "$value\n";

    .... but it doesn't work - it prints $value unaltered. What do I need to do
    to the "$value =~ s/$delim//g;" line to realise that I'm reffering to the
    _value_ of $delim, rather than the literal text "$delim"?

    This is possibly a FAQ, but it's dead hard to know what to put into Go-Ogle
    to find this information.

    Thanks for any help,

    Mark
    Mark, Jan 27, 2005
    #1
    1. Advertising

  2. Mark wrote :
    > Hello,
    >
    > I have 2 scalars. I want to remove all occurences of one within the other.
    >
    > I've tried this:


    use strict;
    use warnings;

    > $delim = '|';


    $delim = '\|';

    > $value = "||||||||T||||||e||||||||||s|||||||||t||||||||!||||||";
    >
    > $value =~ s/$delim//g;
    >
    > print "$value\n";


    HTH
    Martin

    --
    perl -e 'print 7.74.117.115.116.11.32.13.97.110.111.116.104.101.114.11
    ..32.13.112.101.114.108.11.32.13.104.97.99.107.101.114.10.7'
    Martin Kissner, Jan 27, 2005
    #2
    1. Advertising

  3. "Mark" <> wrote in
    news::

    > Hello,
    >
    > I have 2 scalars. I want to remove all occurences of one within the
    > other.
    >
    > I've tried this:
    >
    > $delim = '|';
    > $value = "||||||||T||||||e||||||||||s|||||||||t||||||||!||||||";
    >
    > $value =~ s/$delim//g;
    >
    > print "$value\n";
    >
    > ... but it doesn't work - it prints $value unaltered. What do I need
    > to do to the "$value =~ s/$delim//g;" line to realise that I'm
    > reffering to the _value_ of $delim, rather than the literal text
    > "$delim"?


    Oh, but that is not what is happening. Indeed Perl is correctly using the
    value of $delim. The problem is that the pipe character has a specific
    meaning in regular expressions. Try:

    #! /usr/bin/perl

    use strict;
    use warnings;

    my $delim = '|';
    my $value = "||||||||T||||||e||||||||||s|||||||||t||||||||!||||||";

    $value =~ s/\Q$delim\E//g;

    print "$value\n";
    __END__

    C:\Dload> q
    Test!

    For more information, read

    perldoc perlretut

    "Grouping things and hierarchical matching"

    and

    "More on characters, strings, and character classes"

    Sinan
    A. Sinan Unur, Jan 27, 2005
    #3
  4. Mark wrote:

    > I've tried this:
    >
    > $delim = '|';
    > $value = "||||||||T||||||e||||||||||s|||||||||t||||||||!||||||";
    >
    > $value =~ s/$delim//g;
    >
    > print "$value\n";
    >
    > ... but it doesn't work - it prints $value unaltered. What do I need to do
    > to the "$value =~ s/$delim//g;" line to realise that I'm reffering to the
    > _value_ of $delim, rather than the literal text "$delim"?


    You *are* getting the value of $delim. However, that value is a character
    that has special meaning when used in regexes. You'd get the same result
    from using the value as literal text:

    $value =~ s/|//g;

    To match a literal |, you need to escape it:

    $value =~ s/\|//g;

    So naturally, what you want to do is store the escape in $delim:

    $delim = '\|';

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Jan 27, 2005
    #4
  5. Martin Kissner <> writes:
    > Mark wrote :
    > > I have 2 scalars. I want to remove all occurences of one within the other.
    > >
    > > I've tried this:

    >
    > use strict;
    > use warnings;
    >
    > > $delim = '|';

    >
    > $delim = '\|';


    To let Perl help you get from '|' to '\|', you can use the quotemeta()
    function:

    $delim = '|';
    $delim = quotemeta $delim;
    Arndt Jonasson, Jan 27, 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. Brian Blais
    Replies:
    1
    Views:
    359
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    324
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,464
    Masudur
    Feb 18, 2008
  4. Clint Olsen
    Replies:
    6
    Views:
    333
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Regent
    Replies:
    9
    Views:
    129
    Jason Hood
    Dec 30, 2003
Loading...

Share This Page