Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Help with substitution

by tej (Scribe)
on Mar 24, 2011 at 11:23 UTC ( [id://895230]=perlquestion: print w/replies, xml ) Need Help??

tej has asked for the wisdom of the Perl Monks concerning the following question:

I have a string as, $string="Referring Hospital Triage Time <183> 2 h (N <26> 191)"; $pre_str=Referring Hospital;

I want to remove $pre_str from $string. I am writing the following code

$str1=$string; $str1=~s/\Q$pre_str\E//;

When i print $str1 I get output as "Referring Hospital Triage Time <183> 2 h (N <26> 191)"

Why is this happening? Please help!!

Thank you..

Replies are listed 'Best First'.
Re: Help with substitution
by jethro (Monsignor) on Mar 24, 2011 at 12:32 UTC
    $string="Referring Hospital Triage Time <183> 2 h (N <26> 191)"; $pre_str="Referring Hospital"; $str1=$string; $str1=~s/\Q$pre_str\E//; print $str1;

    prints

    Triage Time <183> 2 h (N <26> 191)
Re: Help with substitution
by JavaFan (Canon) on Mar 24, 2011 at 12:00 UTC
    $pre_str=Referring Hospital;
    doesn't do what you think it does. It calls the method 'Referring' in the package 'Hospital'. (Which will die if that method cannot be found).

    Perhaps you miss cut-and-paste? But then, if you did, you're showing us different code that you have a problem with. Can you post some runnable code that doesn't do what you expect it to do?

Re: Help with substitution
by Nikhil Jain (Monk) on Mar 24, 2011 at 12:06 UTC
    use strict; my $string="Referring Hospital Triage Time <183> 2 h (N <26> 191)"; my $pre_str="Referring Hospital"; my $str1=$string; $str1=~s/$pre_str(.*?)/$1/; print "$str1\n";

    Output: Triage Time <183> 2 h (N <26> 191)

    One more thing The \Q tells Perl where to start escaping special characters, and the \E tells it where to stop, and in your $pre_str, i am not finding any special characters in your given example, so no need to use it.

Re: Help with substitution
by TomDLux (Vicar) on Mar 25, 2011 at 01:28 UTC

    Using regex is kinda like hiring a bulldozer to move a couple shovels-full of dirt. Removing simple, constant strings is a perfect job for index and  substr.

    use strict; my $string="Referring Hospital Triage Time <183> 2 h (N <26> 191)"; my $pre_str="Referring Hospital"; my $index = index $string, $pre_str; die( "prefix '$pre_str' not present in string '$string'." ) unless $index >= 0; my $len = length $pre_str; my $copy = $string; substr $copy, $index, $len, ''; print <<"EOMSG"; Deleting $len characters starting at postion $index of the string $string leaves $copy EOMSG

    gemerates the output

    Deleting 18 characters starting at postion 0 of the string Referring Hospital Triage Time <183> 2 h (N <26> 191) leaves Triage Time <183> 2 h (N <26> 191)

    So what do you want to do about that space that isn't part of your prefix string?

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://895230]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-19 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found