[22826] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5047 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 27 14:06:24 2003

Date: Tue, 27 May 2003 11:05:06 -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           Tue, 27 May 2003     Volume: 10 Number: 5047

Today's topics:
    Re: converting string of [01] to (binary) number <krahnj@acm.org>
    Re: converting string of [01] to (binary) number <dbo@xbn.nl>
        how to not cause a file lock? (peder)
    Re: how to not cause a file lock? <tassilo.parseval@rwth-aachen.de>
        LWP::Useragent needs to be flushed if the response is h (Nagaraj)
    Re: Makefile corrupted after running perl Makefile.PL. (Randy Kobes)
        Module to decode MS Word documents? <stjm2@cam.ac.uk>
        NEWBIE: Store and Access Arrays (Sylvie Stone)
        Reading file segments into an array <ventil8@supanet.com>
        Reading file segments into an array <ventil8@supanet.com>
    Re: Reading file segments into an array <g4rry_short@zw4llet.com>
    Re: Reading file segments into an array <krahnj@acm.org>
        Reverse Reference Question <mooseshoes@gmx.net>
    Re: scope of variable <jasonp@uq.net.au>
    Re: Sorting hash <ddunham@redwood.taos.com>
    Re: Sorting hash <uri@stemsystems.com>
    Re: Sorting hash <ddunham@redwood.taos.com>
        STDOUT print not showing up (Jan)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 27 May 2003 16:36:18 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: converting string of [01] to (binary) number
Message-Id: <3ED393D7.E09F28EC@acm.org>

news@roaima.freeserve.co.uk wrote:
> 
>     my $a="0000_0000_0011_0000_1010_1100";
> 
>     my @nibbles = split (/_/, $a);
>     while (my $byte = shift (@nibbles) . shift (@nibbles)) {
>         my $hex = uc sprintf "%02x", ord pack("B8","$byte");
                    ^^^^^^^^^^^^^^^^^
                    sprintf '%02X',

>         print "$hex\n"
>     }

:-)

John
-- 
use Perl;
program
fulfillment


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

Date: Tue, 27 May 2003 18:52:42 +0200
From: David Bouman <dbo@xbn.nl>
Subject: Re: converting string of [01] to (binary) number
Message-Id: <3ED397DA.2B28B4B8@xbn.nl>

Eric Moors wrote:

> I'm trying to split a string of n*8 characters in their hexadecimal
> counterparts. A small example:
> 
> As input I have the ascii string:
> 
> "0000_0000_0011_0000_1010_1100"
> 
> and I want to convert this to the ascii string:
> 
> 00
> 30
> AC
> 
> ...
> I looked into (un)pack, but couldn't get any lifesign out of that.

$_ = "0000_0000_0011_0000_1010_1100";
y/_//d;
my @r = unpack "H2"x(length>>3),pack "B*",$_;
print "$_\n" for @r;

or.. if you want one result string, as you seem to imply:

my $r = unpack "H*",pack "B*", $_;
print "$r\n";

You may want to uc() the result(s) too..

--
D.


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

Date: 27 May 2003 10:51:27 -0700
From: peder@stickomdeintpassa.com (peder)
Subject: how to not cause a file lock?
Message-Id: <3d4fb39f.0305270951.5d458cec@posting.google.com>

hi

i'm working on a project where i will read a text file from a network
share (win32). it is crucial that this file doesn't get locked by the
perl script since the file is business critical.

how can i prevent this? 

is there like a safe way of doing this or is there a dodgy way that
works?

or is it impossible? and in that case, is it quicker to first make a
local copy of the file and then have perl read it?

all thoughts appreciated.


thanks all

peder
sthlm


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

Date: 27 May 2003 18:04:01 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: how to not cause a file lock?
Message-Id: <bb09ah$600$1@nets3.rz.RWTH-Aachen.DE>

Also sprach peder:

> i'm working on a project where i will read a text file from a network
> share (win32). it is crucial that this file doesn't get locked by the
> perl script since the file is business critical.
> 
> how can i prevent this? 
> 
> is there like a safe way of doing this or is there a dodgy way that
> works?

