[28557] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9921 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 2 11:10:17 2006

Date: Thu, 2 Nov 2006 08: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           Thu, 2 Nov 2006     Volume: 10 Number: 9921

Today's topics:
        Substitution (Regular Expressions) <mabounajm@gmail.com>
    Re: Substitution (Regular Expressions) <peace.is.our.profession@gmx.de>
    Re: Substitution (Regular Expressions) anno4000@radom.zrz.tu-berlin.de
    Re: Substitution (Regular Expressions) <mabounajm@gmail.com>
    Re: Substitution (Regular Expressions) anno4000@radom.zrz.tu-berlin.de
    Re: Substitution (Regular Expressions) <tadmc@augustmail.com>
        Unable to get an output from a piece of code snippet. : maheshpop1@gmail.com
    Re: Unable to get an output from a piece of code snippe (reading news)
    Re: Use of uninitialized variable with print << HTMLEND kevinwhite@bellsouth.net
    Re: Use of uninitialized variable with print << HTMLEND <bik.mido@tiscalinet.it>
    Re: Use of uninitialized variable with print << HTMLEND <tadmc@augustmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 2 Nov 2006 05:24:26 -0800
From: "enigma" <mabounajm@gmail.com>
Subject: Substitution (Regular Expressions)
Message-Id: <1162473866.348103.83730@m7g2000cwm.googlegroups.com>

Hi,

Is there a way to use the substitution operator a maximum of 'x' times
(without using a loop or writing the command 'x' times).... I can' use
the s/whatever/thing/g because it will replace all instances..

Thanks



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

Date: Thu, 02 Nov 2006 15:06:37 +0100
From: Mirco Wahab <peace.is.our.profession@gmx.de>
Subject: Re: Substitution (Regular Expressions)
Message-Id: <eicu9i$i18$1@mlucom4.urz.uni-halle.de>

Thus spoke enigma (on 2006-11-02 14:24):

> Is there a way to use the substitution operator a maximum of 'x' times
> (without using a loop or writing the command 'x' times).... I can' use
> the s/whatever/thing/g because it will replace all instances..

One kind of a solution would be: 'count down' and
modify the match accordingly:

 ...
  my $text = 'enigma';
  my $n = 3;

  $text =~ s/\w(??{"\^" if --$n < 0})/~/g;
  print "$text\n";
  ...

prints:

  ~~~gma


Regards

M.


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

Date: 2 Nov 2006 14:13:08 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Substitution (Regular Expressions)
Message-Id: <4qucnkFojgokU1@news.dfncis.de>

enigma <mabounajm@gmail.com> wrote in comp.lang.perl.misc:
> Hi,
> 
> Is there a way to use the substitution operator a maximum of 'x' times
> (without using a loop or writing the command 'x' times).... I can' use
> the s/whatever/thing/g because it will replace all instances..

Without a loop?  I don't think so, unless you want to use code insertions
in your pattern.  Here is one way:

    my $pat = 'a.';
    my $repl = 'AA';
    my $n = 3;

    my $str = 'aabbacbdadbeaf';

    $str =~ /$pat/g for 1 .. $n;
    substr( $str, 0, pos $str) =~ s/$pat/$repl/g;

    print "$str\n";

The loop sets pos( $str) to the point after the $n-th match.  Then
you can use global substitution on that part of the string.

Anno



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

Date: 2 Nov 2006 06:36:42 -0800
From: "enigma" <mabounajm@gmail.com>
Subject: Re: Substitution (Regular Expressions)
Message-Id: <1162478202.218738.30340@h54g2000cwb.googlegroups.com>

Thanks Alot!

That was helpful.

Enigma
anno4000@radom.zrz.tu-berlin.de wrote:
> enigma <mabounajm@gmail.com> wrote in comp.lang.perl.misc:
> > Hi,
> >
> > Is there a way to use the substitution operator a maximum of 'x' times
> > (without using a loop or writing the command 'x' times).... I can' use
> > the s/whatever/thing/g because it will replace all instances..
>
> Without a loop?  I don't think so, unless you want to use code insertions
> in your pattern.  Here is one way:
>
>     my $pat = 'a.';
>     my $repl = 'AA';
>     my $n = 3;
>
>     my $str = 'aabbacbdadbeaf';
>
>     $str =~ /$pat/g for 1 .. $n;
>     substr( $str, 0, pos $str) =~ s/$pat/$repl/g;
>
>     print "$str\n";
>
> The loop sets pos( $str) to the point after the $n-th match.  Then
> you can use global substitution on that part of the string.
> 
> Anno



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

