[25885] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8113 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 25 09:05:25 2005

Date: Wed, 25 May 2005 06:05:09 -0700 (PDT)
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, 25 May 2005     Volume: 10 Number: 8113

Today's topics:
        re: fibonacci string (Anno Siegel)
    Re: fibonacci string <josef.moellers@fujitsu-siemens.com>
        get the current week nbr <alexj@freesurf.ch>
    Re: get the current week nbr <1usa@llenroc.ude.invalid>
    Re: get the current week nbr <alexj@freesurf.ch>
    Re: Need help with proper file locking of flatfile data (Anno Siegel)
    Re: simplest method for renaming files / Windows ? <joe@inwap.com>
    Re: simplest method for renaming files / Windows ? <1usa@llenroc.ude.invalid>
    Re: simplest method for renaming files / Windows ? <jurgenex@hotmail.com>
    Re: Strange 'for' behaviour? <tim@vegeta.ath.cx>
    Re: substitution <joe@inwap.com>
    Re: substitution <tadmc@augustmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 25 May 2005 08:51:45 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: re: fibonacci string
Message-Id: <d71eb1$rp8$1@mamenchi.zrz.TU-Berlin.DE>

david k. wall <darkon.tdo@gmail.com> wrote in comp.lang.perl.misc:
> here's a fun fact i ran across in the book _the golden ratio_, by 
> mario livio.  take the string '1' and replace it with '10'.  
> thereafter, replace any occurrence of '1' with '10' and '0' with '1'.  
> then count the number of 0s and 1s in the string.  you get a 
> fibonacci sequence for each count (offset by one iteration).

Well, lessee...

If string v has n0 zeroes and n1 ones, substituting all ones with
'10' gives n1 ones and n1 zeroes.  Substituting zeroes with '1' adds
another n0 ones (and no zeroes). So the number n0' of zeroes in the
next string is n1, and the number n1' of ones is n0 + n1.

If (n0, n1) are subsequent Fibonacci numbers, then (n0', n1') =
(n1, n0 + n1) are the next two Fibonacci numbers.  Since this is true
for the very first string '1', with 0 = fib[ 0] zeroes and 1 = fib[ 1]
ones, it is true for all strings thus generated.

The observation that it doesn't matter where the zeroes and ones
appear in the strings leads to a solution that doesn't use s///:

    my $v = '1';
    for ( 1 .. 20 ) {
        my $n0 = $v =~ tr/0/1/;
        printf "%10d %10d\n",  $n0, length( $v) - $n0;
        $v .= '0' x ( length( $v) - $n0);
    }

I generates a different sequence of strings $v (less fancy patterns),
but with the same distribution of ones and zeroes.  If you prefer
to call it "cheating" I won't object.

Anno


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

Date: Wed, 25 May 2005 11:02:50 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: fibonacci string
Message-Id: <d71eq7$8ar$1@nntp.fujitsu-siemens.com>

Anno Siegel wrote:
> david k. wall <darkon.tdo@gmail.com> wrote in comp.lang.perl.misc:
>=20
>>here's a fun fact i ran across in the book _the golden ratio_, by=20
>>mario livio.  take the string '1' and replace it with '10'. =20
>>thereafter, replace any occurrence of '1' with '10' and '0' with '1'.  =

>>then count the number of 0s and 1s in the string.  you get a=20
>>fibonacci sequence for each count (offset by one iteration).
>=20
>=20
> Well, lessee...
>=20
> If string v has n0 zeroes and n1 ones, substituting all ones with
> '10' gives n1 ones and n1 zeroes.  Substituting zeroes with '1' adds
> another n0 ones (and no zeroes). So the number n0' of zeroes in the
> next string is n1, and the number n1' of ones is n0 + n1.
>=20
> If (n0, n1) are subsequent Fibonacci numbers, then (n0', n1') =3D
> (n1, n0 + n1) are the next two Fibonacci numbers.  Since this is true
> for the very first string '1', with 0 =3D fib[ 0] zeroes and 1 =3D fib[=
 1]
> ones, it is true for all strings thus generated.

Not bad. Very nice to see that there are still people around who can=20
_prove_ that an algorithm does what it is supposed to do rather than=20
deduce from a small set of results that all results follow a pattern.

Congratulations!

> The observation that it doesn't matter where the zeroes and ones
> appear in the strings leads to a solution that doesn't use s///:
>=20
>     my $v =3D '1';
>     for ( 1 .. 20 ) {
>         my $n0 =3D $v =3D~ tr/0/1/;
>         printf "%10d %10d\n",  $n0, length( $v) - $n0;
>         $v .=3D '0' x ( length( $v) - $n0);
>     }
>=20
> I generates a different sequence of strings $v (less fancy patterns),
> but with the same distribution of ones and zeroes.  If you prefer
> to call it "cheating" I won't object.

No, I'd call this professionalism.
I wish I'd be as thorough as you.

I bow my head,

Josef
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett



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

Date: Wed, 25 May 2005 14:06:24 +0200
From: Alexandre Jaquet <alexj@freesurf.ch>
Subject: get the current week nbr
Message-Id: <42946a41$0$1159$5402220f@news.sunrise.ch>