Perl wont lock any files unless you tell it to do so. Thus if you simply
open the file without any further concerns, your desired behaviour
should be the default.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: 27 May 2003 08:47:10 -0700
From: cpmurali@hotmail.com (Nagaraj)
Subject: LWP::Useragent needs to be flushed if the response is huge
Message-Id: <3dc53e98.0305270747.7454fc57@posting.google.com>

We are using the LWP UserAgent class.  It works great except when very 
large content is returned.  When that happens none of the output gets 
flushed until the entire page is cached.  According to the documentation 
their is a way to use the object and pass it a subroutine to handle the 
output (possibly in small chunks :) 
My question is:  Does anyone have a code fragment that would help us 
implement a subroutine that would simple flush the content as it arrives?
Thanks in Advance.


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

Date: 27 May 2003 15:13:56 GMT
From: randy@theoryx5.uwinnipeg.ca (Randy Kobes)
Subject: Re: Makefile corrupted after running perl Makefile.PL.
Message-Id: <slrnbd6vbv.3ra.randy@theoryx5.uwinnipeg.ca>

On 27 May 2003 06:56:48 -0700, ZorroFree <zorans@kekec2.e5.ijs.si> wrote:
>Good day there.
>
>Although I am certaintly very new to anythink concerning Perl,
>I think I've got a very essential Makefile corruption problem.
>The problem allready occured to me when trying to build two
>distinct products (actualy Time:HiRes module and SpamAssasin
>anti spam utility).
>When I commit perl Makefile.PL, as a result I get Makefile
>which is very odd: single quotes are placed incorrectly arround
>values to variables, some variables are set strange (most probably
>vandalized) directory values (for example /u or ib/perl5) and
>lines with suspicious meaning are inserted between other lines
>that makes sense according to Makefile syntax.

There was another post recently that reported a similar
problem. It may be due to a corrupt rpm. What happens if
you do, for example,
   bash$ perl -V:cc
Does it report an error? If you look through Perl's Config.pm
(under something like /usr/lib/perl5/5.x.x/i686-linux/), can
you see if there's any unmatched quotes? The ultimate solution
may be to rebuild Perl yourself.

-- 
best regards,
randy kobes


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

Date: Tue, 27 May 2003 18:11:53 +0100
From: "Stuart Moore" <stjm2@cam.ac.uk>
Subject: Module to decode MS Word documents?
Message-Id: <bb068p$mdc$1@pegasus.csx.cam.ac.uk>

Are there any modules that'd help me grab text from a word document? Doesn't
matter if it looses all formatting etc but it'll need to get text from
inside tables, preferably leaving some mark where the table boundaries are
but not essential. A quick browse of CPAN revealied nothing.

If not, any suggestions for other languages (preferably ones that are easily
used for cgi)

Stuart




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

Date: 27 May 2003 10:30:16 -0700
From: sylviestone@canada.com (Sylvie Stone)
Subject: NEWBIE: Store and Access Arrays
Message-Id: <181a24a8.0305270930.670a81@posting.google.com>

Hi - I'm sure I'll get flamed for this but I've read the faq and did
not find quite what I was looking for. Maybe I'm going about it all
wrong.

I have a web form that pulls data from a datbase on submit, then
prints it to screen. I'd like to give the user an option to have the
data emailed to them.

This is what I have. I can print to screen, but I can't *store* the
array to be used later on in the script to build the email body. It's
on the Intranet, otherwise I'd post a URL.

THANK YOU! Sylvie

# Display names of registered profiles

if ($p{namesusing}) {

my $sql = qq{select lname, fname, email from $tablename1 where custid
in (select custid from $tablename2) or custid in (select custid from
$tablename3) order by lname};
   $b = 0;
   if (($sqh = $dbh->prepare($sql))) {
           $sqh->execute() or &InsertError() and die;
           $b = 1;
      }
      print "<tr><td>\n";
      print "Display names of registered profiles:\n";
      print "</td></tr>\n";

      if ($b) {
      $count=0;
      while (($first, $second, $third) = $sqh->fetchrow()) {
              $count++;
              print "<tr><td>$count $first, $second |
third</td></tr>\n";
              push(@record,"$first, $second, $third");

        } # end while retreiving
      } # end if success from database
    } # end if display requested

