[26504] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8662 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Nov 13 18:05:33 2005

Date: Sun, 13 Nov 2005 15:05:04 -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           Sun, 13 Nov 2005     Volume: 10 Number: 8662

Today's topics:
    Re: A question about m/PATTERN/g - first match disappea <abigail@abigail.nl>
    Re: A question about m/PATTERN/g - first match disappea <1usa@llenroc.ude.invalid>
        can you please optimize this sub? <fisherNOPASARAN@uch.net>
    Re: can you please optimize this sub? <rm@nospam.popeslane.clara.co.uk>
    Re: can you please optimize this sub? <fisherNOPASARAN@uch.net>
    Re: can you please optimize this sub? <tadmc@augustmail.com>
    Re: FAQ 7.28 How can I use a variable as a variable nam <sherm@dot-app.org>
        Tree view of directory while using Subversion <jkeen_via_google@yahoo.com>
    Re: Tree view of directory while using Subversion <rvtol+news@isolution.nl>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 13 Nov 2005 20:25:39 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: A question about m/PATTERN/g - first match disappears
Message-Id: <slrndnf8a3.2l3.abigail@alexandra.abigail.nl>

A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMCDLVI September
MCMXCIII in <URL:news:Xns970CB99E5B8E5asu1cornelledu@127.0.0.1>:
&&  Abigail <abigail@abigail.nl> wrote in
&&  news:slrndncsmn.2l3.abigail@alexandra.abigail.nl: 
&&  
&& > A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMCDLVI September
&& > MCMXCIII in <URL:news:Xns970CB121219BFasu1cornelledu@127.0.0.1>:
&& > ~~  
&& > ~~  Now, why do you want to do that? :) You may have overlooked this,
&& > ~~  but I think the most succint explanation is given by the
&& > ~~  documentation for pos. When you match a string using a regex with
&& > ~~  the g modifier, the position of the match is stored, so that 
&& > ~~  the next match can match the next occurence (if any).
&& > 
&& > That's for a /g match *IN SCALAR CONTEXT*. In list context, it's quite
&& > different.
&&  
&&  The OP had a /g match in scalar context, followed by a /g match in list 
&&  context on the same string. My assumption is that the /g match in list 
&&  context started from where the match in scalar context left off. The 
&&  explanation for the behavior arose from the way /g behaves in scalar 
&&  context. Am I missing something?


I've no doubt that you know the difference between scalar and list
context. But I wanted to make sure the OP is made aware of it as well.