Date: 2 Nov 2006 14:59:00 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Substitution (Regular Expressions)
Message-Id: <4qufdkFojgokU2@news.dfncis.de>

 <anno4000@radom.zrz.tu-berlin.de> wrote in comp.lang.perl.misc:
> enigma <mabounajm@gmail.com> wrote in comp.lang.perl.misc:
> > Hi,
> > 
> > Is there a way to use the substitution operator a maximum of 'x' times
> > (without using a loop or writing the command 'x' times).... I can' use
> > the s/whatever/thing/g because it will replace all instances..
> 
> Without a loop?  I don't think so, unless you want to use code insertions
> in your pattern.  Here is one way:
> 
>     my $pat = 'a.';
>     my $repl = 'AA';
>     my $n = 3;
> 
>     my $str = 'aabbacbdadbeaf';
> 
>     $str =~ /$pat/g for 1 .. $n;
>     substr( $str, 0, pos $str) =~ s/$pat/$repl/g;
> 
>     print "$str\n";
> 
> The loop sets pos( $str) to the point after the $n-th match.  Then
> you can use global substitution on that part of the string.

Well, there *is* an alternative without a (visible) loop:

    $str =~ s/($pat)/-- $n >= 0 ? $repl : $1/eg;

Instead of code insertions in the pattern it uses code evaluation
on the replacement side.  It works, but unlike the solution above
it does a lot of unnecessary work when there are many instances
of the pattern that are not to be replaced.

Anno


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

Date: Thu, 2 Nov 2006 09:13:01 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Substitution (Regular Expressions)
Message-Id: <slrnekk2nt.nno.tadmc@tadmc30.august.net>

anno4000@radom.zrz.tu-berlin.de <anno4000@radom.zrz.tu-berlin.de> wrote:
> enigma <mabounajm@gmail.com> wrote in comp.lang.perl.misc:
>> Hi,
>> 
>> Is there a way to use the substitution operator a maximum of 'x' times
>> (without using a loop or writing the command 'x' times).... I can' use
>> the s/whatever/thing/g because it will replace all instances..
>
> Without a loop?  I don't think so, unless you want to use code insertions
> in your pattern.  


or by inserting code in your replacement.  :-)


> Here is one way:
>
>     my $pat = 'a.';
>     my $repl = 'AA';
>     my $n = 3;
>
>     my $str = 'aabbacbdadbeaf';
>
>     $str =~ /$pat/g for 1 .. $n;
>     substr( $str, 0, pos $str) =~ s/$pat/$repl/g;


    $str =~ s/$pat/ $_++ >= $n ? $& : $repl/ge;


>     print "$str\n";


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


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

Date: 2 Nov 2006 02:36:25 -0800
From: maheshpop1@gmail.com
Subject: Unable to get an output from a piece of code snippet. :Confusing
Message-Id: <1162463785.574052.50970@i42g2000cwa.googlegroups.com>

Hi folks,

 I have this following code snippet, where I am looking to get some
output from the CVS repository by running cvs log command at the bash
shell. The puzzling thing is that when I put this code snippet in a
seperate file called test.pl and run it. It runs fine and I can see the
output of cvs log. But when I run this as part of a complete program,
it fails at  the @data = `$cmd 2>&-`; area.
I am trying to print the @data and all I get is this output

----LOG OUTPUT ----
cvsroot = /Repository/trunk cvsbase = /testCVS/junit g_isCli =
 printing in Proble.pl program argv :  -log
 global command is  LOG  command =  cvs log
 Command is  cvs log
 11

(The above 11 is the output of the @data that I get from the shell
back).
Here is the code snippet

    chdir('/testCVS/junit')  or die "can't change dir" ;
    my $cmd = 'cvs log';
    print "Command is $cmd";
    @data = `$cmd 2>&- `;
    print LOG @data;


