[23541] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5749 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Nov 4 14:11:07 2003

Date: Tue, 4 Nov 2003 11:10:14 -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, 4 Nov 2003     Volume: 10 Number: 5749

Today's topics:
        Renaming files <jc65140@aod.fedex.com>
    Re: Renaming files <noreply@gunnar.cc>
    Re: Renaming files <hexkid@hotpop.com>
    Re: Renaming files <tore@aursand.no>
    Re: Renaming files (Tad McClellan)
    Re: Rounding a float in Perl? (Roy Johnson)
    Re: Rounding a float in Perl? (Anno Siegel)
    Re: sub confusion <walterg@example.com>
    Re: sub confusion <walterg@example.com>
    Re: sub confusion <noreply@gunnar.cc>
    Re: sub confusion <usenet@morrow.me.uk>
    Re: testing for 'undefined subroutine' (Anno Siegel)
    Re: Verbose warnings (Greg Bacon)
        what is @$? <jgrg@sanger.ac.uk>
    Re: what is @$? (Anno Siegel)
    Re: what is @$? <jgrg@sanger.ac.uk>
    Re: what is @$? <abigail@abigail.nl>
    Re: what is @$? (Anno Siegel)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 04 Nov 2003 10:15:16 -0600
From: Johnny Cockrell <jc65140@aod.fedex.com>
Subject: Renaming files
Message-Id: <3FA7D093.6F9874EA@aod.fedex.com>

Hello,

Can anyone help me in renaming about 1000 files I have.

The file names are like 21-10-03-902-02.tif
The change i need to make is in the third set of digits (where the 03 is
in
this example). I need to make the 2 digits to be 3 digits with a leading
0.
In the above example 21-10-03-902-02.tif would need to become
                                  21-10-003-902-02.tif

25-25-25-100-10.tif would need to become 25-25-025-100-10.tif.
I simply need to make the two digits to be 3 digits with a leading 0.
I don't know much perl and need to get this done. Any help would be
greatly appreciated.

Johnny



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

Date: Tue, 04 Nov 2003 17:31:06 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Renaming files
Message-Id: <bo8kus$1asffj$2@ID-184292.news.uni-berlin.de>

Johnny Cockrell wrote:
> Can anyone help me in renaming about 1000 files I have.

<snip>

> I don't know much perl and need to get this done.

     http://jobs.perl.org/

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



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

Date: 4 Nov 2003 17:35:52 GMT
From: Pedro <hexkid@hotpop.com>
Subject: Re: Renaming files
Message-Id: <bo8o1n$1bbp3f$1@ID-203069.news.uni-berlin.de>

Johnny Cockrell wrote:
> Hello,
>
> Can anyone help me in renaming about 1000 files I have.
>
> The file names are like 21-10-03-902-02.tif
> The change i need to make is in the third set of digits (where the 03 is
> in this example). I need to make the 2 digits to be 3 digits with a leading
> 0.

newbie answer:



#!/usr/bin/perl

use strict;
use warnings;
use File::Glob ':glob';

my @files = bsd_glob('[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9]\.tif');
for (@files) {
  my $newname = substr($_, 0, 6) . '0' . substr($_, 6);
  rename($_, $newname); # maybe add or die("cannot rename: $!")
  print "renamed $_ as $newname\n";
}

-- 
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.


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

Date: Tue, 04 Nov 2003 19:50:04 +0100
From: Tore Aursand <tore@aursand.no>
Subject: Re: Renaming files
Message-Id: <pan.2003.11.04.17.58.39.240425@aursand.no>

On Tue, 04 Nov 2003 10:15:16 -0600, Johnny Cockrell wrote:
> Can anyone help me in renaming about 1000 files I have.
> [...]

What have you tried so far?  This newsgroup isn't a place to advertise
jobs, but rather to get help on problems related to Perl.

> The file names are like 21-10-03-902-02.tif

This should get you started:

  perldoc File::Basename
  perldoc -f split


-- 
Tore Aursand <tore@aursand.no>


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

Date: Tue, 4 Nov 2003 12:54:53 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Renaming files
Message-Id: <slrnbqftft.jqk.tadmc@magna.augustmail.com>

Johnny Cockrell <jc65140@aod.fedex.com> wrote:

> The file names are like 21-10-03-902-02.tif
> The change i need to make is in the third set of digits (where the 03 is
> in
> this example). I need to make the 2 digits to be 3 digits with a leading
> 0.


   substr($file, 6, 2) = sprintf '%03d', substr($file, 6, 2);


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


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

Date: 4 Nov 2003 08:56:42 -0800
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: Rounding a float in Perl?
Message-Id: <3ee08638.0311040856.61ef3ad9@posting.google.com>

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message news:<bntfci$gjv$3@mamenchi.zrz.TU-Berlin.DE>...
Your rounding is faster, but it doesn't quite work, in the sense of
always getting the same result as sprintf *or* the mathematically
accurate result.

nround(0.05, 1) yields 0, whereas sprintf('%.1f', 0.05) yields 0.1. Of
course, sprintf('%.1f', 3.05) yields 3.0, and so does Nround.

Here's one that works (in the sense of being mathematically accurate)
for all the values I've tried, but it is only about half as fast as
nround, or 3/4 as fast as sprintf.

sub stround {
   my ($n, $places) = (@_,0);
   my $sign = ($n < 0) ? '-' : '';
   my $abs = abs($n);
   $sign . substr($abs+('0.'.'0'x$places.'5'),
                  0, $places+length(int($abs))+1);
}


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

Date: 4 Nov 2003 18:37:54 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Rounding a float in Perl?
Message-Id: <bo8rm2$n4$1@mamenchi.zrz.TU-Berlin.DE>

Roy Johnson <rjohnson@shell.com> wrote in comp.lang.perl.misc:
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message
> news:<bntfci$gjv$3@mamenchi.zrz.TU-Berlin.DE>...
> Your rounding is faster, but it doesn't quite work, in the sense of
> always getting the same result as sprintf *or* the mathematically
> accurate result.

What is the mathematically accurate result of rounding 0.05 to one
decimal place?  The problem is, there are two equally likely candidates,
and mathematics doesn't presume to make a decision.  Mathematically,
the result isn't defined.

> nround(0.05, 1) yields 0, whereas sprintf('%.1f', 0.05) yields 0.1. Of
> course, sprintf('%.1f', 3.05) yields 3.0, and so does Nround.

Algorithms have to make a decision, and standards define how to make
it.  I am a little surprised at what IEEE does here (assuming it *is*
IEEE), or rather, I would be surprised if I hadn't long ago given up
being surprised by numeric results.

> Here's one that works (in the sense of being mathematically accurate)
> for all the values I've tried, but it is only about half as fast as
> nround, or 3/4 as fast as sprintf.

Again, there is no "mathematically accurate" solution in the critical
case (where both possible values are equally far off).  However, your
solution now differs from sprintf when rounding 0.5 to an integer:
sprintf says 0, stround says 1.

The point of speed is probably moot now.  The next Perl will include
Ilya's patch, if the creeks don't rise,  and that'll be it for Perl
implementations of rounding.

> sub stround {
>    my ($n, $places) = (@_,0);
>    my $sign = ($n < 0) ? '-' : '';
>    my $abs = abs($n);
>    $sign . substr($abs+('0.'.'0'x$places.'5'),
>                   0, $places+length(int($abs))+1);
> }

Hmm...  I can't say that I like the mixture of arithmetic and text-
processing, though it may be said to be typical of Perl.  If I had to
*prove* an implementation conforms to some standard, I'd prefer a numeric
solution, if possible one that corresponds step by step to the description
in the standard.

Anno


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

Date: Tue, 04 Nov 2003 17:53:46 GMT
From: <walterg@example.com>
Subject: Re: sub confusion
Message-Id: <KIRpb.3545$Wy2.42852@typhoon.sonic.net>

Eric J. Roode <REMOVEsdnCAPS@comcast.net> wrote:
>Your main confusion seems to be with $_.  In a subroutine, the argument 
>passed does not automatically go into $_.  If you want it there, you have 
>to put it there with a line such as:
>
>    local $_ = shift;

Thanks Eric.  I must be getting overly object-centric to have presumed $_
would be local and default to the sub's ARGV[0].

Walt


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

Date: Tue, 04 Nov 2003 18:29:38 GMT
From: <walterg@example.com>
Subject: Re: sub confusion
Message-Id: <meSpb.3565$Wy2.42761@typhoon.sonic.net>

