[22549] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4770 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 27 18:31:22 2003

Date: Thu, 27 Mar 2003 15:30:39 -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, 27 Mar 2003     Volume: 10 Number: 4770

Today's topics:
        simple substitution also changes line endings (Tuang)
    Re: simple substitution also changes line endings (Tad McClellan)
    Re: simple substitution also changes line endings <Norbert_Schmidt@DU3.MAUS.DE>
    Re: simple substitution also changes line endings <michael.p.broida@boeing.com>
    Re: simple substitution also changes line endings <please@no.spam>
    Re: Simple, but I can't figure it out. (Corey Andrews)
    Re: Simple, but I can't figure it out. (Tad McClellan)
    Re: Sorting keys in a hash runs slower now. <glex_nospam@qwest.net>
    Re: Sorting keys in a hash runs slower now. <tore@aursand.no>
    Re: Sorting keys in a hash runs slower now. <tzz@lifelogs.com>
    Re: Sorting keys in a hash runs slower now. <goldbb2@earthlink.net>
    Re: Sorting keys in a hash runs slower now. (C Marshall)
    Re: Sorting keys in a hash runs slower now. (C Marshall)
    Re: Sorting keys in a hash runs slower now. (C Marshall)
        split a long line into many lines <ningli2000@worldnet.att.net>
    Re: split a long line into many lines <grazz@nyc.rr.com>
    Re: split a long line into many lines (Sam Holden)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 27 Mar 2003 12:08:20 -0800
From: tuanglen@hotmail.com (Tuang)
Subject: simple substitution also changes line endings
Message-Id: <df045d93.0303271208.2fbdf03c@posting.google.com>

I'm using Perl on my Win2K laptop. I installed Cygwin to give me all
the standard Unix tools I find so useful, and I mostly access Perl
thru Cygwin. (It's the ActiveState version of Perl.)

I tried to create a nice unix filter template, as follows:

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

#!/cygdrive/c/Perl/bin/perl -w -i.bak -p
#

# insert processing code here:
s/A/X/g;

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

I create a source file and put this into it:

A
B
C

Viewed with a hex editor, that is:

41 0A 42 0A 43 0A

(41=A, 42=B, 43=C, 0A=LF). As you can see, the line endings are unix
style (LF), as expected. Good.

Then, I run my little filter program on the file, and view the results
with the hex editor:

58 0D 0A 42 0D 0A 43 0D 0A

The 'A' (41) has been converted to an 'X' (58), as I requested. But
all of the line endings have been changed to DOS/Windows-style line
endings, which I did NOT request.

I REALLY don't want a filter to change anything I didn't ask it to
change. If I want it to modify something, like line endings, I'll
include that change in the filter. I'm using Perl in part because I
want to manipulate files from several different platforms using the
same tool, no matter what platform I'm on at the moment.

Is there any option or line of code or something that I could insert
in the filter to tell it "don't make any changes that I don't ask
for"? I realize that I can always run another filter to repair line
endings if I notice the corruption, but that's not what I'm asking.
I'm wondering if there's a way to tell it to just leave everything
alone unless I tell it to change it.

Thanks.


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

Date: Thu, 27 Mar 2003 15:15:28 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: simple substitution also changes line endings
Message-Id: <slrnb86qfg.4ir.tadmc@magna.augustmail.com>

Tuang <tuanglen@hotmail.com> wrote:

> I'm using Perl on my Win2K laptop. 

> But
> all of the line endings have been changed to DOS/Windows-style line
> endings, which I did NOT request.


Yes you did (by omission).


> Is there any option or line of code or something that I could insert
> in the filter to tell it "don't make any changes that I don't ask
> for"? 


   perldoc -f binmode


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


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

Date: Thu, 27 Mar 2003 22:25:41 +0100
From: Norbert Schmidt <Norbert_Schmidt@DU3.MAUS.DE>
Subject: Re: simple substitution also changes line endings
Message-Id: <ejq68vgkn2gutsedav2d6f2e9lq0c8efd2@4ax.com>

Hello Tuang,

>I'm using Perl on my Win2K laptop. 
> (It's the ActiveState version of Perl.)
>I tried to create a nice unix filter template, as follows:

>The 'A' (41) has been converted to an 'X' (58), as I requested. But
>all of the line endings have been changed to DOS/Windows-style line
>endings, which I did NOT request.

well, you did, indirectly ... ;-)
Since AS-Perl runs on Windows it tries to do the right thing by
folding "\r\n" on input to "\n" and expanding it again to "\r\n" under
the (usually correct) assumption that native Windows tools expect line
ending this way.

>Is there any option or line of code or something that I could insert
>in the filter to tell it "don't make any changes that I don't ask
>for"?

Of course, there is! :-)
binmode() is your friend! 