Abigail
-- 
sub camel (^#87=i@J&&&#]u'^^s]#'#={123{#}7890t[0.9]9@+*`"'***}A&&&}n2o}00}t324i;
h[{e **###{r{+P={**{e^^^#'#i@{r'^=^{l+{#}H***i[0.9]&@a5`"':&^;&^,*&^$43##@@####;
c}^^^&&&k}&&&}#=e*****[]}'r####'`=437*{#};::'1[0.9]2@43`"'*#==[[.{{],,,1278@#@);
print+((($llama=prototype'camel')=~y|+{#}$=^*&[0-9]i@:;`"',.| |d)&&$llama."\n");


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

Date: Sun, 13 Nov 2005 20:36:34 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: A question about m/PATTERN/g - first match disappears
Message-Id: <Xns970D9ECBDF8asu1cornelledu@127.0.0.1>

Abigail <abigail@abigail.nl> wrote in
news:slrndnf8a3.2l3.abigail@alexandra.abigail.nl: 

> A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMCDLVI September
> MCMXCIII in <URL:news:Xns970CB99E5B8E5asu1cornelledu@127.0.0.1>:
> &&  Abigail <abigail@abigail.nl> wrote in
> &&  news:slrndncsmn.2l3.abigail@alexandra.abigail.nl: 
> &&  
> && > A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMCDLVI
> && > September MCMXCIII in <URL:news:Xns970CB121219BFasu1cornelledu@
> && > 127.0.0.1:
> && > ~~  
> && > ~~  Now, why do you want to do that? :) You may have overlooked 
> && > ~~  this, but I think the most succint explanation is given by 
> && > ~~  the documentation for pos. When you match a string using a 
> && > ~~  regex with the g modifier, the position of the match is 
> && > ~~  stored, so that the next match can match the next occurence 
> && > ~~  (if any).
> && > 
> && > That's for a /g match *IN SCALAR CONTEXT*. In list context, it's 
> && > quite different.
> &&  ...
> && I missing something?
>
> I've no doubt that you know the difference between scalar and list
> context. But I wanted to make sure the OP is made aware of it as well.

Got it. Thank you for the clarification. 

Sinan
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


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

Date: Sun, 13 Nov 2005 22:00:25 +0200
From: "Serge A. Ribalchenko" <fisherNOPASARAN@uch.net>
Subject: can you please optimize this sub?
Message-Id: <20051113220025.2d1fce3d.fisherNOPASARAN@uch.net>

--Signature=_Sun__13_Nov_2005_22_00_25_+0200_c/eUJvy46s1Q=C9s
Content-Type: text/plain; charset=US-ASCII
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

Hi all,

I feel this subroutine is looking ugly. Can you optimize/squize it?

Input: number of seconds
Output: string XXdays XXhours XXmin XXsec

sub time2str($) {
    my $t=$_[0];
    my $retval="";
    if ($t >= 86400) {
	$retval= sprintf("%dd ", $t/86400);
	$t%= 86400;
    } else { $retval= "0d "; }
    if ($t >= 3600) {
	$retval.= sprintf("%02dh", $t/3600);
	$t%= 3600;
    } else { $retval.= "00h"; }
    if ($t >= 60) {
	$retval.= sprintf("%02dm", $t/60);
	$t%= 60;
    } else { $retval.= "00m"; }
    return $retval.= sprintf("%02ds",$t);
}

-- 
Best wishes,
~       Serge.           pubkeys: http://uch.net/~fisher/keys.asc
~fingerprint : 4346 2766 BC96 E77F 5BFF  1E7C 12C2 3852 E5FD DC34

--Signature=_Sun__13_Nov_2005_22_00_25_+0200_c/eUJvy46s1Q=C9s
Content-Type: application/pgp-signature

-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFDd5teX9BKv7kKDR0RAhVfAJ9+cRosucM7UwyC1K9ykWWL8b7t+QCfVpLz
Dr5JtuGc9LRZd3NX7PiBVGI=
=S5w/
-----END PGP SIGNATURE-----

--Signature=_Sun__13_Nov_2005_22_00_25_+0200_c/eUJvy46s1Q=C9s--


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

Date: Sun, 13 Nov 2005 21:19:10 +0000
From: Rob <rm@nospam.popeslane.clara.co.uk>
Subject: Re: can you please optimize this sub?
Message-Id: <OrKdnXM83_VNMOreRVnysQ@pipex.net>

Serge A. Ribalchenko wrote:
> I feel this subroutine is looking ugly. Can you optimize/squize it?
> 
> Input: number of seconds
> Output: string XXdays XXhours XXmin XXsec
> 
> sub time2str($) {
>     my $t=$_[0];
>     my $retval="";
>     if ($t >= 86400) {
> 	$retval= sprintf("%dd ", $t/86400);
> 	$t%= 86400;
>     } else { $retval= "0d "; }
>     if ($t >= 3600) {
> 	$retval.= sprintf("%02dh", $t/3600);
> 	$t%= 3600;
>     } else { $retval.= "00h"; }
>     if ($t >= 60) {
> 	$retval.= sprintf("%02dm", $t/60);
> 	$t%= 60;
>     } else { $retval.= "00m"; }
>     return $retval.= sprintf("%02ds",$t);
> }

How about:

#!perl -w
use strict;
use warnings;

sub time2str($);

foreach (0,1,59,60,61,3599,3600,3601,86399,86400,86401) {
	print time2str($_), "\n";
}

exit(0);

sub time2str($) {
     my $t=shift;
     my @answers;

     foreach (60,60,24) {
	    push @answers, $t % $_;
	    $t = int($t/$_);
     }

     return sprintf("%dd %02dh%02dm%02ds", $t, reverse @answers);
}
__END__

Regards,
Rob.
-- 
Robert May
Win32::GUI, a perl extension for native Win32 applications
http://perl-win32-gui.sourceforge.net/


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

Date: Sun, 13 Nov 2005 23:40:23 +0200
From: "Serge A. Ribalchenko" <fisherNOPASARAN@uch.net>
Subject: Re: can you please optimize this sub?
Message-Id: <20051113234023.15edf5e6.fisherNOPASARAN@uch.net>

--Signature=_Sun__13_Nov_2005_23_40_23_+0200_+QB8zXr+OBoShpJB
Content-Type: text/plain; charset=US-ASCII
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

On Sun, 13 Nov 2005 21:19:10 +0000
Rob <rm@nospam.popeslane.clara.co.uk> wrote:

> sub time2str($) {
>      my $t=shift;
>      my @answers;
> 
>      foreach (60,60,24) {
> 	    push @answers, $t % $_;
> 	    $t = int($t/$_);
>      }
> 
>      return sprintf("%dd %02dh%02dm%02ds", $t, reverse @answers);
> }

Wow, cool! Much better.
Thanks, Rob!

-- 
Best wishes,
~       Serge.           pubkeys: http://uch.net/~fisher/keys.asc
~fingerprint : 4346 2766 BC96 E77F 5BFF  1E7C 12C2 3852 E5FD DC34

--Signature=_Sun__13_Nov_2005_23_40_23_+0200_+QB8zXr+OBoShpJB
Content-Type: application/pgp-signature

-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFDd7LOX9BKv7kKDR0RAkmDAJ9iI/BP70beWuPIy0utPfmak/XGBQCePlP8
gr1pb5J8JuhKQgHbFiCl1rs=
=R755
-----END PGP SIGNATURE-----

--Signature=_Sun__13_Nov_2005_23_40_23_+0200_+QB8zXr+OBoShpJB--


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

Date: Sun, 13 Nov 2005 16:09:51 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: can you please optimize this sub?
Message-Id: <slrndnfedf.v6b.tadmc@magna.augustmail.com>

Serge A. Ribalchenko <fisherNOPASARAN@uch.net> wrote:
> 
> I feel this subroutine is looking ugly. Can you optimize/squize it?


> sub time2str($) {
>     my $t=$_[0];
>     my $retval="";
>     if ($t >= 86400) {
> 	$retval= sprintf("%dd ", $t/86400);
> 	$t%= 86400;
>     } else { $retval= "0d "; }
>     if ($t >= 3600) {
> 	$retval.= sprintf("%02dh", $t/3600);
> 	$t%= 3600;
>     } else { $retval.= "00h"; }
>     if ($t >= 60) {
> 	$retval.= sprintf("%02dm", $t/60);
> 	$t%= 60;
>     } else { $retval.= "00m"; }
>     return $retval.= sprintf("%02ds",$t);
> }


I don't see that any of those if/else clauses are necesary...


-----------------------------
sub time2str {
    my($t) = @_;
    my $retval;

    foreach my $unit ( [86400, "%dd " ], [ 3600, "%02dh"],
                       [   60, "%02dm"], [    1, "%02ds"] ) {

      $retval .= sprintf $unit->[1], $t / $unit->[0];
      $t %= $unit->[0];
    }

    return $retval;
}
-----------------------------

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


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

Date: Sun, 13 Nov 2005 13:13:30 -0500
From: Sherm Pendley <sherm@dot-app.org>
Subject: Re: FAQ 7.28 How can I use a variable as a variable name?
Message-Id: <m2wtjcwh4l.fsf@Sherm-Pendleys-Computer.local>

"Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail@moderncppdesign.com> writes:

> As far as I know, everybody and
> their grandma swears that globals are a bad idea.)

Unless your grandma happens to be Grace Hopper, I doubt that... ;-)