Bob Walton <invalid-email@rochester.rr.com> wrote:
>walterg@example.com wrote:
>> $today=`date`;
>
>You might consider using Perl's builtin localtime()

Thanks Bob.  Though I should have posted a better example.  What's
being parsed is log files, specifically the datestamp, for sorting.
Also need the local timezone i.e, MST, and can't, apparently, get it
from localtime().

Still haven't figured out TimeToNum() though.  Given $time = "12:34:56";

    sub TimeToNum {
        $_ = shift;
        s/://g;
    } 
    TimeToNum($time)

returns "2" whereas

    $time =~ s/://g;

returns the expected "123456".

Walt


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

Date: Tue, 04 Nov 2003 19:34:20 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: sub confusion
Message-Id: <bo8s6k$1bfa4d$1@ID-184292.news.uni-berlin.de>

walterg@example.com wrote:
> Still haven't figured out TimeToNum() though.
> Given $time = "12:34:56";
> 
>     sub TimeToNum {
>         $_ = shift;
>         s/://g;
>     } 
>     TimeToNum($time)
> 
> returns "2"

Yes it does. And if you wonder why, you'd better study the 
documentation for Perl subroutines:

     perldoc perlsub

or

     http://www.perldoc.com/perl5.8.0/pod/perlsub.html

"The return value of a subroutine is the value of the last expression 
evaluated."

You may want

     sub TimeToNum {
         $_ = shift;
         s/://g;
         return $_;
     }

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



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

Date: Tue, 4 Nov 2003 19:01:39 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: sub confusion
Message-Id: <bo8t2j$qiv$2@wisteria.csv.warwick.ac.uk>


<walterg@example.com> wrote:
> Eric J. Roode <REMOVEsdnCAPS@comcast.net> wrote:
> >Your main confusion seems to be with $_.  In a subroutine, the argument 
> >passed does not automatically go into $_.  If you want it there, you have 
> >to put it there with a line such as:
> >
> >    local $_ = shift;
> 
> Thanks Eric.  I must be getting overly object-centric to have presumed $_
> would be local and default to the sub's ARGV[0].

 ...Perl 6... :)

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: 4 Nov 2003 11:56:56 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: testing for 'undefined subroutine'
Message-Id: <bo8468$dbv$1@mamenchi.zrz.TU-Berlin.DE>

Torsten Mangner  <tmangner@alphaone.de> wrote in comp.lang.perl.misc:
> Hi group,
> 
> i need a capability (via a skript or something) to test my skripts (at 
> compile time),

If you are using a script to check other scripts, the question of
"compile time" doesn't come up -- you won't be compiling the scripts
you are testing.

>                if all subroutines that are called in the programm are 
> really defined or 'use'd.

You don't "use" a subroutine, you "use" a module which may define some
subroutines.

In all its generality, your problem will be hard to solve.  Since
subroutine calls can be modified at run time, there is no easy way
to know which subroutines will be called without running the program.
Since subroutine definitions can be added or modified at run time,
there is no way of telling which calls will succeeded without running
the program.

If you can settle for a fixed list of sub names, add this to your
script:

CHECK {
    report_sub( $_) for qw( sub1 sub2 ...);

    sub report_sub {
        my $name = shift;
        my $status = 'unknown';
        $status = 'declared' if exists &$name;
        $status = 'defined' if defined &$name;
        print "$name is $status\n";
    }
}

The CHECK block is run after compilation is complete (that means, all
"use" statements have been executed at this point).  For the application
of "exists" and "defined" to subroutines, see "perldoc -f exists".

Anno



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

Date: Tue, 04 Nov 2003 14:43:50 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Verbose warnings
Message-Id: <vqfep6jb05dd4d@corp.supernews.com>

In article <3fa6ba78$0$1100$3c090ad1@news.plethora.net>,
    Seebs <seebs@plethora.net> wrote:

: Is there any way to find out WHICH variable is uninitialized, short of
: breaking a line up into bunches of single lines?
: [...]