any help is appreciated.

regards,
Mahesh



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

Date: Thu, 02 Nov 2006 12:33:09 GMT
From: "Mumia W. (reading news)" <paduille.4060.mumia.w@earthlink.net>
Subject: Re: Unable to get an output from a piece of code snippet. :Confusing
Message-Id: <9Al2h.1128$ig4.932@newsread2.news.pas.earthlink.net>

On 11/02/2006 04:36 AM, maheshpop1@gmail.com wrote:
> Hi folks,
> 
>  I have this following code snippet, where I am looking to get some
> output from the CVS repository by running cvs log command at the bash
> shell. The puzzling thing is that when I put this code snippet in a
> seperate file called test.pl and run it. It runs fine and I can see the
> output of cvs log. But when I run this as part of a complete program,
> it fails at  the @data = `$cmd 2>&-`; area.
> I am trying to print the @data and all I get is this output
> 
> ----LOG OUTPUT ----
> cvsroot = /Repository/trunk cvsbase = /testCVS/junit g_isCli =
>  printing in Proble.pl program argv :  -log
>  global command is  LOG  command =  cvs log
>  Command is  cvs log
>  11
> 
> (The above 11 is the output of the @data that I get from the shell
> back).
> Here is the code snippet
> 
>     chdir('/testCVS/junit')  or die "can't change dir" ;
>     my $cmd = 'cvs log';
>     print "Command is $cmd";
>     @data = `$cmd 2>&- `;
>     print LOG @data;
> 
> 
> any help is appreciated.
> 
> regards,
> Mahesh
> 

"&-" is not valid for bash as far as I can tell. Most people do "2>&1" 
to redirect standard error to standard output.


-- 
paduille.4060.mumia.w@earthlink.net


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

Date: 2 Nov 2006 05:13:49 -0800
From: kevinwhite@bellsouth.net
Subject: Re: Use of uninitialized variable with print << HTMLEND (and $ENV)
Message-Id: <1162473229.271826.279580@h48g2000cwc.googlegroups.com>