sherm--

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


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

Date: Sun, 13 Nov 2005 16:59:25 GMT
From: James E Keenan <jkeen_via_google@yahoo.com>
Subject: Tree view of directory while using Subversion
Message-Id: <NhKdf.5092$zL1.388@trndny07>

Tad McClellan has on a couple of occasions posted the following shell 
code as a quick way of generating a tree view of the content of a directory:

 find . -print | sed -e 's,[^/]*/\([^/]*\)$,`--\1,' -e 's,[^/]*/,|  ,g'

See, for example, http://tinyurl.com/a8mmr.

I've used this many times, but now that I use Subversion a lot, it 
generates too much output, because it prints out all the .svn directories.

I hacked on Tad's suggestion and came up with this, which I've placed in 
my .bashrc file:

# trim:  A trimmer tree
# bypassing all subdirectories whose names begin with '.'
trim ()
{
     find . -not -regex '.*/\..*'  -print | sed -e 
's,[^/]*/\([^/]*\)$,`--\1,' -e 's,[^/]*/,|  ,g' | more
}

Comments?

jimk


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

Date: Sun, 13 Nov 2005 20:14:21 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Tree view of directory while using Subversion
Message-Id: <dl87kl.v0.1@news.isolution.nl>

James E Keenan:

> # trim:  A trimmer tree
> # bypassing all subdirectories whose names begin with '.'
> trim ()
> {
>      find . -not -regex '.*/\..*'  -print | sed -e
> 's,[^/]*/\([^/]*\)$,`--\1,' -e 's,[^/]*/,|  ,g' | more
> }
> 
> Comments?


No Perl involved.


  find . -not -name '.*' 
  | sort 
  | sed -e 's,[^/]*/\([^/]*\)$,`--\1,' -e 's,[^/]*/,|  ,g' 
  | less

Alternative sed-line:

  | sed 's,[^/]*/\([^/]*\)$,+--\1,; s,[^/]*/,|  ,g' 


Try also:

  tree -C | less -R

-- 
Affijn, Ruud

"Gewoon is een tijger."


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

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


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