Here's a start:

    $ cat Devel/wundef.pm
    package Devel::wundef;

    BEGIN {
        $SIG{__WARN__} = \&wundef;

        open STDOUT, ">/dev/null" or die "$0: open /dev/null: $!\n";
    }

    use warnings;
    use strict;

    use Data::Dumper;

    my %cache;
    my %seen;

    sub grabline {
        my $path = shift;
        my $num = shift;

        die "$0: bad line ($num)" if $num <= 0;

        return $cache{$path}->[$num-1] if $cache{$path};

        open my $fh, "<", $path or die "$0: open $path: $!\n";
        local $/ = "\n";
        $cache{$path} = [ <$fh> ];

        return $cache{$path}->[$num-1];
    }

    sub wundef {
        my $warning = shift;
        return unless $warning =~ /^Use of uninitialized value/;

        my($pkg,$file,$line) = caller;
        return unless $seen{$pkg}{$file}{$line}++;

        print STDERR $warning;
        local $_ = grabline $file, $line;
        while (/\$((\w+::)*\w+)/g) {
            my $var = $1;

            if ($var =~ /::/) {
                $var = $var;
            }
            else {
                $var = $pkg . '::' . $var;
            }

            #warn "var = [$var]\n";
            no strict 'vars';
            print STDERR "$var undefined\n" unless defined $$var;
        }
    }

    package DB;

    use Data::Dumper;

    sub DB {
    #    print "DB::DB called\n";
    }

    1;

    $ cat try
    #! /usr/local/bin/perl -w

    $abc = 1;
    $klm::nop = 2;

    print $abc, $def, $efg::hij, $klm::nop;

    $ perl -d:wundef try
    Use of uninitialized value in print at try line 6.
    main::def undefined
    efg::hij undefined

Hope this helps,
Greg
-- 
The Fifth Protocol of the Elders of Zion: Pika, Pika!
    -- Free Republic poster mocking Pokemon-as-Jewish-conspiracy


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

Date: Tue, 04 Nov 2003 16:27:16 +0000
From: James Gilbert <jgrg@sanger.ac.uk>
Subject: what is @$?
Message-Id: <bo8k15$fes$1@niobium.hgmp.mrc.ac.uk>

I recently found this piece of code in one of my scripts:

             eval{
                 $row->SequenceInfo->drop_Sequence;
             };
             if (@$){
                 print "ERROR dropping sequence\n$@";
             }

ie: I had meant to put "$@", but typed "@$" in the condition.

What does perl interpret "@$" as?  I can't see any note of
a special array called "$".  It doesn't produce any warnings,
and compiles under strict.

	James



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

Date: 4 Nov 2003 17:04:17 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: what is @$?
Message-Id: <bo8m6h$qpo$1@mamenchi.zrz.TU-Berlin.DE>

James Gilbert  <jgrg@sanger.ac.uk> wrote in comp.lang.perl.misc:
> I recently found this piece of code in one of my scripts:
> 
>              eval{
>                  $row->SequenceInfo->drop_Sequence;
>              };
>              if (@$){
>                  print "ERROR dropping sequence\n$@";
>              }
> 
> ie: I had meant to put "$@", but typed "@$" in the condition.
> 
> What does perl interpret "@$" as?  I can't see any note of
> a special array called "$".  It doesn't produce any warnings,
> and compiles under strict.

It's an unused system variable.  All predeclared "punctuation variables"
come in threes: a scalar, an array, and a hash.  So along with "$$" (the
PID, remember?) there is %$ and indeed @$, the one you have stumbled on.
Similarly you could use "%.", "@/", and even "$INC" under strict without
declaration.

The only "practical" use for these variables is code obfuscation. Inserting 
blanks between the type specifier ("$", "@" or "%") and the name (you can 
do that) makes interesting-to-parse code passages.  See Abigail's sigs for 
examples.

Anno


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

Date: Tue, 04 Nov 2003 17:33:57 +0000
From: James Gilbert <jgrg@sanger.ac.uk>
Subject: Re: what is @$?
Message-Id: <bo8nu6$gnd$1@niobium.hgmp.mrc.ac.uk>