Hi again,

Rigth now I'm looking for a function to get the current week nbr we are.

Thanks in advance.

Alexandre Jaquet


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

Date: Wed, 25 May 2005 12:16:20 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: get the current week nbr
Message-Id: <Xns966153F3E841Dasu1cornelledu@127.0.0.1>

Alexandre Jaquet <alexj@freesurf.ch> wrote in news:42946a41$0$1159
$5402220f@news.sunrise.ch:

> Rigth now I'm looking for a function to get the current week nbr we are.

Posting here is not a substitute for searching CPAN yourself.

<URL: http://search.cpan.org/~sbeck/DateManip-5.42a/Manip.pod>

See, especially,

 $wkno = Date_WeekOfYear($m,$d,$y,$first);

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: Wed, 25 May 2005 14:22:58 +0200
From: Alexandre Jaquet <alexj@freesurf.ch>
Subject: Re: get the current week nbr
Message-Id: <42946e23$0$1164$5402220f@news.sunrise.ch>

A. Sinan Unur a écrit :
> Alexandre Jaquet <alexj@freesurf.ch> wrote in news:42946a41$0$1159
> $5402220f@news.sunrise.ch:
> 
> 
>>Rigth now I'm looking for a function to get the current week nbr we are.
> 
> 
> Posting here is not a substitute for searching CPAN yourself.
> 
> <URL: http://search.cpan.org/~sbeck/DateManip-5.42a/Manip.pod>
> 
> See, especially,
> 
>  $wkno = Date_WeekOfYear($m,$d,$y,$first);
> 
> Sinan
> 


great thanks


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

Date: 25 May 2005 10:08:46 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Need help with proper file locking of flatfile data
Message-Id: <d71ire$1sq$1@mamenchi.zrz.TU-Berlin.DE>

Randy <rbutcher.nospam@hotmail.com> wrote in comp.lang.perl.misc:
> "John W. Krahn" <someone@example.com> wrote in message
> news:WWTke.9376$HI.2387@edtnps84...
> > Randy wrote:
> > >
> > > I seem to be having some difficulties locking a simple counter flatfile
> > > database.
> >
> > That is covered by the FAQ:
> >
> > perldoc -q "I still don't get locking.  I just want to increment the
> number in
> > the file.  How can I do this"
> 
> I have checked perldoc many times. perldoc has only limited information on
> locking, and it's method is not adequate for high traffic flatfile database
> locking. I have learned that a sentinel locking file is essential, which
> perldoc does not cover.

Who told you so?  It's a myth, and not even a plausible one.  Even if
it were true, applying it at this stage is premature optimization.

Sentinel locking has nothig to do with traffic.  If anything, it is
less efficient than straight file locking.  You use it basically when
you want to protect a resource with flock semantics that isn't a (single,
local) file.

One example, as indicated, is locking of multiple files (the sentinel
can be one of the files or an extra one).  Another is the database a
hash might be tied to.  While the database technically is a file, Perl
doesn't treat it as such.  In particular, you don't routinely get a
handle to the database file, so you can't lock it.

This is the kind of problem you solve with a lock on a sentinel file.
If that is not your case (it doesn't seem to be), just go with the
FAQ solution.

Anno


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

Date: Wed, 25 May 2005 00:05:56 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: simplest method for renaming files / Windows ?
Message-Id: <WPSdnd0k-MBFvgnfRVn-rQ@comcast.com>

Geoff Cox wrote:

> I thought rename did not apply to Perl with Windows? am I wrong?

The rename() function works just fine with Windows.
Whatever gave you the impression that it doesn't?
(If you are referring to perl's '-i' command line option with
Windows, that is a different subject.)

> Assuming it does work do I have to?
> 
> 1. rename orig files to new names
> 2. delete orig files

But if you've done a rename, the original file is no longer
there, therefore there is nothing for step 2 to delete.

> 3. rename new files to orig file names

What?  You mean put everything back the same as it was?
Why do that if you are not going to change anything?

> Cannot just rename files in one step?

Sounds like you are very confused.

>>> 1. use Find::File
>>> 2. open each file and print OUT each line to  files with a new name
>>> 3. delete the original files
>>> 4. rename the new files to the original ones with another Perl script

That makes more sense.  Just do step 4 in your original script.

	==================================================

Here is how "perl -pi -e 's/foo/bar/g' *.txt" works with Unix:
   1) Open input file (whatever.txt) for reading.
   2) Delete the input file.  (Can't do this with Windows.)
   3) Open output file (whatever.txt).
   4) Read lines from the input file one at a time.
   5) Change all instances of 'foo' to 'bar'.
   6) Write the modified line to the output file.
   7) Repeat previous three steps until EOF.
   8) Close the input file.  If no processes still have it open
      for reading, this is when the file is actually deleted.
   9) Close the output file.
  10) Repeat for all files specified on the command line.

