[25219] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 7464 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Nov 30 06:10:55 2004

Date: Tue, 30 Nov 2004 03:10:10 -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           Tue, 30 Nov 2004     Volume: 10 Number: 7464

Today's topics:
    Re: replace words in string with hash values <tadmc@augustmail.com>
    Re: replace words in string with hash values <noreply@gunnar.cc>
    Re: replace words in string with hash values <see@sig.invalid>
    Re: replace words in string with hash values <tadmc@augustmail.com>
        System command <varunjindal@yahoo.com>
    Re: System command (Anno Siegel)
    Re: System command <spamtrap@dot-app.org>
    Re: Unix commands and perl <jwillmore@fastmail.us>
    Re: Unix commands and perl (Anno Siegel)
        why no syntax error? (Ronald Fischer)
    Re: why no syntax error? <spamtrap@dot-app.org>
    Re: why no syntax error? (Anno Siegel)
    Re: why no syntax error? <bernard.el-haginDODGE_THIS@lido-tech.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Mon, 29 Nov 2004 18:27:38 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: replace words in string with hash values
Message-Id: <slrncqnfjq.1fb.tadmc@magna.augustmail.com>

Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
> wana wrote:
>> foreach (keys %h)
>> {
>>   $a =~ s/\b$_\b/$h{$_}/g;
>> }
>> 
>> I want to replace matches in string to hash key with hash value.  I am
>> replacing acronyms with phrases where acronym is hash key.  Is there a
>> better or different way?
> 
> You have Perl search the whole string as many times as there are keys in 
> the hash. With this approach, searching the string once is sufficient:
> 
>      my $keys = join '|', keys %h;
>      $a =~ s/($keys)/$h{$1}/g;


But you better put the word boundaries back in though!


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Tue, 30 Nov 2004 03:28:36 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: replace words in string with hash values
Message-Id: <3124fcF36km4fU1@uni-berlin.de>

Tad McClellan wrote:
> Gunnar Hjalmarsson wrote:
>> wana wrote:
>>> 
>>> foreach (keys %h)
>>> {
>>>   $a =~ s/\b$_\b/$h{$_}/g;
>>> }
>>> 
>>> I want to replace matches in string to hash key with hash value.
>>> I am replacing acronyms with phrases where acronym is hash key.
>>> Is there a better or different way?
>> 
>> You have Perl search the whole string as many times as there are
>> keys in the hash. With this approach, searching the string once is
>> sufficient:
>> 
>>     my $keys = join '|', keys %h;
>>     $a =~ s/($keys)/$h{$1}/g;
> 
> But you better put the word boundaries back in though!

Yes, they were omitted by mistake; thanks for pointing it out.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Mon, 29 Nov 2004 23:04:27 -0500
From: Bob Walton <see@sig.invalid>
Subject: Re: replace words in string with hash values
Message-Id: <41abeec8$1_4@127.0.0.1>

wana wrote:

> foreach (keys %h)
> {
>   $a =~ s/\b$_\b/$h{$_}/g;
> }
> 
> I want to replace matches in string to hash key with hash value.  I am
> replacing acronyms with phrases where acronym is hash key.  Is there a
> better or different way?
> 

The only thing I've got to add to what's already been
said is to watch out for regexp metacharacters in your
hash keys.  I have no idea what you might count as an
"acronym".  If any metacharacters do appear, you can
use \b\Q$_\E\b as your regexp to quote them.
 ...
> wana (on pda)

-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


------------------------------

Date: Mon, 29 Nov 2004 23:37:13 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: replace words in string with hash values
Message-Id: <slrncqo1o9.1rn.tadmc@magna.augustmail.com>

Bob Walton <see@sig.invalid> wrote:
> wana wrote:
> 
>> foreach (keys %h)
>> {
>>   $a =~ s/\b$_\b/$h{$_}/g;
>> }
>> 
>> I want to replace matches in string to hash key with hash value.

> The only thing I've got to add to what's already been
> said is to watch out for regexp metacharacters in your
> hash keys.  I have no idea what you might count as an
> "acronym".  If any metacharacters do appear, you can
> use \b\Q$_\E\b as your regexp to quote them.


But if a metachar could be the first or last char, then
the word boundary probably won't match where you want it to...


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: 30 Nov 2004 02:30:00 -0800
From: "Varun Jindal" <varunjindal@yahoo.com>
Subject: System command
Message-Id: <1101810600.015345.187340@z14g2000cwz.googlegroups.com>

hello,

i am using a unix shell command within my perl script using system
command.

lets say, i want to know what is hte system time (it is just an
example).

so i use the system command, but i want to put the output of the
command in a variable in the perl script.
how do i do it.

thanks in advance,

--Varun.



------------------------------

Date: 30 Nov 2004 10:52:03 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: System command
Message-Id: <cohjcj$sde$2@mamenchi.zrz.TU-Berlin.DE>

Varun Jindal <varunjindal@yahoo.com> wrote in comp.lang.perl.misc:
> hello,
> 
> i am using a unix shell command within my perl script using system
> command.
> 
> lets say, i want to know what is hte system time (it is just an
> example).
> 
> so i use the system command, but i want to put the output of the
> command in a variable in the perl script.
> how do i do it.

