Gossamer Forum
Home : General : Perl Programming :

Question about script with $'s in it.

Quote Reply
Question about script with $'s in it.
I am taking some basic data from a text file and am trying to get ride of a few things in each string of data. Since I am very new to perl programing, I don't have a clue of how to do it.

Here is the sample script of what I am trying to do. It has some sample data in it as $stringX.

#!/usr/bin/perl -w

$string1="# include $RULE_PATH/chat.rules";
$string2="include $RULE_PATH/chat.rules";
$string3="#include $RULE_PATH/chat.rules";
$string4="#include chat.rules";

print "\n\n\nOrginal Strings\n";
print "$string1\n";
print "$string2\n";
print "$string3\n";
print "$string4\n";

($strmod1) = $string1 =~ /[#]*(\S+)\.rules/;
($strmod2) = $string2 =~ /[#]*(\S+)\.rules/;
($strmod3) = $string3 =~ /[#]*(\S+)\.rules/;
($strmod4) = $string4 =~ /[#]*(\S+)\.rules/;

print "\n\n\nNew Strings\n";
print "$strmod1\n";
print "$strmod2\n";
print "$strmod3\n";
print "$strmod4\n";
print "\n\n\n";


As you can see the stringX var's have a $ in them. First, I want the script to ignore the $ and to just treat the string as a string. Right now I am getting a:

Use of uninitialized value in concatenation (.) or string at ./ruletest.pl line 3.
Use of uninitialized value in concatenation (.) or string at ./ruletest.pl line 4.
Use of uninitialized value in concatenation (.) or string at ./ruletest.pl line 5.


Second, the only thing that I want to have in the strmod's is the word chat and nothing else. So the output should just be "chat".

Each string is an example of the type of data that I will be gathering.

Any ideas?
Quote Reply
Re: [cslyon] Question about script with $'s in it. In reply to
Escape it in the string...i.e;

$string1="# include \$RULE_PATH/chat.rules";

..that should do it.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Question about script with $'s in it. In reply to
I would but I can't control what I put in the string. I am pulling this data from a text file and that is just a sample of what is in the text file. The example program is just a little bit of the program and not the entire thing.
Quote Reply
Re: [cslyon] Question about script with $'s in it. In reply to
$string =~ s/\$/\\\$/g;

give that a go.... (after the defining of these variables).

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Question about script with $'s in it. In reply to
OK, I gave that a try. It is still giving me the:

Use of uninitialized value in concatenation (.) or string at ./ruletest.pl line 3.
Use of uninitialized value in concatenation (.) or string at ./ruletest.pl line 5.
Use of uninitialized value in concatenation (.) or string at ./ruletest.pl line 6.


I put that string right after just on one variable.

<snip>

$string1="# include $RULE_PATH/chat.rules";
$string1 =~ s/\$/\\\$/g;


</snip>



I thnk it has a problem with the string1 var.
Quote Reply
Re: [cslyon] Question about script with $'s in it. In reply to
You have an un-escaped . in there too. Try;

$string1="# include $RULE_PATH/chat.rules";
$string1 =~ s/\$/\\\$/g;
$string1 =~ s/\./\\\./g;

Does that work?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Question about script with $'s in it. In reply to
Same thing on that one.

OK, Maybe I am going down the wrong road. Let me ask you this? Once I get this working can I then remove it with this?



($strmod1) = $string1 =~ /[#]*(\S+)\.rules/;




Right now that is removing the # if there is one and any whitespaces. Would there be a way to remove the $RULE_PATH? If so, how would that work?
Quote Reply
Re: [cslyon] Question about script with $'s in it. In reply to
Is it meant to output;

Quote:
Orginal Strings
# include $RULE_PATH/chat.rules
include $RULE_PATH/chat.rules
#include $RULE_PATH/chat.rules
#include chat.rules

New Strings
$RULE_PATH/chat
$RULE_PATH/chat
$RULE_PATH/chat
chat

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Question about script with $'s in it. In reply to
Just to output;

New String
chat
chat
chat
chat
Quote Reply
Re: [cslyon] Question about script with $'s in it. In reply to
Well, I've come up with;

Code:
#!perl
# ==================================================================

use strict;

use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/html \n\n";
my $string1="# include \$RULE_PATH/chat.rules";
my $string2="include \$RULE_PATH/chat.rules";
my $string3="#include \$RULE_PATH/chat.rules";
my $string4="#include chat.rules";

print "\n\n\nOrginal Strings\n";
print "$string1\n";
print "$string2\n";
print "$string3\n";
print "$string4\n";

$string1 =~ m,/(.+?)\.rules, and my $strmod1 = $1;
$string2 =~ m,/(.+?)\.rules, and my $strmod2 = $1;
$string3 =~ m,/(.+?)\.rules, and my $strmod3 = $1;
$string4 =~ m,your guess is as good as mine, and my $strmod4 = $1;

print "\n\n\nNew Strings\n";
print "$strmod1\n";
print "$strmod2\n";
print "$strmod3\n";
print "$strmod4\n";
print "\n\n\n";

Unfortunatly, the last string doesn't work right... and if I use something like;

$string4 =~ m,(\s|/)(.+?)\.rules, and my $strmod3 = $2;

it just comes up with the whole string Frown

I've tried my best. Maybe someone could give a hand on the last one :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Question about script with $'s in it. In reply to
Why bother escaping $RULE_PATH?....just use '' instead of "" to stop interpolation.
Quote Reply
Re: [Paul] Question about script with $'s in it. In reply to
That did the trick.



Thanks for your help guys.