Anno Siegel wrote:
> James Gilbert  <jgrg@sanger.ac.uk> wrote in comp.lang.perl.misc:
> 
>>I recently found this piece of code in one of my scripts:
>>
>>             eval{
>>                 $row->SequenceInfo->drop_Sequence;
>>             };
>>             if (@$){
>>                 print "ERROR dropping sequence\n$@";
>>             }
>>
>>ie: I had meant to put "$@", but typed "@$" in the condition.
>>
>>What does perl interpret "@$" as?  I can't see any note of
>>a special array called "$".  It doesn't produce any warnings,
>>and compiles under strict.
> 
> 
> It's an unused system variable.  All predeclared "punctuation variables"
> come in threes: a scalar, an array, and a hash.  So along with "$$" (the
> PID, remember?) there is %$ and indeed @$, the one you have stumbled on.
> Similarly you could use "%.", "@/", and even "$INC" under strict without
> declaration.
> 
> The only "practical" use for these variables is code obfuscation. Inserting 
> blanks between the type specifier ("$", "@" or "%") and the name (you can 
> do that) makes interesting-to-parse code passages.  See Abigail's sigs for 
> examples.

Cool!  The "@@" array appeals to me as convenient temporary
storage.  Now if I can just work out a way to mix it into a
nice big map and grep, I'll have the java programmers in our
team scratching their heads for weeks...

perl -wle 'use strict; @@ = qw{fee fie foo}; foreach (@{@ @}) {print}'

	James



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

Date: 04 Nov 2003 18:09:46 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: what is @$?
Message-Id: <slrnbqfqra.uue.abigail@alexandra.abigail.nl>

James Gilbert (jgrg@sanger.ac.uk) wrote on MMMDCCXVII September MCMXCIII
in <URL:news:bo8k15$fes$1@niobium.hgmp.mrc.ac.uk>:
??  I recently found this piece of code in one of my scripts:
??  
??               eval{
??                   $row->SequenceInfo->drop_Sequence;
??               };
??               if (@$){
??                   print "ERROR dropping sequence\n$@";
??               }
??  
??  ie: I had meant to put "$@", but typed "@$" in the condition.
??  
??  What does perl interpret "@$" as?  I can't see any note of
??  a special array called "$".  It doesn't produce any warnings,
??  and compiles under strict.


It refers to the array '$'. There's no note of it because it's
not special. It isn't any different than '@toodle_poops'.


Abigail
-- 
# Count the number of lines; code doesn't match \w. Linux specific.
()=<>;$!=$=;($:,$,,$;,$")=$!=~/.(.)..(.)(.)..(.)/;
$;++;$*++;$;++;$*++;$;++;`$:$,$;$" $. >&$*`; 


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

Date: 4 Nov 2003 18:46:16 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: what is @$?
Message-Id: <bo8s5o$n4$2@mamenchi.zrz.TU-Berlin.DE>

James Gilbert  <jgrg@sanger.ac.uk> wrote in comp.lang.perl.misc:
> 
> 
> Anno Siegel wrote:
> > James Gilbert  <jgrg@sanger.ac.uk> wrote in comp.lang.perl.misc:
> > 
> >>I recently found this piece of code in one of my scripts:
> >>
> >>             eval{
> >>                 $row->SequenceInfo->drop_Sequence;
> >>             };
> >>             if (@$){
> >>                 print "ERROR dropping sequence\n$@";
> >>             }
> >>
> >>ie: I had meant to put "$@", but typed "@$" in the condition.
> >>
> >>What does perl interpret "@$" as?  I can't see any note of
> >>a special array called "$".  It doesn't produce any warnings,
> >>and compiles under strict.
> > 
> > 
> > It's an unused system variable.  All predeclared "punctuation variables"
> > come in threes: a scalar, an array, and a hash.  So along with "$$" (the
> > PID, remember?) there is %$ and indeed @$, the one you have stumbled on.
> > Similarly you could use "%.", "@/", and even "$INC" under strict without
> > declaration.
> > 
> > The only "practical" use for these variables is code obfuscation. Inserting 
> > blanks between the type specifier ("$", "@" or "%") and the name (you can 
> > do that) makes interesting-to-parse code passages.  See Abigail's sigs for 
> > examples.
> 
> Cool!  The "@@" array appeals to me as convenient temporary
> storage.  Now if I can just work out a way to mix it into a
> nice big map and grep, I'll have the java programmers in our
> team scratching their heads for weeks...
> 
> perl -wle 'use strict; @@ = qw{fee fie foo}; foreach (@{@ @}) {print}'

That's the spirit!

To be serious for a moment, these are system variables, whether the system
uses them or not.  They shouldn't be used for anything.

Anno


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

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.  

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


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