< snip >

$recordall = @record;

if ($recordall > 0){
  for $i (0..$#record)
  {
    print "$record[$i]";
  }
} else {
  print "\n           ** EMPTY **\n";
}


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

Date: Tue, 27 May 2003 17:26:24 +0100
From: "Ivan Dragomilov" <ventil8@supanet.com>
Subject: Reading file segments into an array
Message-Id: <3ed39226$0$29708$cc9e4d1f@news.dial.pipex.com>

Hi all,

I am extremely new to PERL and need some pointers to achieve the result I am
looking for.

I have a file that looks like this:

::NR
::TI
A study in archeology
::AU
Smith, J.
::PY
2002

::NR
::TI
Forensic study for dummies
::PY
2003

::NR
::TI
I am what I am
::AU
Dullard, A.

::NR
::TI
This record is almost completely empty

::NR
::TI
H.E.L.P.
::AU
Other, Ann
::PY
2003


The objective:

I want to read each line of the file in a line at a time and load the
contents of each line into a specific part of an array, leaving dummy
references where fields are omitted in the input.

For instance, the array might be called @record and should look something
like this -

$record[0] contains the tag "::TI"
$record[1] contains the data "A study in archeology"
$record[2] contains the tag "::AU"
$record[3] contains the data "Smith, J."
$record[4] contains the tag "::PY"
$record[5] contains the data "2002"

The array is processed and emptied at the end of a series of other functions
and the next one is loaded.  Note that the fields are mispopulated.

$record[0] contains the tag "::TI"
$record[1] contains the data "Forensic study for dummies"
$record[2] contains the tag "::PY"
$record[3] contains the data "2003"
$record[4] contains nothing.
$record[5] contains nothing.

If I do the following (Note - This code is incomplete):

while ($line = <Inp_File>) {
    if ($line ne "\:\:NR\n") {
        push(@record,$line);
    }

This reproduces the desired outcome in part.  The problem is that when
record 2 is encountered, the ::AU tag is missing and so the array is
incorrectly populated.  Similarly, record 3 is missing the "::PY" tag, but
not the "::AU" and record 4 is missing nearly all of the fields.

The tag "::NR" New Record is a constant and signifies the beginning of a
record.

I tried scanning the lines as they were read in, but came across a stumbling
block.  If I look for a particular tag and assign it to its correct position
in the array, how do I tell the program to assign the next line of input,
from the file, to the corresponding array position.

Any ideas that you have will be well received and code snippets with
explanations would be really welcome.  I'm banging my head on this and the
answer is probably staring me in the face.

Many thanks in advance.



Ivan D.




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

Date: Tue, 27 May 2003 17:26:51 +0100
From: "Ivan Dragomilov" <ventil8@supanet.com>
Subject: Reading file segments into an array
Message-Id: <3ed39226$1$29708$cc9e4d1f@news.dial.pipex.com>

Hi all,

I am extremely new to PERL and need some pointers to achieve the result I am
looking for.

I have a file that looks like this:

::NR
::TI
A study in archeology
::AU
Smith, J.
::PY
2002

::NR
::TI
Forensic study for dummies
::PY
2003

::NR
::TI
I am what I am
::AU
Dullard, A.

::NR
::TI
This record is almost completely empty

::NR
::TI
H.E.L.P.
::AU
Other, Ann
::PY
2003


The objective:

I want to read each line of the file in a line at a time and load the
contents of each line into a specific part of an array, leaving dummy
references where fields are omitted in the input.

For instance, the array might be called @record and should look something
like this -

$record[0] contains the tag "::TI"
$record[1] contains the data "A study in archeology"
$record[2] contains the tag "::AU"
$record[3] contains the data "Smith, J."
$record[4] contains the tag "::PY"
$record[5] contains the data "2002"

The array is processed and emptied at the end of a series of other functions
and the next one is loaded.  Note that the fields are mispopulated.

$record[0] contains the tag "::TI"
$record[1] contains the data "Forensic study for dummies"
$record[2] contains the tag "::PY"
$record[3] contains the data "2003"
$record[4] contains nothing.
$record[5] contains nothing.

If I do the following (Note - This code is incomplete):

while ($line = <Inp_File>) {
    if ($line ne "\:\:NR\n") {
        push(@record,$line);
    }

This reproduces the desired outcome in part.  The problem is that when
record 2 is encountered, the ::AU tag is missing and so the array is
incorrectly populated.  Similarly, record 3 is missing the "::PY" tag, but
not the "::AU" and record 4 is missing nearly all of the fields.

The tag "::NR" New Record is a constant and signifies the beginning of a
record.

I tried scanning the lines as they were read in, but came across a stumbling
block.  If I look for a particular tag and assign it to its correct position
in the array, how do I tell the program to assign the next line of input,
from the file, to the corresponding array position.

Any ideas that you have will be well received and code snippets with
explanations would be really welcome.  I'm banging my head on this and the
answer is probably staring me in the face.

Many thanks in advance.



Ivan D.





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

Date: Tue, 27 May 2003 18:15:16 +0000
From: Garry Short <g4rry_short@zw4llet.com>
Subject: Re: Reading file segments into an array
Message-Id: <bb06ka$ot3$1$830fa79d@news.demon.co.uk>

Ivan Dragomilov wrote:

> Hi all,
> 
> I am extremely new to PERL and need some pointers to achieve the result I
> am looking for.
> 
> I have a file that looks like this:
> 
> ::NR
> ::TI
> A study in archeology
> ::AU
> Smith, J.
> ::PY
> 2002
> 
> ::NR
> ::TI
> Forensic study for dummies
> ::PY
> 2003
> 
> ::NR
> ::TI
> I am what I am
> ::AU
> Dullard, A.
> 
> ::NR
> ::TI
> This record is almost completely empty
> 
> ::NR
> ::TI
> H.E.L.P.
> ::AU
> Other, Ann
> ::PY
> 2003
> 
> 
> The objective:
> 
> I want to read each line of the file in a line at a time and load the
> contents of each line into a specific part of an array, leaving dummy
> references where fields are omitted in the input.
> 
> For instance, the array might be called @record and should look something
> like this -
> 
> $record[0] contains the tag "::TI"
> $record[1] contains the data "A study in archeology"
> $record[2] contains the tag "::AU"
> $record[3] contains the data "Smith, J."
> $record[4] contains the tag "::PY"
> $record[5] contains the data "2002"
> 
> The array is processed and emptied at the end of a series of other
> functions
> and the next one is loaded.  Note that the fields are mispopulated.
> 
> $record[0] contains the tag "::TI"
> $record[1] contains the data "Forensic study for dummies"
> $record[2] contains the tag "::PY"
> $record[3] contains the data "2003"
> $record[4] contains nothing.
> $record[5] contains nothing.
> 
> If I do the following (Note - This code is incomplete):
> 
> while ($line = <Inp_File>) {
>     if ($line ne "\:\:NR\n") {
>         push(@record,$line);
>     }
> 
> This reproduces the desired outcome in part.  The problem is that when
> record 2 is encountered, the ::AU tag is missing and so the array is
> incorrectly populated.  Similarly, record 3 is missing the "::PY" tag, but
> not the "::AU" and record 4 is missing nearly all of the fields.
> 
> The tag "::NR" New Record is a constant and signifies the beginning of a
> record.
> 
> I tried scanning the lines as they were read in, but came across a
> stumbling
> block.  If I look for a particular tag and assign it to its correct
> position in the array, how do I tell the program to assign the next line
> of input, from the file, to the corresponding array position.
> 
> Any ideas that you have will be well received and code snippets with
> explanations would be really welcome.  I'm banging my head on this and the
> answer is probably staring me in the face.
> 
> Many thanks in advance.
> 
> 
> 
> Ivan D.

You're either going to end up with an array of arrays, or an array of hashes
for this.

You could do something like:

my %stuff;      # Declare the hash
open FILE, $file or die "Can't open $file: $!\n";
chomp(my @file = <FILE>);       # read file into memory
close FILE;

while (<FILE>) {
  shift;
  my $ti = "";          # Declare and blank variables
  my $au = "";
  my $py = "";
  if ($_ eq "::NR") {   # We've found a new record ..
    shift;              # .. so get the next line
    if ($_ eq "::TI") { # we've found a title marker ..
      $ti = shift;      # So assign the next line to $ti
      shift;            # Get the next line to work on
    }
    if ($_ eq "::AU") { # We've found an author marker ..
      $au = shift;      # .. so assign author to $au
      shift;            # Get the next line to work on
    }
    if ($_ eq "::PY") { # We've found a Published Year marker ..
      $py = shift;      # .. so assign the PY to $py
      shift;
    }
    if ($_ eq "") {     # we've found the end of record
      %stuff = (                # Create the hash for this record
        Title => $ti,
        Author => $au,
        Published => $py
      );
      unshift(@books, \%stuff); # Put the reference to the hash
    }                           # .. onto the books array
  }
}



The only catch with this is if you have two books with the same title, one
record will overwrite the other. BTW, this is untested code, so don't
expect it to work as is.

Regards,

Garry




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

Date: Tue, 27 May 2003 17:21:31 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Reading file segments into an array
Message-Id: <3ED39E7A.AEDA87B2@acm.org>

Ivan Dragomilov wrote:
> 
> I am extremely new to PERL and need some pointers to achieve the result I am
> looking for.
> 
> I have a file that looks like this:
> 
> [snip]
> 
> The objective:
> 
> I want to read each line of the file in a line at a time and load the
> contents of each line into a specific part of an array, leaving dummy
> references where fields are omitted in the input.
> 
> For instance, the array might be called @record and should look something
> like this -
> 
> $record[0] contains the tag "::TI"
> $record[1] contains the data "A study in archeology"
> $record[2] contains the tag "::AU"
> $record[3] contains the data "Smith, J."
> $record[4] contains the tag "::PY"
> $record[5] contains the data "2002"
> 
> The array is processed and emptied at the end of a series of other functions
> and the next one is loaded.  Note that the fields are mispopulated.
> 
> $record[0] contains the tag "::TI"
> $record[1] contains the data "Forensic study for dummies"
> $record[2] contains the tag "::PY"
> $record[3] contains the data "2003"
> $record[4] contains nothing.
> $record[5] contains nothing.
> 
> If I do the following (Note - This code is incomplete):
> 
> while ($line = <Inp_File>) {
>     if ($line ne "\:\:NR\n") {
>         push(@record,$line);
>     }
> 
> This reproduces the desired outcome in part.  The problem is that when
> record 2 is encountered, the ::AU tag is missing and so the array is
> incorrectly populated.  Similarly, record 3 is missing the "::PY" tag, but
> not the "::AU" and record 4 is missing nearly all of the fields.
> 
> The tag "::NR" New Record is a constant and signifies the beginning of a
> record.
> 
> I tried scanning the lines as they were read in, but came across a stumbling
> block.  If I look for a particular tag and assign it to its correct position
> in the array, how do I tell the program to assign the next line of input,
> from the file, to the corresponding array position.
> 
> Any ideas that you have will be well received and code snippets with
> explanations would be really welcome.  I'm banging my head on this and the
> answer is probably staring me in the face.

You probably want to use an Array of Hashes:

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

$/ = '';  # paragraph mode
my @data;

while ( <DATA> ) {
    #  skip invalid records
    next unless /\A::NR/;
    #  store current record as a hash
    #  provide defaults for empty fields
    push @data, { ( '::TI' => '', '::AU' => '', '::PY' => '', /^(::(?:TI|AU|PY))\n(.+)/mg ) };
    }

for ( @data ) {
    print "::TI  $_->{'::TI'}\n";
    print "::AU  $_->{'::AU'}\n";
    print "::PY  $_->{'::PY'}\n\n";
    }

__DATA__

::NR
::TI
A study in archeology
::AU
Smith, J.
::PY
2002

::NR
::TI
Forensic study for dummies
::PY
2003

::NR
::TI
I am what I am
::AU
Dullard, A.

::NR
::TI
This record is almost completely empty

::NR
::TI
H.E.L.P.
::AU
Other, Ann
::PY
2003



John
-- 
use Perl;
program
fulfillment


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

Date: 27 May 2003 17:18:30 GMT
From: "Moose" <mooseshoes@gmx.net>
Subject: Reverse Reference Question
Message-Id: <bb06l6$fbr@dispatch.concentric.net>

All:

I'm a Perl beginner and I would like to create a structure that includes a
hash of masterkeywords (eg. %masterkeywords), the keys of which get
referenced by a set of subscriber ID's.  In this way I can keep track of
which subscriber ID's are referencing the same masterkeywords.  I would also
like to be able to do a reverse reference and by submitting a key to the
hash, generate the list of subscriber ID's that are referenced.

Please suggest a Perl technique for accomplishing this.

Thanks in advance.

Moose




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

Date: Tue, 27 May 2003 11:49:51 -0400
From: Jason Parker-Burlingham <jasonp@uq.net.au>
Subject: Re: scope of variable
Message-Id: <87of1obd9c.fsf@freezer.burling>

Malte Ubl <ubl@schaffhausen.de> writes:

> fatted wrote:
>> With the strict pragma, the filehandle OUT is not available in the
>> while block
> The strict pragma does not change the scope of a variable.

Right.  To elaborate a bit:  Variable scoping rules don't apply to
filehandles.  This code works:

	#!/usr/bin/perl -w
	
	use strict;
	
	# open a new scope for the FOO filehandle
	{  
	        open FOO, ">foo-$$"
	                or die "Can't open: $!";
	}
	
	# and yet it's available outside that scope!
	        print FOO "My pid is $$\n";
	        close FOO
	                or die "Can't close: $!";

But if we use the newer "open my $foo ..." syntax then the filehandle
(now really a lexically-scoped GLOB reference) must follow the
variable scoping rules enforced by "use strict".
-- 
``Oooh!  A gingerbread house!  Hansel and Gretel are set for life!''


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

Date: Tue, 27 May 2003 16:24:34 GMT
From: Darren Dunham <ddunham@redwood.taos.com>
Subject: Re: Sorting hash
Message-Id: <6jMAa.689$VL1.67765358@newssvr13.news.prodigy.com>

Tassilo v. Parseval <tassilo.parseval@rwth-aachen.de> wrote:

>> This is about what happens if two values would be identical. Basically 
>> I don't care in _which_ order the corresponding keys are stored in the 
>> array, but the sort will be done many times, and I want the keys to be 
>> stored in _the same_ order each time as long as the hash isn't changed.
>> 
>> My question is: Can I trust that the keys will appear in @sortedkeys 
>> in _the same_ order, even if there would be occurrences of identical 
>> hash values, or do I need to add another sort criterion to be sure?

> If I understand you correctly, you want a stable sort-algorithm. With
> Perl5.8.0 you can explicitely say

>     use sort 'stable';  # currently the same as '_mergesort'

Hmm.  While a "stable" sort is sufficient for the original question,
it's not required.

I believe the OP doesn't mind if the order of identical keys changes
from the keys list to the sort output, only that the order remain
identical if repeated.  I'm pretty certain that even the quicksort
algorithm will do that.

Of course I'm not sure I understand why a sort on the same elements
would be repeated many times instead of simply stored.

-- 
Darren Dunham                                           ddunham@taos.com
Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >


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

Date: Tue, 27 May 2003 16:44:15 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Sorting hash
Message-Id: <x7y90snxup.fsf@mail.sysarch.com>

>>>>> "DD" == Darren Dunham <ddunham@redwood.taos.com> writes:

  >> use sort 'stable';  # currently the same as '_mergesort'

  DD> Hmm.  While a "stable" sort is sufficient for the original question,
  DD> it's not required.

stable is an attribute of sort algorithms and not a sort method. it just
means that elements with identical keys retain their same relative order
after the sort is complete. sort algortihms swaps elements in ways that
are either stable or not. the older quicksort algorithms used by perl
were unstable (and crashable in when the sort callback gave inconsistant
results). the new (and i think default) sort in 5.8 is always stable and
it is documented to be so which means it can be used to major
advantages. you can always convert an unstable sort to a stable one by
adding a index value key to each element and making it the last sort
field. so when the normal keys are equal, the index key will keep the
elements in correct relative order.

  DD> Of course I'm not sure I understand why a sort on the same elements
  DD> would be repeated many times instead of simply stored.

because quality sorts run at O( N * Log N ) speeds.  some pairs of keys
will be compared more then one time in the subsorts. simply storing the
sorted values in the 'right' place is not possible as that would be a O(
N ) algorithm. 

read this paper for much more on perl sorting

	http://www.sysarch.com/perl/sort_paper.html

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Tue, 27 May 2003 17:22:58 GMT
From: Darren Dunham <ddunham@redwood.taos.com>
Subject: Re: Sorting hash
Message-Id: <S9NAa.740$Sb2.69363835@newssvr13.news.prodigy.com>

Uri Guttman <uri@stemsystems.com> wrote:
>>>>>> "DD" == Darren Dunham <ddunham@redwood.taos.com> writes:

>   >> use sort 'stable';  # currently the same as '_mergesort'

>   DD> Hmm.  While a "stable" sort is sufficient for the original question,
>   DD> it's not required.

> stable is an attribute of sort algorithms and not a sort method. it just
> means that elements with identical keys retain their same relative order
> after the sort is complete. sort algortihms swaps elements in ways that
> are either stable or not.

Certainly.

> results). the new (and i think default) sort in 5.8 is always stable and
> it is documented to be so which means it can be used to major
> advantages. you can always convert an unstable sort to a stable one by
> adding a index value key to each element and making it the last sort
> field. so when the normal keys are equal, the index key will keep the
> elements in correct relative order.

Sure.  I was just pointing out that the OPs post did not seem to
specifically require a stable sort, so that if 5.8 just wasn't
available, he might not be out of luck.

I certainly wasn't trying to imply that it shouldn't be used if
available.

>   DD> Of course I'm not sure I understand why a sort on the same elements
>   DD> would be repeated many times instead of simply stored.

> because quality sorts run at O( N * Log N ) speeds.  some pairs of keys
> will be compared more then one time in the subsorts. simply storing the
> sorted values in the 'right' place is not possible as that would be a O(
> N ) algorithm. 

I meant that the OP shouldn't be sorting the list multiple times, but
should be storing the results...

my @sorted = sort {however} keys %hash;

And then operate on @sorted instead of regenerating it multiple times
from the same hash.

I read the post as saying that a specific requirement was based on the
fact that %hash did not change.  If it didn't change, then the sort
didn't need to be rerun, and his concern about changing key order
appears to vanish.

-- 
Darren Dunham                                           ddunham@taos.com
Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >


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

Date: 27 May 2003 10:34:00 -0700
From: jan_buys@hotmail.com (Jan)
Subject: STDOUT print not showing up
Message-Id: <11971c2c.0305270934.1d337c62@posting.google.com>

Hello,

Feeling a bit newbie here for asking such stupid question.

I have a problem when printing to a default STDOUT command-line
window.  What I basically do is print a bunch of info to STDOUT and in
the end I ask a question to the user of the script.  (I use the normal
print command and some HERE documents for that).

Problem is : the text is not showing up in the console (normal cmd.exe
console) and the script just waits for an answer to the question
(which, of course, is not visible).  Only when I answer it, all text
output that should have been printed prior to waiting for answer is
printed.  Not quite userfriendly, unless you're clairvoyant, which
most common users are not.

I've been looking at the code for hours and at exactly similar scripts
I made in the past, but I cannot find the problem.  Does this ring a
bell somewhere ?

Thanks for any answer you might come up with,
Jan

info : I'm using Activeperl 5.6.x for w32 and CQperl (5.0.x something)
for these scripts.  OS is W2k Pro


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

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


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