Read the documentation of the function you are using, in particular
the third paragraph.  perldoc -f system.

Anno


------------------------------

Date: Tue, 30 Nov 2004 06:00:33 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: System command
Message-Id: <T_GdnXpg49tPzzHcRVn-iw@adelphia.com>

Varun Jindal wrote:

> so i use the system command, but i want to put the output of the
> command in a variable in the perl script.

The docs for the system() function say, in part, "This is not what you 
want to use to capture the output from a command, for that you should 
use ..."

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


------------------------------

Date: Mon, 29 Nov 2004 20:12:30 -0500
From: James Willmore <jwillmore@fastmail.us>
Subject: Re: Unix commands and perl
Message-Id: <pan.2004.11.30.01.12.30.575917@fastmail.us>

On Mon, 29 Nov 2004 22:18:38 +0100, Michele Dondi wrote:

> On Mon, 29 Nov 2004 12:37:19 -0500, James Willmore
> <jwillmore@fastmail.us> wrote:
> 
>>[jim@localhost jim]$ perl -e 'print [getgrgid($<)]->[0],"\n";'
> 
> Not that I find it disturbing or incorrect, but what is this business
> of referencing and dereferencing?
> 
>   perl -le 'print +(getgrgid $<)[0]'

That's better.  I always have to work a little bit harder than I need to :-)

Jim


------------------------------

Date: 30 Nov 2004 11:04:38 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Unix commands and perl
Message-Id: <cohk46$sde$3@mamenchi.zrz.TU-Berlin.DE>

James Willmore  <jwillmore@fastmail.us> wrote in comp.lang.perl.misc:
> On Mon, 29 Nov 2004 22:18:38 +0100, Michele Dondi wrote:
> 
> > On Mon, 29 Nov 2004 12:37:19 -0500, James Willmore
> > <jwillmore@fastmail.us> wrote:
> > 
> >>[jim@localhost jim]$ perl -e 'print [getgrgid($<)]->[0],"\n";'
> > 
> > Not that I find it disturbing or incorrect, but what is this business
> > of referencing and dereferencing?
> > 
> >   perl -le 'print +(getgrgid $<)[0]'
> 
> That's better.  I always have to work a little bit harder than I need to :-)

That's what getgrgid() returns in scalar context anyway:

    perl -le 'print scalar getgrgid $<'

is a little longer, but conceptually simpler.

Anno


------------------------------

Date: 30 Nov 2004 02:05:31 -0800
From: ronaldf@eml.cc (Ronald Fischer)
Subject: why no syntax error?
Message-Id: <219750c.0411300205.19d62317@posting.google.com>

I recently wrote the following nonsense:

  if($arg = ~ /^\d{4}$/) { ... }

In case you don't see it immediately: I had erroneously typed a space between
the equal sign and the tilde.

To my surprise, this statement was executed without error message. $arg was set
to 294967295.

The tilde did bitwise complement of course, but on what? On a regexp? This
does not seem to make sense to me....

This was executed under Perl 5.6.1

Ronald


------------------------------

Date: Tue, 30 Nov 2004 05:18:42 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: why no syntax error?
Message-Id: <Mb-dnf4xBp-e1DHcRVn-hg@adelphia.com>

Ronald Fischer wrote:

> I recently wrote the following nonsense:
> 
>   if($arg = ~ /^\d{4}$/) { ... }
> 
> The tilde did bitwise complement of course, but on what? On a regexp? This
> does not seem to make sense to me....

Pattern matching implicitly uses $_ if no other variable is used. I 
expect you'd get the same results from this:

if ($arg = ~ ($_ =~ /^\d{4}$/)) { ... }

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


------------------------------

Date: 30 Nov 2004 10:21:18 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: why no syntax error?
Message-Id: <cohhiu$sde$1@mamenchi.zrz.TU-Berlin.DE>

Ronald Fischer <ronaldf@eml.cc> wrote in comp.lang.perl.misc:
> I recently wrote the following nonsense:
> 
>   if($arg = ~ /^\d{4}$/) { ... }
> 
> In case you don't see it immediately: I had erroneously typed a space between
> the equal sign and the tilde.
> 
> To my surprise, this statement was executed without error message. $arg was set
> to 294967295.
> 
> The tilde did bitwise complement of course, but on what? On a regexp? This
> does not seem to make sense to me....

A regex that isn't bound by =~ applies to $_.  That is what happens
here.  The result of that match in scalar context is inverted and
assigned to $arg.

Anno


------------------------------

Date: Tue, 30 Nov 2004 11:20:21 +0100
From: "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net>
Subject: Re: why no syntax error?
Message-Id: <Xns95B1735AAB55Belhber1lidotechnet@62.89.127.66>

ronaldf@eml.cc (Ronald Fischer) wrote:

> I recently wrote the following nonsense:
> 
>   if($arg = ~ /^\d{4}$/) { ... }
> 
> In case you don't see it immediately: I had erroneously typed a
> space between the equal sign and the tilde.
> 
> To my surprise, this statement was executed without error message.
> $arg was set to 294967295.
> 
> The tilde did bitwise complement of course, but on what? On a
> regexp?

No, on what the match operator returned.


-- 
Cheers,
Bernard


------------------------------

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 7464
***************************************


home help back first fref pref prev next nref lref last post