Michele Dondi wrote:
> On 31 Oct 2006 14:28:03 -0800, kevinwhite@bellsouth.net wrote:
>
> >I am trying to fix some code I inherited, and there are many warnings I
> >would like to clean up.  The biggest offenders are as follows:
> >
> >print <<"HTMLEND"; #<= this causes an uninitialized variable warning...
> >
> ><html><head></head><body></body></html>
> >HTMLEND
>
> This shouldn't cause 'uninitialized' warnings. Unless you have
> variables interpolated in. In which case you may either take care of
> checking for defined()ness or *temporarily* go C<no warnings;>.
>
> >The second example is:
> >my $htmlfile = "";
> >my $hide = "";
>
> No need to initialize them, BTW.
>
> >$htmlfile = param('filename');
> >$hide = param('hide') || "no"; #<= what does this do?
>
> Sets $hide to "no" if param('hide') is a Perl false value. More about
> || in
>
>   perldoc perlop
>
> >if ($htmlfile eq "default.html")  #<= this causes an uninitialized
> >variable warning...
>
> Ditto as above!
>
> >The third problem I have is printing environment variables when I have
> >"use strict" enabled.  What is the legal way to do this?
> >
> >I have:
> >my $n;
> >my @keys;
> >my @values;
> >my $Item;
> >
> >@keys = keys %ENV;
> >@values %ENV;
>  ^^^^^^^^^^^^^
>  ^^^^^^^^^^^^^
>
> (This is not valid Perl, I assume you mean
>
>   @values = values %ENV;  #)

Yes, my bad - the system I am working on is not on a network, so I
don't have the luxury of cut/paste.

Thanks all for the help.  It turns out the offending lines get reported
in kind of a strange way.  If you have an if statement, with multiple
elsif, the first if check gets reported if any of the following elsif
parameters include an uninitialized variable.  The same is true for
print <<HTMLEND; statements.

$hide = param('hide'); also causes uninitialized variables, so making
them my $hide = param('hide') || ""; was a better solution.

-Kevin
>
> >$n = 0;
>
> So far so fine as far as strict is concerned, although we recommend
> people all the time to declare lexicals as close as possible to the
> point where they're actually used
>
> >foreach $Item (@keys) { $$Item = $values[$n++];}
> >foreach $Item (@keys) { print "$Item=$$Item"; }
>
> Here you have a symref, which is not making strict.pm happy. Just use
> somehash instead.
>
> But... what is it that you're trying to do? All in all it seems you're
> wanting to do X but you're either really doing Y or doing X some very
> awkward and convoluted way...
>
>
> HTH,
> Michele
> --
> {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
> (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
> .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
> 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,



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

Date: Thu, 02 Nov 2006 16:09:51 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Use of uninitialized variable with print << HTMLEND (and $ENV)
Message-Id: <jn1kk2ljd4s4987jbh0b3gkcvjdssjbcue@4ax.com>

On 2 Nov 2006 05:13:49 -0800, kevinwhite@bellsouth.net wrote:

>> (This is not valid Perl, I assume you mean
>>
>>   @values = values %ENV;  #)
>
>Yes, my bad - the system I am working on is not on a network, so I
>don't have the luxury of cut/paste.

Don't you have a terminal emulator or something like that that *does*
support copy/paste? I occasionally happen e.g. to write code in ssh
sessions from Putty (due to being absymally lazy, lately I'm under a
redmondish environment much more frequently than in penguinish ones)
but then I can still do that quite well...

>Thanks all for the help.  It turns out the offending lines get reported
>in kind of a strange way.  If you have an if statement, with multiple
>elsif, the first if check gets reported if any of the following elsif
>parameters include an uninitialized variable.  The same is true for
>print <<HTMLEND; statements.

I didn't know about the elsif's, also because I actually I happen to
use them on an extremely sparse basis. For ">print <<HTMLEND;
statements" it does make much more sense, because the statement *is*
the print() one, terminated at the semicolon. Anything that follows is
fundamentally a fancy way to insert a string, although I can
understand that the behaviour can become moderately annoying if you
actually include *code* there:

  C:\temp>cat -n foo.pl
       1  #!/usr/bin/perl
       2
       3  use strict;
       4  use warnings;
       5
       6  my $x;
       7
       8  print <<"E";
       9  foo
      10  @{[ "$x" == 1 ? 'bar' : 'no way' ]}
      11  baz
      12  E
      13
      14  __END__
  
  C:\temp>foo
  Use of uninitialized value in string at C:\temp\foo.pl line 8.
  Argument "" isn't numeric in numeric eq (==) at C:\temp\foo.pl line
8.
  foo
  no way
  baz

>$hide = param('hide'); also causes uninitialized variables, so making
>them my $hide = param('hide') || ""; was a better solution.

Indeed. But we all want // for an even better one! ;-)


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Thu, 2 Nov 2006 09:18:36 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Use of uninitialized variable with print << HTMLEND (and $ENV)
Message-Id: <slrnekk32c.nno.tadmc@tadmc30.august.net>

kevinwhite@bellsouth.net <kevinwhite@bellsouth.net> wrote:
> Michele Dondi wrote:
>> On 31 Oct 2006 14:28:03 -0800, kevinwhite@bellsouth.net wrote:
>>
>> >I am trying to fix some code I inherited, and there are many warnings I
>> >would like to clean up.  The biggest offenders are as follows:
>> >
>> >print <<"HTMLEND"; #<= this causes an uninitialized variable warning...
>> >
>> ><html><head></head><body></body></html>
>> >HTMLEND
>>
>> This shouldn't cause 'uninitialized' warnings. Unless you have
>> variables interpolated in. 


> It turns out the offending lines get reported
> in kind of a strange way.  


And the "strange way" is common to all programming languages,
not just Perl.


> If you have an if statement, with multiple
> elsif, the first if check gets reported if any of the following elsif
> parameters include an uninitialized variable.


Line numbers reported are when the language parser _notices_ the
error (or warning in this case).

When you get lost driving for example, you can report where it was
that you noticed that you were lost, but you can't really report
where it was that you made the wrong turn. Similarly for parsers.


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


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

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


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