Here is how 'perl -pi.bak -e "s/foo/bar/g" *.txt' works (Windows + Unix)
   1) Rename filename to filename.bak (whatever.txt to whatever.txt.bak).
   2) Open the renamed file for reading.
   3-7) = same as above
   8) Close the input file.  The original file is still there, albeit
      with a different name.
   9-10) same as above.

Here's how to do those steps using File::Find

   sub wanted {
     my $fil = $_;	# Name of input file, relative to current dir
     my $tmp = "$fil.tmp";
     rename $fil,$tmp or die "Cannot rename $fil to $tmp - $!\n";
     open my $out,'>',$fil or die "Cannot write to $fil - $!\n";
     open my $in, '<',$tmp or die "Cannot read from $tmp - $!\n";
     while (defined my $line = <>) {
       $line =~ s/foo/bar/g;
       print $out $line or warn "Problem writing to $fil - $!\n";
     }
     close $out or warn "Problems closing $fil - $!\n";
     close $in;
     unlink $in or warn "Unable to remove $tmp - $!\n";
   }


	-Joe


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

Date: Wed, 25 May 2005 11:42:02 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: simplest method for renaming files / Windows ?
Message-Id: <Xns96614E237F1DEasu1cornelledu@127.0.0.1>

Geoff Cox <geoff.cox@notquitecorrectfreeuk.com> wrote in 
news:vi6891tknivdpmqn60tg8bbtgnjpk4jmim@4ax.com:

> On 25 May 2005 06:00:23 GMT, John Bokma <john@castleamber.com> wrote:

What did John write? You have been around long enough to know to quote 
some context when replying.

> I have just tried
> 
> rename ("d://perl/progs/test/file1.txt","file2.txt") ;
> 
> and 
> 
> this appears to delete the orig file and create new file with the new
> name ...

perldoc -f rename

-- 
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: Wed, 25 May 2005 12:17:56 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: simplest method for renaming files / Windows ?
Message-Id: <U1_ke.5257$GN3.2235@trnddc04>

Geoff Cox wrote:
> At the moment I
> 1. use Find::File
> 2. open each file and print OUT each line to  files with a new name
> 3. delete the original files
> 4. rename the new files to the original ones with another Perl script

If you don't mind asking me, but what is the purpose of this excercise, i.e. 
what are you trying to achive with steps 1-4 above?
For all practical purposes you got an identical set of files, just some file 
attributes (created, last modified, maybe owner, group, access right) would 
be different and there are better ways to change those than to copy each and 
every file and rename it back to the original name.

> I know there are better ways - but am confused as to which to use.
> Which is the simplest?

Depends on what you actually want to achive. Your steps 1-4 are almost a 
NOP.

jue




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

Date: Wed, 25 May 2005 07:57:43 GMT
From: Tim Hammerquist <tim@vegeta.ath.cx>
Subject: Re: Strange 'for' behaviour?
Message-Id: <slrnd98bjo.6o4.tim@vegeta.saiyix>

Tad McClellan <tadmc@augustmail.com> wrote:
>  Tim Hammerquist <tim@vegeta.ath.cx> wrote:
> > Indeed.  Thanks for the correction.
> > 
> > /me reverts to "lurk" mode until he can learn to post while not half
> >     asleep.
>  
> No, no! Let's simply turn it into a Learning Opportunity and you'll
> end up being credited with starting a valuable sub-thread.
>  
> Lessee...
>  
> Before I even noticed the typo, I was going to suggest
> 
>    foreach my $year ( reverse 1999 .. 2005 )
> 
> because it would be harder to screw that one up.
> 
> Then I noticed that you had proven my case!
> 
> Thanks man.

Um... you're welcome?

Tim Hammerquist
-- 
I'm brave but my feet aren't!
Damn feet!
    -- Akito Tenkawa, "Martian Successor: Nadesico"


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

Date: Wed, 25 May 2005 00:12:23 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: substitution
Message-Id: <TcSdnc4N9-TBuAnfRVn-jA@comcast.com>

Alexandre Jaquet wrote:
> I would like to replace all fields values who begin by $ERROR with empty 
> 
> I've try something like :
> s/\$ERROR{'([\w]+)'}/ /;

Why bother with the character class?  Just gobble up everything
from the open brace to the close brace.  (Note: you neglected to
put backslashes before { and } above.)

   s/\$ERROR\{.*?\}/ /g;

	-Joe


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

Date: Wed, 25 May 2005 07:07:22 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: substitution
Message-Id: <slrnd98qjq.53r.tadmc@magna.augustmail.com>

Joe Smith <joe@inwap.com> wrote:
> Alexandre Jaquet wrote:

>> s/\$ERROR{'([\w]+)'}/ /;

> (Note: you neglected to
> put backslashes before { and } above.)
> 
>    s/\$ERROR\{.*?\}/ /g;


Note that you do not need to put backslashes before { and }
in either of the above.

perl's parser is rather too clever.  :-)


------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = "\$ERROR{'foobar'}\n";
print "matched [$&]\n" if s/\$ERROR{'([\w]+)'}/ /;
print;

$_ = "\$ERROR{'foobar'}\n";
print "matched [$&]\n" if s/\$ERROR{.*?}/ /g;
print;
------------------


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


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