Simply turn binmode on for all files (STDIN/STDOUT) and you'll read
and write files raw.

You can leave this in if you run the script on *nix, where it just
does nothing.

Regards, Norbert



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

Date: Thu, 27 Mar 2003 21:21:15 GMT
From: "Michael P. Broida" <michael.p.broida@boeing.com>
Subject: Re: simple substitution also changes line endings
Message-Id: <3E836B4B.1572E594@boeing.com>

Tuang wrote:
> 
> I'm using Perl on my Win2K laptop. I installed Cygwin to give me all
> the standard Unix tools I find so useful, and I mostly access Perl
> thru Cygwin. (It's the ActiveState version of Perl.)
> 
> I tried to create a nice unix filter template, as follows:
> 
> ==========================
> 
> #!/cygdrive/c/Perl/bin/perl -w -i.bak -p
> #
> 
> # insert processing code here:
> s/A/X/g;
> 
> ==========================
> 
> I create a source file and put this into it:
> 
> A
> B
> C
> 
> Viewed with a hex editor, that is:
> 
> 41 0A 42 0A 43 0A
> 
> (41=A, 42=B, 43=C, 0A=LF). As you can see, the line endings are unix
> style (LF), as expected. Good.
> 
> Then, I run my little filter program on the file, and view the results
> with the hex editor:
> 
> 58 0D 0A 42 0D 0A 43 0D 0A
> 
> The 'A' (41) has been converted to an 'X' (58), as I requested. But
> all of the line endings have been changed to DOS/Windows-style line
> endings, which I did NOT request.
> 
> I REALLY don't want a filter to change anything I didn't ask it to
> change. If I want it to modify something, like line endings, I'll
> include that change in the filter. I'm using Perl in part because I
> want to manipulate files from several different platforms using the
> same tool, no matter what platform I'm on at the moment.
> 
> Is there any option or line of code or something that I could insert
> in the filter to tell it "don't make any changes that I don't ask
> for"? I realize that I can always run another filter to repair line
> endings if I notice the corruption, but that's not what I'm asking.
> I'm wondering if there's a way to tell it to just leave everything
> alone unless I tell it to change it.

	I think it's probably Windows doing that to you.
	If you write a file in Windows NORMALLY, it puts
	in Windows line-termination.  "Normally" means
	just line-by-line output.

	You can do a more "character-oriented" I/O and
	Windows won't touch that.  Emacs must use that
	method because it will (if told to) leave the
	line-terminations alone.  I don't know how to
	make Perl do the same.

	Basically: don't edit Unix files on a Windows
	system.  <grin>

		Mike


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

Date: Thu, 27 Mar 2003 22:39:46 +0000
From: Chris Lowth <please@no.spam>
Subject: Re: simple substitution also changes line endings
Message-Id: <v5Lga.1509$8s6.13475@newsfep4-glfd.server.ntli.net>

Tuang wrote:

> I'm using Perl on my Win2K laptop. I installed Cygwin to give me all
> the standard Unix tools I find so useful, and I mostly access Perl
> thru Cygwin. (It's the ActiveState version of Perl.)
> 
> I tried to create a nice unix filter template, as follows:
> 
> ==========================
> 
> #!/cygdrive/c/Perl/bin/perl -w -i.bak -p
> #
> 
> # insert processing code here:
> s/A/X/g;
> 
> ==========================
> 
> I create a source file and put this into it:
> 
> A
> B
> C
> 
> Viewed with a hex editor, that is:
> 
> 41 0A 42 0A 43 0A
> 
> (41=A, 42=B, 43=C, 0A=LF). As you can see, the line endings are unix
> style (LF), as expected. Good.
> 
> Then, I run my little filter program on the file, and view the results
> with the hex editor:
> 
> 58 0D 0A 42 0D 0A 43 0D 0A
> 
> The 'A' (41) has been converted to an 'X' (58), as I requested. But
> all of the line endings have been changed to DOS/Windows-style line
> endings, which I did NOT request.
> 
> I REALLY don't want a filter to change anything I didn't ask it to
> change. If I want it to modify something, like line endings, I'll
> include that change in the filter. I'm using Perl in part because I
> want to manipulate files from several different platforms using the
> same tool, no matter what platform I'm on at the moment.
> 
> Is there any option or line of code or something that I could insert
> in the filter to tell it "don't make any changes that I don't ask
> for"? I realize that I can always run another filter to repair line
> endings if I notice the corruption, but that's not what I'm asking.
> I'm wondering if there's a way to tell it to just leave everything
> alone unless I tell it to change it.
> 
> Thanks.

Do you remember that question that the cygwin installer asked about whether 
to use DOS or Unix line-ends? I think I can guess how you answered that 
question. It's cygwin, not perl really. It's doing what you asked it to do. 
If you want cygwin programs to behave like real unix ones you can either 
change the setting (ermm .. not sure how), or re-install cygwin (heavy!). 
If you are only concerned about perl, use "binmode()" in your script.

Sup2u

By the way, having told cygwin to do DOS line-ends, you'll find that a lot 
of the standard cygwin tools are doing the same thing.

Chris

-- 
My real address is: chris at lowth dot sea oh em
-> OpenSource e-mail virus protection : http://protector.sourceforge.net
-> iptables configuration wizards : http://www.lowth.com/LinWiz


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

Date: 26 Mar 2003 15:35:00 -0800
From: matirxrabbit@Hotmail.com (Corey Andrews)
Subject: Re: Simple, but I can't figure it out.
Message-Id: <8e213620.0303261535.623b5df2@posting.google.com>

"Alan J. Flavell" <flavell@mail.cern.ch> wrote in message news:<Pine.LNX.4.53.0303242338200.22946@lxplus065.cern.ch>...

Hey guys. Thanks for all your help. And yeah, as you probably noticed,
not only am I a newbie to perl, but to posting on groups as well.
Sorry about that.  I still have to learn all the 'internet etiquettes'
of posting.

> What's happening here is that you're tackling a relatively difficult
> task for a newcomer (i.e writing a server-side script) but you seem to
> lack the relevant background in designing a program to do what you
> want, quite apart from any issues about coding it in Perl (the topic
> for this group) let alone the issues of writing a server-side script
> for the Common Gateway Interface (which is properly the business of
> the comp.infosystems.www.authoring.cgi group).
> 
> No need to get upset about that, and no offence meant, but it's better
> to be able to walk a few steps before entering for a half-marathon.
>
> It might be a help if you'd reveal any background that you might have
> in programming, but if this is your first encounter with the field,
> then I'd seriously suggest buying a Perl-related introductory book and
> playing around with the language before worrying about CGI scripts.

To that I have to say, yes..this is the first language I've ever dealt
with in general.  I run my own httpd server on my Slackware box so
I've been exposed to scripts. I need to write certain CGI scripts for
my site.  I'd much rather not just download pre-written
scripts...where not only, in my opinion, contain a lot of fluff, but I
don't learn much.  I've written a few CGI scripts in perl, basic ones
of course.  But i'm using the CGI/perl as a base step before I take a
leap into hardcore perl programming.

> The "Llama book" i.e learning Perl would be a fine start, for example.
> (No, I don't get anything out of recommending it, I just think it's
> very good of its kind - get the newest edition though...)

Thanks for the recommendation. I will look into that book.


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

Date: Wed, 26 Mar 2003 19:26:14 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Simple, but I can't figure it out.
Message-Id: <slrnb84kpm.2dq.tadmc@magna.augustmail.com>

Corey Andrews <matirxrabbit@Hotmail.com> wrote:


> I still have to learn all the 'internet etiquettes'
> of posting.


      http://mail.augustmail.com/~tadmc/clpmisc.shtml


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


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

Date: Wed, 26 Mar 2003 12:02:08 -0600
From: Jeff D Gleixner <glex_nospam@qwest.net>
Subject: Re: Sorting keys in a hash runs slower now.
Message-Id: <GTlga.565$fH3.26551@news.uswest.net>

[...]
> Now as it is only sorting 25 values I wouldn't expect it to slow down
> so much.
> 
> Any idea what is causing the slowdown ?

For one, it's rebuilding the pillar_hash every time your sort function is
called, which, if you put a print "blah" in pillar_order, you'll
see just how many times it's called.

Start by making %pillar_hash only once, as a file-scoped lexical. (Move it
and the map out of pillar_order.)  Then pillar_order becomes:
sub pillar_order {
         $pillar_hash{$main::a} <=> $pillar_hash{$main::b};
}



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

Date: Wed, 26 Mar 2003 20:23:38 +0100
From: "Tore Aursand" <tore@aursand.no>
Subject: Re: Sorting keys in a hash runs slower now.
Message-Id: <pan.2003.03.26.18.15.03.151103@aursand.no>

On Wed, 26 Mar 2003 08:07:15 -0800, C Marshall wrote:
> foreach my $term (sort MXDB::pillar_order ...

This means that %pillar_hash (inside 'pillar_order()') will be rebuilt
every time.  I can't see why you need to do that.

One general tip;  Do you have to call 'print' every time?  It's more
efficient to gather the output you want in a variable, and then print that
variable at the end of your script.

> my %pillar_hash = map {$_, $i++} @pillars;

As mentioned, you should create this hash outside the sub-routine.  In
addition, you could create the hash in the first place (and forget about
the use of an array and 'map').


-- 
Tore Aursand


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

Date: Wed, 26 Mar 2003 14:35:50 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Sorting keys in a hash runs slower now.
Message-Id: <4nhe9pgbu1.fsf@lockgroove.bwh.harvard.edu>

On 26 Mar 2003, c_j_marshall@hotmail.com wrote:
> I have a sizable hash - one section of which I was sorting manually
> and it ran in an acceptable timeframe.
> 
> However the sort order needed to be pulled out to a module as it is
> subject to change over time and many scripts may make use of
> it. This also lets me correct a bug in the old logic where 18M
> (which equals 18months) would come before 1Y (one year)
> Unfortunately in doing so the script now runs 20 times slower.
 ...
> Now as it is only sorting 25 values I wouldn't expect it to slow
> down so much.
> 
> Any idea what is causing the slowdown ?
> 
> There is other stuff in MXDB including a "use DBI;" which I would
> understand causing a slowdown when it started - but I can see the
> code is just running slower on every iteration.

Use the Benchmark module on the particular subroutines of interest,
and see if there really is a difference in performance.  The Benchmark
module will also give you a concrete measurement of performance, as
opposed to "running slower" and "20 times slower."

Ted


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

Date: Thu, 27 Mar 2003 02:07:12 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Sorting keys in a hash runs slower now.
Message-Id: <3E82A320.5DB54DE5@earthlink.net>

C Marshall wrote:
[snip]
> And then the implementation in MXDB.pm is:
> 
> sub pillar_order {
>         my $pillar_a = $main::a;
>         my $pillar_b = $main::b;
> 
>         my @pillars = ( 'O/N',
[snip]
>                         '30Y');
> 
>         my $i=0;
>         my %pillar_hash = map {$_, $i++} @pillars;
> 
>         return $pillar_hash{$pillar_a} <=> $pillar_hash{$pillar_b};
> }

How about an algorithmic comparison?

   my %short = qw( O/N 1 T/N 2 S/N 3 );
   my %scale = qw( Y 1 M 12 W 52 );
   *a = *main::a; *b = *main::b;
   sub pillar_order {
      if( exists $short{$a} and exists $short{$b} ) {
         $short{$a} cmp $short{$b};
      } elsif( exists $short{$a} ) {
         -1;
      } elsif( exists $short{$b} ) {
         +1;
      } else {
         my ($aval, $atype) = $a =~ /(\d+)(.)/;
         my ($bval, $btype) = $b =~ /(\d+)(.)/;
         $aval / $scale{$atype} cmp $bval / $scale{$btype};
      }
   }

Also, consider changing your original code to use a temp variable to be
cleaner/easier to read:

   my $dvc = $all{$date}{vol}{$ccy};
   foreach my $term (sort MXDB::pillar_order keys %$dvc) {
      printf "%.4f;", $dvc->{$term};
   }

Or:

   my $dvc = $all{$date}{vol}{$ccy};
   print map sprintf("%.4f;", $_),
      @{$dvc}{sort MXDB::pillar_order keys %$dvc};

[all code here is untested]

-- 
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}


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

Date: 27 Mar 2003 01:58:55 -0800
From: c_j_marshall@hotmail.com (C Marshall)
Subject: Re: Sorting keys in a hash runs slower now.
Message-Id: <cb9c7b76.0303270158.7ef2ec2a@posting.google.com>

Jeff D Gleixner <glex_nospam@qwest.net> wrote in message news:<GTlga.565$fH3.26551@news.uswest.net>...
> > Any idea what is causing the slowdown ?
> 
> For one, it's rebuilding the pillar_hash every time your sort function is
> called, which, if you put a print "blah" in pillar_order, you'll
> see just how many times it's called.
> 
> Start by making %pillar_hash only once, as a file-scoped lexical. (Move it
> and the map out of pillar_order.)  Then pillar_order becomes:
> sub pillar_order {
>          $pillar_hash{$main::a} <=> $pillar_hash{$main::b};
> }


Very good advice (Jeff and others) - its working perfectly now. 
Thank you.


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

Date: 27 Mar 2003 02:05:52 -0800
From: c_j_marshall@hotmail.com (C Marshall)
Subject: Re: Sorting keys in a hash runs slower now.
Message-Id: <cb9c7b76.0303270205.7bf8e46a@posting.google.com>

Benjamin Goldberg <goldbb2@earthlink.net> wrote in message 
> 
> How about an algorithmic comparison?
> 
>    my %short = qw( O/N 1 T/N 2 S/N 3 );
>    my %scale = qw( Y 1 M 12 W 52 );
>    *a = *main::a; *b = *main::b;
>    sub pillar_order {
>       if( exists $short{$a} and exists $short{$b} ) {
>          $short{$a} cmp $short{$b};
>       } elsif( exists $short{$a} ) {
>          -1;
>       } elsif( exists $short{$b} ) {
>          +1;
>       } else {
>          my ($aval, $atype) = $a =~ /(\d+)(.)/;
>          my ($bval, $btype) = $b =~ /(\d+)(.)/;
>          $aval / $scale{$atype} cmp $bval / $scale{$btype};
>       }
>    }
> 

Please could you explain how this works in more detail for me.


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

Date: 27 Mar 2003 02:06:38 -0800
From: c_j_marshall@hotmail.com (C Marshall)
Subject: Re: Sorting keys in a hash runs slower now.
Message-Id: <cb9c7b76.0303270206.5d4bb43d@posting.google.com>

"Tore Aursand" <tore@aursand.no> wrote in message news:<pan.2003.03.26.18.15.03.151103@aursand.no>...
> On Wed, 26 Mar 2003 08:07:15 -0800, C Marshall wrote:
> > foreach my $term (sort MXDB::pillar_order ...
> 
> This means that %pillar_hash (inside 'pillar_order()') will be rebuilt
> every time.  I can't see why you need to do that.
> 
> One general tip;  Do you have to call 'print' every time?  It's more
> efficient to gather the output you want in a variable, and then print that
> variable at the end of your script.

More good advice - I did that too and it runs faster than it did before.
Thank you


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

Date: Thu, 27 Mar 2003 03:27:40 GMT
From: "Ning li" <ningli2000@worldnet.att.net>
Subject: split a long line into many lines
Message-Id: <Mcuga.26650$ja4.1811847@bgtnsc05-news.ops.worldnet.att.net>

Hi,

    I want to split a file containing one long line into many lines with
equal length, like 100 character per line. Is there a simple way in Perl to
do that?

    Thanks in advance.

    Nick




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

Date: Thu, 27 Mar 2003 03:34:22 GMT
From: Steve Grazzini <grazz@nyc.rr.com>
Subject: Re: split a long line into many lines
Message-Id: <2juga.13434$iE4.4641415@twister.nyc.rr.com>

Ning li <ningli2000@worldnet.att.net> wrote:
> 
> I want to split a file containing one long line into 
> many lines with equal length, like 100 character per 
> line. Is there a simple way in Perl to do that?
> 

You just have to set

   $/ = \100;

And then read from the filehandle as usual.

   while (<FILE>) {
       ... $_ is the next 100 characters ...
   }

HTH
-- 
Steve


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

Date: 27 Mar 2003 04:03:43 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: split a long line into many lines
Message-Id: <slrnb84u0v.vp9.sholden@flexal.cs.usyd.edu.au>

On Thu, 27 Mar 2003 03:34:22 GMT, Steve Grazzini <grazz@nyc.rr.com> wrote:
> Ning li <ningli2000@worldnet.att.net> wrote:
>> 
>> I want to split a file containing one long line into 
>> many lines with equal length, like 100 character per 
>> line. Is there a simple way in Perl to do that?
>> 
> 
> You just have to set
> 
>    $/ = \100;
> 
> And then read from the filehandle as usual.
> 
>    while (<FILE>) {
>        ... $_ is the next 100 characters ...
>    }

If the OP actually has a line a long line of text and wants to break insert
line breaks between words (rather than exactly at 100 characters, which is 
I admit what was asked), then there are a few CPAN modules that will do it.

Text::Wrap is probably the simplest. 

If that's not what was wanted, then feel free to ignore me...

-- 
Sam Holden



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

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


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