[24046] in Perl-Users-Digest
Perl-Users Digest, Issue: 6243 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Mar 10 18:06:27 2004
Date: Wed, 10 Mar 2004 15:05:07 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 10 Mar 2004 Volume: 10 Number: 6243
Today's topics:
Causing Perl to issue a single "write" system call? <mankypro@yahoo.com>
Re: Causing Perl to issue a single "write" system call? <usenet@morrow.me.uk>
Re: Causing Perl to issue a single "write" system call? <mankypro@yahoo.com>
IF statement case sensitivity <connell@freebreakfast.co.uk>
Re: IF statement case sensitivity <mdudley@execonn.com>
Re: IF statement case sensitivity <connell@freebreakfast.co.uk>
Re: IF statement case sensitivity <usenet@morrow.me.uk>
Re: IF statement case sensitivity <tadmc@augustmail.com>
Re: Quoting non-numbers <krahnj@acm.org>
Re: Quoting non-numbers <usenet@morrow.me.uk>
reading regexpr from keyboard? <jwall@t-online.de>
Re: reading regexpr from keyboard? <ittyspam@yahoo.com>
Re: reading regexpr from keyboard? <tadmc@augustmail.com>
Regex doesn't match - what am I doing wrong? (Sriram)
Simple Reference Question (Len)
Re: Simple Reference Question <usenet@morrow.me.uk>
Syntax error <mdudley@execonn.com>
Re: Syntax error <ittyspam@yahoo.com>
Re: Syntax error <mdudley@execonn.com>
Re: Syntax error <usenet@morrow.me.uk>
Re: Syntax error <ittyspam@yahoo.com>
Re: Syntax error <tore@aursand.no>
using an assoc. array as a 'set' (ixtahdoom)
Re: using an assoc. array as a 'set' <tassilo.parseval@rwth-aachen.de>
variable initialization question <stremitz@consultant.com>
Re: variable initialization question <ittyspam@yahoo.com>
Re: variable initialization question <krahnj@acm.org>
Re: variable initialization question <stremitz@consultant.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 10 Mar 2004 19:23:49 GMT
From: Doc Miller <mankypro@yahoo.com>
Subject: Causing Perl to issue a single "write" system call?
Message-Id: <5jou40lpg216huahugdssm51rs7q7bqqsn@4ax.com>
I have a Perl script that writes a single entry to a log file and am
concerned with concurrent writes to the file by multiple instances of the
script.
I know that if you open a file for "append" on Unix (and I believe Windows)
the OS will guarantee that each write is at the end of the file.
Is there any way to cause Perl to issue a single "write" system call?
I would prefer not to need to deal with locking the log file if at all
possible. I want to keep this script as fast and lightweight as I can.
-Doc
------------------------------
Date: Wed, 10 Mar 2004 20:21:24 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Causing Perl to issue a single "write" system call?
Message-Id: <c2ntc4$b4r$1@wisteria.csv.warwick.ac.uk>
Quoth nowhere@here.com:
> I have a Perl script that writes a single entry to a log file and am
> concerned with concurrent writes to the file by multiple instances of the
> script.
>
> I know that if you open a file for "append" on Unix (and I believe Windows)
> the OS will guarantee that each write is at the end of the file.
It will guarentee that the write is at the end of the file; it will not
guarentee that the write is atomic, though. That is, you are guaranteed
that all the data you write goes into the file (and won't be overwritten
by someone else), but there are no guarantees about interleaving.
Actually, my reading of SUS3 doesn't even guarantee that: all it says is
| If the O_APPEND flag of the file status flags is set, the file offset
| shall be set to the end of the file prior to each write and no
| intervening file modification operation shall occur between changing
| the file offset and the write operation.
As it also says
| This volume of IEEE Std 1003.1-2001 does not specify behavior of
| concurrent writes to a file from multiple processes. Applications
| should use some form of concurrency control.
, all that is (appears to me to be) guaranteed is that the first byte
written will be appended. Subsequent bytes could well interleave with,
and thus overwrite, other processes appending to the file.
As a matter of fact, Linux appears to perform both write(2) and
writev(2) atomically for 'normal' filesystems, but I could be misreading
the code.
> Is there any way to cause Perl to issue a single "write" system call?
syswrite calls write(2) directly.
Ben
--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
ben@morrow.me.uk (Kate Rusby)
------------------------------
Date: Wed, 10 Mar 2004 22:07:56 GMT
From: Doc Miller <mankypro@yahoo.com>
Subject: Re: Causing Perl to issue a single "write" system call?
Message-Id: <gp0v40t92m25epd0m3c6mkgiotj3q3r5je@4ax.com>
On Wed, 10 Mar 2004 20:21:24 +0000 (UTC), Ben Morrow <usenet@morrow.me.uk>
appears to have written:
[snip snip]
>As a matter of fact, Linux appears to perform both write(2) and
>writev(2) atomically for 'normal' filesystems, but I could be misreading
>the code.
>
>> Is there any way to cause Perl to issue a single "write" system call?
>
>syswrite calls write(2) directly.
>
>Ben
That is how I read it too. Since I am only writing a single line I suspect
write(2) will work but I'm not going to bank on it. I'll run a couple of
tests and if it doesn't work I'll go back to my flock() routiines.
-Doc
------------------------------
Date: Wed, 10 Mar 2004 19:21:00 -0000
From: "Connell Gauld" <connell@freebreakfast.co.uk>
Subject: IF statement case sensitivity
Message-Id: <c2npk3$efb$1@news8.svr.pol.co.uk>
Hey,
How do I change the following IF statement to make it case-insensitive?
if ($example eq "text"){
......
}
------------------------------
Date: Wed, 10 Mar 2004 13:42:24 -0500
From: Marshall Dudley <mdudley@execonn.com>
Subject: Re: IF statement case sensitivity
Message-Id: <404F6190.294F46EF@execonn.com>
if (lc $example eq "text")
if (uc $example eq "TEST")
if ($example =~ /^text$/i)
all should work.
Marshall
Connell Gauld wrote:
> Hey,
> How do I change the following IF statement to make it case-insensitive?
>
> if ($example eq "text"){
> ......
> }
------------------------------
Date: Wed, 10 Mar 2004 19:24:12 -0000
From: "Connell Gauld" <connell@freebreakfast.co.uk>
Subject: Re: IF statement case sensitivity
Message-Id: <c2npq4$4lj$1@newsg1.svr.pol.co.uk>
"Marshall Dudley" <mdudley@execonn.com> wrote in message
news:404F6190.294F46EF@execonn.com...
> if (lc $example eq "text")
>
> if (uc $example eq "TEST")
>
> if ($example =~ /^text$/i)
>
> all should work.
>
> Marshall
>
> Connell Gauld wrote:
>
> > Hey,
> > How do I change the following IF statement to make it case-insensitive?
> >
> > if ($example eq "text"){
> > ......
> > }
>
Thats great thanks :)
------------------------------
Date: Wed, 10 Mar 2004 19:59:00 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: IF statement case sensitivity
Message-Id: <c2ns24$a3l$1@wisteria.csv.warwick.ac.uk>
Quoth Marshall Dudley <mdudley@execonn.com>:
> if (lc $example eq "text")
>
> if (uc $example eq "TEST")
In a Unicode environment lc is better than uc (Unicode defines three
cases: lower, upper and title, and mappings upper<->lower and
title<->lower but not title->upper).
Ben
--
Every twenty-four hours about 34k children die from the effects of poverty.
Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
that image, that ghastly, grey-billowing, double-barrelled fall, repeated
twelve times every day. Full of children. [Iain Banks] ben@morrow.me.uk
------------------------------
Date: Wed, 10 Mar 2004 16:25:37 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: IF statement case sensitivity
Message-Id: <slrnc4v5f1.259.tadmc@magna.augustmail.com>
[ upside-down text rearranged.
Please do not post upside-down.
]
Marshall Dudley <mdudley@execonn.com> wrote:
> Connell Gauld wrote:
>> How do I change the following IF statement to make it case-insensitive?
>>
>> if ($example eq "text"){
>> ......
>> }
>
> if (lc $example eq "text")
>
> if (uc $example eq "TEST")
>
> if ($example =~ /^text$/i)
>
> all should work.
That last one is not equivalent though.
The others will match only one string, "text".
The last will also match "text\n"...
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 10 Mar 2004 21:29:46 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Quoting non-numbers
Message-Id: <404F88A6.4AEF6E32@acm.org>
Stevens wrote:
>
> I'm trying to create a regular expression that takes a list of space
> delimited words and numbers and encloses every non-number within single
> quotes.
>
> e.g 1967 foo 3.234 system_analyst 87834/d23434 would be returned as 1967
> 'foo' 3.234 'system_analyst' '87834/d23434'
>
> I can't get the syntax right ... how could you do this?
s/(\S+)/ $1 eq $1 + 0 ? $1 : "'$1'" /eg;
John
--
use Perl;
program
fulfillment
------------------------------
Date: Wed, 10 Mar 2004 21:39:32 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Quoting non-numbers
Message-Id: <c2o1uk$dja$1@wisteria.csv.warwick.ac.uk>
Quoth "John W. Krahn" <krahnj@acm.org>:
> Stevens wrote:
> >
> > I'm trying to create a regular expression that takes a list of space
> > delimited words and numbers and encloses every non-number within single
> > quotes.
> >
> > e.g 1967 foo 3.234 system_analyst 87834/d23434 would be returned as 1967
> > 'foo' 3.234 'system_analyst' '87834/d23434'
> >
> > I can't get the syntax right ... how could you do this?
>
{
no warnings 'numeric';
> s/(\S+)/ $1 eq $1 + 0 ? $1 : "'$1'" /eg;
}
Ben
--
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. ben@morrow.me.uk
------------------------------
Date: Wed, 10 Mar 2004 21:44:51 +0100
From: "Jochen Wall" <jwall@t-online.de>
Subject: reading regexpr from keyboard?
Message-Id: <c2nuo1$624$06$1@news.t-online.com>
Hi!
I'm reading two reg. expressions from keyboard.
Then I pass them to a subprogram.
But the result is not what I expect.
e.g. if I type in
str = test.pl
reg1 = \.pl
reg2 = \.txt
I will get "test.\txt" instead of "test.txt".
If I put the reg. expressions directly into the subprogramm
$str = s/\.pl/\.txt/g;
I will get "test.txt".
What's wrong with the reg. expression in the subprogram?
here's the program:
#! /usr/bin/perl
print "str = ";
my $str = <STDIN>;
chomp($str);
print "reg1 = ";
my $reg1 = <STDIN>;
chomp($reg1);
print "reg2 = ";
my $reg2 = <STDIN>;
chomp($reg2);
print ReplaceString($str,$reg1,$reg2),"\n";
sub ReplaceString
{
my $str = $_[0];
my $reg1 = $_[1];
my $reg2 = $_[2];
$str =~ s/$reg1/$reg2/g;
return $str;
}
------------------------------
Date: Wed, 10 Mar 2004 16:03:15 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: reading regexpr from keyboard?
Message-Id: <20040310155838.A27834@dishwasher.cs.rpi.edu>
On Wed, 10 Mar 2004, Jochen Wall wrote:
> Hi!
>
> I'm reading two reg. expressions from keyboard.
> Then I pass them to a subprogram.
> But the result is not what I expect.
>
> e.g. if I type in
>
> str = test.pl
> reg1 = \.pl
> reg2 = \.txt
>
> I will get "test.\txt" instead of "test.txt".
> If I put the reg. expressions directly into the subprogramm
> $str = s/\.pl/\.txt/g;
> I will get "test.txt".
>
> What's wrong with the reg. expression in the subprogram?
>
> here's the program:
>
> #! /usr/bin/perl
>
> print "str = ";
> my $str = <STDIN>;
> chomp($str);
>
> print "reg1 = ";
> my $reg1 = <STDIN>;
> chomp($reg1);
>
> print "reg2 = ";
> my $reg2 = <STDIN>;
> chomp($reg2);
>
> print ReplaceString($str,$reg1,$reg2),"\n";
>
> sub ReplaceString
> {
> my $str = $_[0];
> my $reg1 = $_[1];
> my $reg2 = $_[2];
>
> $str =~ s/$reg1/$reg2/g;
>
> return $str;
> }
>
You shouldn't backslash the . (or most other special characters) in the
replacement portion of s///; That's because the replacement is not a
regular expression, it's a string. Therefore, when you enter your second
regexp in the code, you should be entering just
.txt
instead of
\.txt
Paul Lalli
------------------------------
Date: Wed, 10 Mar 2004 16:22:32 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: reading regexpr from keyboard?
Message-Id: <slrnc4v598.259.tadmc@magna.augustmail.com>
Jochen Wall <jwall@t-online.de> wrote:
> str = test.pl
> reg1 = \.pl
> reg2 = \.txt
>
> I will get "test.\txt" instead of "test.txt".
> What's wrong with the reg. expression in the subprogram?
> $str =~ s/$reg1/$reg2/g;
There is nothing wrong with the reg. expression in the program.
There is something wrong with what you input into the program.
You _told_ it to put a backslash (and a dot and a "t" and a "x" and a "t")
in the replacement.
If you don't want a backslash don't enter a backslash.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 10 Mar 2004 14:53:09 -0800
From: mailsb04-posts@yahoo.com (Sriram)
Subject: Regex doesn't match - what am I doing wrong?
Message-Id: <1bada5a3.0403101453.7c681fad@posting.google.com>
Hi,
I am having trouble matching a regex that combines a negated character
class and an anchor ($). Basically, I want to match all strings that
don't end in a digit. So I tried:
bash-2.05a@bermuda:15$perl -e 'while (<STDIN>) { if (/[^0-9]$/) {
print;}}'
skdsklds
skdsklds
sklskl2 <== why does this match? it ends in a digit.
sklskl2
This matched all strings regardless of whether or not they ended in a
digit. But the complemented regex seems to work fine:
bash-2.05a@bermuda:13$perl -e 'while (<STDIN>) { if (/[0-9]$/) {
print;}}'
sdkldsklds2
sdkldsklds2
sdsk2
sdsk2
sks <==== doesn't match as expected
I replaced [0-9] with [\d] but got the same results.
On the other hand, grep works as expected:
bash-2.05a@bermuda:9$grep '[0-9]$'
sdksdjk2
sdksdjk2
22221
22221
sdjkdsjk <== doesn't match as expected
bash-2.05a@bermuda:11$grep '[^0-9]$'
sdklsdklds
sdklsdklds
sdkldslk2 <== doesn't match as expected
What am I doing wrong? Here's the perl version info:
bash-2.05a@bermuda:17$perl -V
Summary of my perl5 (revision 5.0 version 6 subversion 1)
configuration:
Platform:
osname=linux, osvers=2.4.17-0.13smp, archname=i386-linux
uname='linux daffy.perf.redhat.com 2.4.17-0.13smp #1 smp fri feb 1
10:30:48 est 2002 i686 unknown '
config_args='-des -Doptimize=-O2 -march=i386 -mcpu=i686 -Dcc=gcc
-Dcf_by=Red Hat, Inc. -Dcccdlflags=-fPIC -Dinstallprefix=/usr
-Dprefix=/usr -Darchname=i386-linux -
Dvendorprefix=/usr -Dsiteprefix=/usr -Uusethreads -Uuseithreads
-Uuselargefiles -Dd_dosuid -Dd_semctl_semun -Di_db -Di_ndbm -Di_gdbm
-Di_shadow -Di_syslog -Dman3ext=3pm
'
hint=recommended, useposix=true, d_sigaction=define
usethreads=undef use5005threads=undef useithreads=undef
usemultiplicity=undef
useperlio=undef d_sfio=undef uselargefiles=undef usesocks=undef
use64bitint=undef use64bitall=undef uselongdouble=undef
Compiler:
cc='gcc', ccflags ='-fno-strict-aliasing -I/usr/local/include',
optimize='-O2 -march=i386 -mcpu=i686',
cppflags='-fno-strict-aliasing -I/usr/local/include'
ccversion='', gccversion='2.96 20000731 (Red Hat Linux 7.2
2.96-109)', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
d_longlong=define, longlongsize=8, d_longdbl=define,
longdblsize=12
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=4
alignbytes=4, usemymalloc=n, prototype=define
Linker and Libraries:
ld='gcc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib
libs=-lnsl -ldl -lm -lc -lcrypt -lutil
perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil
libc=/lib/libc-2.2.5.so, so=so, useshrplib=false,
libperl=libperl.a
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef,
ccdlflags='-rdynamic'
cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'
Characteristics of this binary (from libperl):
Compile-time options:
Built under linux
Compiled at Apr 1 2002 12:23:22
@INC:
/usr/lib/perl5/5.6.1/i386-linux
/usr/lib/perl5/5.6.1
/usr/lib/perl5/site_perl/5.6.1/i386-linux
/usr/lib/perl5/site_perl/5.6.1
/usr/lib/perl5/site_perl/5.6.0
/usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.6.1/i386-linux
/usr/lib/perl5/vendor_perl/5.6.1
/usr/lib/perl5/vendor_perl
.
Thanks for your help,
Sriram
------------------------------
Date: 10 Mar 2004 12:20:27 -0800
From: lpjacob@yahoo.com (Len)
Subject: Simple Reference Question
Message-Id: <d7c81eae.0403101220.6b171bee@posting.google.com>
Ok. Newbie question here. Can someone explain why the first example
works, and why the second example doesnt. And then, if you could,
please let me know how to make the second example work correctly.
Example 1 - This one works as intended
--------------------------------------
@list1 = ("list2", "list3");
@list2 = ("list2item1", "list2item2");
foreach my $tmpitem ( @list1 ) {
print "$tmpitem\n";
next if ! defined( @$tmpitem );
foreach ( @$tmpitem ) {
print "\t$_\n";
}
}
Example 2 - This does not print out the contents of the list2 array
-------------------------------------------------------------------
use strict;
my @list1 = ("list2", "list3");
my @list2 = ("list2item1", "list2item2");
foreach my $tmpitem ( @list1 ) {
print "$tmpitem\n";
next if ! defined( @$tmpitem );
foreach ( @$tmpitem ) {
print "\t$_\n";
}
}
Thanks.
Len.
------------------------------
Date: Wed, 10 Mar 2004 20:30:50 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Simple Reference Question
Message-Id: <c2nttp$b4r$2@wisteria.csv.warwick.ac.uk>
Quoth lpjacob@yahoo.com (Len):
> Ok. Newbie question here. Can someone explain why the first example
> works, and why the second example doesnt. And then, if you could,
> please let me know how to make the second example work correctly.
>
> Example 1 - This one works as intended
> --------------------------------------
> @list1 = ("list2", "list3");
> @list2 = ("list2item1", "list2item2");
>
> foreach my $tmpitem ( @list1 ) {
> print "$tmpitem\n";
> next if ! defined( @$tmpitem );
> foreach ( @$tmpitem ) {
> print "\t$_\n";
> }
> }
This uses symrefs: you are using a string holding a variable's name to
get at the variable. This can lead to very hard-to-find bugs, which is
why use strict disallows it (and why we insist you use strict :).
You want to either use real references (see perldoc perlreftut, for
starters):
my @list2 = ("list2item1", "list2item2");
my @list3 = ("list3item1", "list3item2");
my @list1 = (\@list2, \@list3);
# code as before
or a hash-of-lists:
my %lists = (
list2 => [ qw(list2item1 list2item2) ],
list3 => [ qw(list3item1 list3item1) ],
);
my @list1 = qw(list2 list3);
$\ = "\n";
for my $tmp (@list1) {
print $tmpitem;
next unless exists $lists{$tmp};
for ( @{ $lists{$tmp} } ) {
print "\t$_";
}
}
See perlreftut again for both the [...] and the @{...} syntax.
As side issues, note how my use of qw// and $\ makes the code clearer.
Ben
--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
ben@morrow.me.uk The Levellers, 'Believers'
------------------------------
Date: Wed, 10 Mar 2004 13:34:01 -0500
From: Marshall Dudley <mdudley@execonn.com>
Subject: Syntax error
Message-Id: <404F5F99.78B76189@execonn.com>
Why won't this compile, and what can I do to make it compile?
$match_word =~ s/\$/\\$/g;
syntax error at ./library/commerce_db_lib.pl line 652, near "=~
s/\$/\\$/g"
Final $ should be \$ or $name at ./library/commerce_db_lib.pl line 681,
within string
All I am wanting to do is replace any $'s in the string with \$, so that
the comparison later will understand it is a literal $ and not a
variable name.
Thanks,
Marshall
------------------------------
Date: Wed, 10 Mar 2004 14:28:49 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Syntax error
Message-Id: <20040310142358.R27834@dishwasher.cs.rpi.edu>
On Wed, 10 Mar 2004, Marshall Dudley wrote:
> Why won't this compile, and what can I do to make it compile?
>
> $match_word =~ s/\$/\\$/g;
>
> syntax error at ./library/commerce_db_lib.pl line 652, near "=~
> s/\$/\\$/g"
> Final $ should be \$ or $name at ./library/commerce_db_lib.pl line 681,
> within string
>
> All I am wanting to do is replace any $'s in the string with \$, so that
> the comparison later will understand it is a literal $ and not a
> variable name.
First, did you try doing exactly what the compiler told you to do? Make
the final $ be \$
$match_word =~ s/\$/\\\$/g;
Second, why are you doing this? From what you've said, you have a string
that has litteral dollar signs in it, and you're somehow worried about
this being interpreted as a variable name. That will only happen if you
eval() the string. You're not doing that, are you? Variables are only
interpreted when they're littlerally typed into the code, not just when
they're contained within a string.
For example:
-----------------------------------
#!/usr/bin/perl
use strict;
use warnings;
my $bar = "hello world";
print "Enter a string\n";
my $string = <STDIN>;
#assume user enters: Foo $bar Baz
print "$string\n";
------------------------------------
This script will print:
Foo $bar Baz
NOT:
Foo hello world Baz
Paul Lalli
------------------------------
Date: Wed, 10 Mar 2004 14:45:42 -0500
From: Marshall Dudley <mdudley@execonn.com>
Subject: Re: Syntax error
Message-Id: <404F7066.5CD368A2@execonn.com>
Paul Lalli wrote:
> On Wed, 10 Mar 2004, Marshall Dudley wrote:
>
> > Why won't this compile, and what can I do to make it compile?
> >
> > $match_word =~ s/\$/\\$/g;
> >
> > syntax error at ./library/commerce_db_lib.pl line 652, near "=~
> > s/\$/\\$/g"
> > Final $ should be \$ or $name at ./library/commerce_db_lib.pl line 681,
> > within string
> >
> > All I am wanting to do is replace any $'s in the string with \$, so that
> > the comparison later will understand it is a literal $ and not a
> > variable name.
>
> First, did you try doing exactly what the compiler told you to do? Make
> the final $ be \$
> $match_word =~ s/\$/\\\$/g;
I thought that did not work when I tried it, but tried again, and I was wrong.
>
>
> Second, why are you doing this? From what you've said, you have a string
> that has litteral dollar signs in it, and you're somehow worried about
> this being interpreted as a variable name. That will only happen if you
> eval() the string. You're not doing that, are you? Variables are only
> interpreted when they're littlerally typed into the code, not just when
> they're contained within a string.
>
> For example:
>
> -----------------------------------
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $bar = "hello world";
> print "Enter a string\n";
> my $string = <STDIN>;
>
> #assume user enters: Foo $bar Baz
> print "$string\n";
> ------------------------------------
>
> This script will print:
> Foo $bar Baz
> NOT:
> Foo hello world Baz
>
> Paul Lalli
Well, putting that line in the code does make it work now, where before it did
not:
$match_word = '$2.50';
#$match_word =~ s/\$/\\\$/g; commented out, does not work
print "$match_word\n";
$line = 'The price is $2.50 cost';
if ($line =~ $match_word) {
print "matched\n";
} else {
print "not matched\n";
}
perl test.pl
$2.50
not matched
$match_word = '$2.50';
$match_word =~ s/\$/\\\$/g;
print "$match_word\n";
$lline = 'The price is $2.50 cost';
if ($line =~ $match_word) {
print "matched\n";
} else {
print "not matched\n";
}
perl test.pl
\$2.50
matched
So the \ IS necessary to make it not treat the $ as a variable name.
Thanks,
Marshall
------------------------------
Date: Wed, 10 Mar 2004 20:36:18 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Syntax error
Message-Id: <c2nu82$b4r$3@wisteria.csv.warwick.ac.uk>
Quoth Marshall Dudley <mdudley@execonn.com>:
> Well, putting that line in the code does make it work now, where before it
> did not:
>
> $match_word = '$2.50';
> #$match_word =~ s/\$/\\\$/g; commented out, does not work
> print "$match_word\n";
> $line = 'The price is $2.50 cost';
> if ($line =~ $match_word) {
> print "matched\n";
> } else {
> print "not matched\n";
> }
>
> perl test.pl
> $2.50
> not matched
>
> $match_word = '$2.50';
> $match_word =~ s/\$/\\\$/g;
> print "$match_word\n";
> $lline = 'The price is $2.50 cost';
> if ($line =~ $match_word) {
> print "matched\n";
> } else {
> print "not matched\n";
> }
>
> perl test.pl
> \$2.50
> matched
>
> So the \ IS necessary to make it not treat the $ as a variable name.
No, it's necessary to make the regex engine not treat it as an
end-of-line match. A better solution is to use quotemeta or the \Q
escape:
my $match_word = '$2.50'; # why aren't you using strict?
$\ = "\n"; # makes things easier to follow
print $match_word;
my $line = 'The price is $2.50 cost'; # never mind the bad English... :)
if ($line =~ /\Q$match_word/) {
print "matched";
}
else {
print "not matched";
}
Ben
--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces molit animos, tristesque mentes erigit. | ben@morrow.me.uk
Musica vel ipsas arbores et horridas movet feras. |
------------------------------
Date: Wed, 10 Mar 2004 15:45:38 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Syntax error
Message-Id: <20040310154328.P27834@dishwasher.cs.rpi.edu>
On Wed, 10 Mar 2004, Marshall Dudley wrote:
> Paul Lalli wrote:
>
> > On Wed, 10 Mar 2004, Marshall Dudley wrote:
>
> Well, putting that line in the code does make it work now, where before it did
> not:
>
> $match_word = '$2.50';
> #$match_word =~ s/\$/\\\$/g; commented out, does not work
> print "$match_word\n";
> $line = 'The price is $2.50 cost';
> if ($line =~ $match_word) {
> print "matched\n";
> } else {
> print "not matched\n";
> }
>
> perl test.pl
> $2.50
> not matched
>
> $match_word = '$2.50';
> $match_word =~ s/\$/\\\$/g;
> print "$match_word\n";
> $lline = 'The price is $2.50 cost';
> if ($line =~ $match_word) {
> print "matched\n";
> } else {
> print "not matched\n";
> }
>
> perl test.pl
> \$2.50
> matched
>
> So the \ IS necessary to make it not treat the $ as a variable name.
>
See, you didn't say you wanted to use the string later in a regular
expression. That's different. I'm still correct, however. :) The $ will
is not being treated as a variable indication. It's being treated as the
regular expression 'end-of-line' marker. Whenever you have a variable in
your RegExp, it's a good idea to quote-meta it:
if ($line =~ /\Q$match_word\E/) { ... }
Paul Lalli
------------------------------
Date: Wed, 10 Mar 2004 23:18:57 +0100
From: Tore Aursand <tore@aursand.no>
Subject: Re: Syntax error
Message-Id: <pan.2004.03.10.19.35.01.720239@aursand.no>
On Wed, 10 Mar 2004 13:34:01 -0500, Marshall Dudley wrote:
> Why won't this compile, and what can I do to make it compile?
>
> $match_word =~ s/\$/\\$/g;
Try this instead:
$match_word =~ s,\$,\\\$,;
In other words: You have to escape the '\\$', as '$' refers to 'at the
end of'.
Feel free to use a slash instead of the commas; I get psycho when I see
too many \/\/\/\/. :)
--
Tore Aursand <tore@aursand.no>
"Nothing is certain but death and taxes. Of the two, taxes happen
annually." -- Joel Fox
------------------------------
Date: 10 Mar 2004 14:07:57 -0800
From: ixtahdoom@yahoo.com (ixtahdoom)
Subject: using an assoc. array as a 'set'
Message-Id: <2aa9aa08.0403101407.527c363d@posting.google.com>
Allo,
I find myself doing this quite a bit in perl:
my %h;
:
:
{
$h{$k} = 1; # don't care about the value, only the key
}
:
Simply because I want to use an assoc. array as a set, i don't care
about the value, only the key. Is there a way to do something like
define $h{$k}
??
This seems more intuitive than having to assign an unused value to a
key.
Yes, I could use a sorted list, but I'd rather avoid the
grep/sorted_insert and use a %.
Thanks!!!
-itd
------------------------------
Date: 10 Mar 2004 22:27:17 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: using an assoc. array as a 'set'
Message-Id: <c2o4o5$ajc$1@nets3.rz.RWTH-Aachen.DE>
Also sprach ixtahdoom:
> I find myself doing this quite a bit in perl:
>
> my %h;
> :
> :
> {
> $h{$k} = 1; # don't care about the value, only the key
> }
> :
>
>
> Simply because I want to use an assoc. array as a set, i don't care
> about the value, only the key. Is there a way to do something like
>
> define $h{$k}
>
> ??
>
> This seems more intuitive than having to assign an unused value to a
> key.
Here is a counter-intuitive (but working) way:
undef $h{$k};
Or you just assign undef in one or the other way:
$h{$k} = undef;
# or
$h{$k} = ();
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Wed, 10 Mar 2004 16:09:32 -0500
From: Alexander Stremitzer <stremitz@consultant.com>
Subject: variable initialization question
Message-Id: <FoL3c.7518$xL3.215@bignews1.bellsouth.net>
I found that the following statement only initializes the first variable.
my ($a,$b,$c) = 0;
A way that works is:
my ($a,$b,$c) = (0,0,0);
Is there a simpler way to initialize all variables with the same value 0 ?
Thanks,
Alex
------------------------------
Date: Wed, 10 Mar 2004 16:15:20 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: variable initialization question
Message-Id: <20040310161242.V27834@dishwasher.cs.rpi.edu>
On Wed, 10 Mar 2004, Alexander Stremitzer wrote:
> I found that the following statement only initializes the first variable.
> my ($a,$b,$c) = 0;
> A way that works is:
> my ($a,$b,$c) = (0,0,0);
>
> Is there a simpler way to initialize all variables with the same value 0 ?
Yes, but before I answer that, you should be asking yourself why you're
doing that. Perl variables are undef by default, which is treated as 0 in
numeric context. There's generally no reason to initialize a variable to
0.
Regardless, however, you can use the x operator:
my ($a, $b, $c) = (0) x 3;
In a list context, the x operator takes a list on the lefthand side and
produces a list of elements consisting of the lefthand argument replicated
the righthand argument's number of times.
Paul Lalli
------------------------------
Date: Wed, 10 Mar 2004 21:44:34 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: variable initialization question
Message-Id: <404F8C1F.18F11CB6@acm.org>
Alexander Stremitzer wrote:
>
> I found that the following statement only initializes the first variable.
> my ($a,$b,$c) = 0;
> A way that works is:
> my ($a,$b,$c) = (0,0,0);
>
> Is there a simpler way to initialize all variables with the same value 0 ?
my ($a,$b,$c) = (0) x 3;
John
--
use Perl;
program
fulfillment
------------------------------
Date: Wed, 10 Mar 2004 22:33:25 GMT
From: Alexander Stremitzer <stremitz@consultant.com>
Subject: Re: variable initialization question
Message-Id: <4031a35052758e6aabef92e7af43eac2@news.teranews.com>
Paul Lalli wrote:
> On Wed, 10 Mar 2004, Alexander Stremitzer wrote:
>
>
>>I found that the following statement only initializes the first variable.
>>my ($a,$b,$c) = 0;
>>A way that works is:
>>my ($a,$b,$c) = (0,0,0);
>>
>>Is there a simpler way to initialize all variables with the same value 0 ?
>
>
>
> Yes, but before I answer that, you should be asking yourself why you're
> doing that. Perl variables are undef by default, which is treated as 0 in
> numeric context. There's generally no reason to initialize a variable to
> 0.
The reason I do this is to avoid warning messages. Of course this is
only a simplified version of my real code.
use strict;
use warnings;
my ($a,$b,$c) = 0;
print $c;
Results in: Use of uninitialized value in print ...
>
> Regardless, however, you can use the x operator:
>
> my ($a, $b, $c) = (0) x 3;
>
> In a list context, the x operator takes a list on the lefthand side and
> produces a list of elements consisting of the lefthand argument replicated
> the righthand argument's number of times.
>
> Paul Lalli
Thanks for the quick reply,
Alex
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 6243
***************************************