[30986] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2231 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Feb 24 03:09:45 2009

Date: Tue, 24 Feb 2009 00:09:09 -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, 24 Feb 2009     Volume: 11 Number: 2231

Today's topics:
        debugging on stdout <no_th@nks.org>
    Re: debugging on stdout <tim@burlyhost.com>
        new CPAN modules on Tue Feb 24 2009 (Randal Schwartz)
    Re: Once again: Rolling Frame! sln@netherlands.com
    Re: Once again: Rolling Frame! <mstep@podiuminternational.org>
    Re: pseudo hash syntax in 5.10 <ben@morrow.me.uk>
    Re: pseudo hash syntax in 5.10 <perl@marc-s.de>
    Re: pseudo hash syntax in 5.10 <hjp-usenet2@hjp.at>
    Re: pseudo hash syntax in 5.10 <tadmc@seesig.invalid>
        struct module : getting at individual bits inside byte  <harijay@gmail.com>
    Re: struct module : getting at individual bits inside b <perl@marc-s.de>
    Re: struct module : getting at individual bits inside b <harijay@gmail.com>
        Trying to convert small C++ subroutine (by Peter Weinbe <usenet@DavidFilmer.com>
    Re: utf8 and chomp <hjp-usenet2@hjp.at>
    Re: utf8 and chomp <whynot@pozharski.name>
    Re: utf8 and chomp <perl@marc-s.de>
    Re: Where is the standard output coming from in the fol <tim@burlyhost.com>
    Re: Where is the standard output coming from in the fol <tim@burlyhost.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 24 Feb 2009 03:51:33 GMT
From: mike <no_th@nks.org>
Subject: debugging on stdout
Message-Id: <m3hc2kebln.fsf@localhost.localdomain>


I have a perl script which talks to mplayer and stepping through it in
emacs' cperl-db it executes flawlessly; but running "./script video" it
does everything except one crucial step and I'm having a hard time
visualizing what could be the matter.

so is there a way to invoke the perl script such as can be done in bash or 
csh (csh -xvf ./script.csh) which would allow me to see what's going on at
full speed?

thanks.

-- 


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

Date: Mon, 23 Feb 2009 22:48:41 -0800
From: Tim Greer <tim@burlyhost.com>
Subject: Re: debugging on stdout
Message-Id: <dLMol.70349$qt3.16606@newsfe10.iad>

mike wrote:

> 
> I have a perl script which talks to mplayer and stepping through it in
> emacs' cperl-db it executes flawlessly; but running "./script video"
> it does everything except one crucial step and I'm having a hard time
> visualizing what could be the matter.
> 
> so is there a way to invoke the perl script such as can be done in
> bash or csh (csh -xvf ./script.csh) which would allow me to see what's
> going on at full speed?
> 
> thanks.
> 

You're going to need to post the relevant code, and provide some details
about the issue, instead of just posting an example of how you run it. 
Also, what is the crucial step it's missing?  What are you trying to
view/see what's "going on" (what is that?)
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

Date: Tue, 24 Feb 2009 05:42:27 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Tue Feb 24 2009
Message-Id: <KFK2Ir.BI2@zorch.sf-bay.org>

The following modules have recently been added to or updated in the
Comprehensive Perl Archive Network (CPAN).  You can install them using the
instructions in the 'perlmodinstall' page included with your Perl
distribution.

App-CPAN-Mini-Visit-0.004
http://search.cpan.org/~dagolden/App-CPAN-Mini-Visit-0.004/
explore each distribution in a minicpan repository 
----
App-CPAN-Mini-Visit-0.005
http://search.cpan.org/~dagolden/App-CPAN-Mini-Visit-0.005/
explore each distribution in a minicpan repository 
----
Business-OnlinePayment-PaymenTech-1.1.0
http://search.cpan.org/~gphat/Business-OnlinePayment-PaymenTech-1.1.0/
PaymenTech backend for Business::OnlinePayment 
----
DBIx-Class-QueryLog-1.1.4
http://search.cpan.org/~gphat/DBIx-Class-QueryLog-1.1.4/
Log queries for later analysis. 
----
Devel-Autoflush-0.05
http://search.cpan.org/~dagolden/Devel-Autoflush-0.05/
Set autoflush from the command line 
----
FFmpeg-Command-0.09
http://search.cpan.org/~mizzy/FFmpeg-Command-0.09/
A wrapper class for ffmpeg command line utility. 
----
File-SearchPath-0.05
http://search.cpan.org/~tjenness/File-SearchPath-0.05/
Search for a file in an environment variable path 
----
Getopt-GUI-Long-0.91
http://search.cpan.org/~hardaker/Getopt-GUI-Long-0.91/
----
IO-Socket-SSL-1.23
http://search.cpan.org/~sullr/IO-Socket-SSL-1.23/
Nearly transparent SSL encapsulation for IO::Socket::INET. 
----
Image-Size-3.2
http://search.cpan.org/~rjray/Image-Size-3.2/
read the dimensions of an image in several popular formats 
----
JSON-2.14
http://search.cpan.org/~makamaka/JSON-2.14/
JSON (JavaScript Object Notation) encoder/decoder 
----
MooseX-Declare-0.05
http://search.cpan.org/~flora/MooseX-Declare-0.05/
Declarative syntax for Moose 
----
MooseX-Dumper-0.01
http://search.cpan.org/~fayland/MooseX-Dumper-0.01/
Dumper with roles 
----
MooseX-Dumper-0.02
http://search.cpan.org/~fayland/MooseX-Dumper-0.02/
Dumper with roles 
----
MooseX-Method-Signatures-0.09
http://search.cpan.org/~flora/MooseX-Method-Signatures-0.09/
Method declarations with type constraints and no source filter 
----
Net-Interface-1.002
http://search.cpan.org/~miker/Net-Interface-1.002/
Perl extension to access network interfaces 
----
Net-SMTP-Pipelining-v0.0.3
http://search.cpan.org/~marcb/Net-SMTP-Pipelining-v0.0.3/
Send email using ESMTP PIPELINING extension 
----
Normalize-0.3
http://search.cpan.org/~kakadu/Normalize-0.3/
normalize scores between 0 and 1. 
----
OpenGuides-0.64
http://search.cpan.org/~dom/OpenGuides-0.64/
A complete web application for managing a collaboratively-written guide to a city or town. 
----
POE-Declare-0.05
http://search.cpan.org/~adamk/POE-Declare-0.05/
A POE abstraction layer for conciseness and simplicity 
----
POE-Session-MessageBased-0.10
http://search.cpan.org/~rcaputo/POE-Session-MessageBased-0.10/
a message-based (not @_ based) POE::Session 
----
Path-Resource-0.072
http://search.cpan.org/~rkrimen/Path-Resource-0.072/
URI/Path::Class combination 
----
Perl-Dist-WiX-v0.13_04
http://search.cpan.org/~csjewell/Perl-Dist-WiX-v0.13_04/
Experimental 4th generation Win32 Perl distribution builder 
----
SOAP-WSDL-2.00.09
http://search.cpan.org/~mkutter/SOAP-WSDL-2.00.09/
SOAP with WSDL support 
----
Sendmail-PMilter-0.97
http://search.cpan.org/~avar/Sendmail-PMilter-0.97/
Perl binding of Sendmail Milter protocol 
----
Shipwright-2.1.3
http://search.cpan.org/~sunnavy/Shipwright-2.1.3/
Best Practical Builder 
----
Shipwright-2.1.4
http://search.cpan.org/~jesse/Shipwright-2.1.4/
Best Practical Builder 
----
Simo-Error-0.0202
http://search.cpan.org/~kimoto/Simo-Error-0.0202/
Error object for Simo 
----
Simo-Error-0.0203
http://search.cpan.org/~kimoto/Simo-Error-0.0203/
Error object for Simo 
----
Simo-Util-0.0202
http://search.cpan.org/~kimoto/Simo-Util-0.0202/
Utility Class for Simo 
----
Task-Pluggable-0.01
http://search.cpan.org/~haosan/Task-Pluggable-0.01/
The great new Task::Pluggable! 
----
Task-Pluggable-0.02
http://search.cpan.org/~haosan/Task-Pluggable-0.02/
Pluggable task module 
----
Test-LeakTrace-0.03
http://search.cpan.org/~gfuji/Test-LeakTrace-0.03/
Traces memory leaks (EXPERIMENTAL) 
----
Text-Sprintf-Named-0.0300
http://search.cpan.org/~shlomif/Text-Sprintf-Named-0.0300/
sprintf-like function with named conversions 
----
Text-Trac-0.14
http://search.cpan.org/~mizzy/Text-Trac-0.14/
Perl extension for formatting text with Trac Wiki Style. 
----
WWW-Search-Ebay-3.008
http://search.cpan.org/~mthurn/WWW-Search-Ebay-3.008/
backend for searching www.ebay.com 
----
XML-Compile-SOAP-2.02
http://search.cpan.org/~markov/XML-Compile-SOAP-2.02/
base-class for SOAP implementations 
----
XML-Compile-SOAP-Daemon-2.01
http://search.cpan.org/~markov/XML-Compile-SOAP-Daemon-2.01/
SOAP accepting server 
----
YUI-Loader-0.07
http://search.cpan.org/~rkrimen/YUI-Loader-0.07/
Load (and cache) the Yahoo JavaScript YUI framework 
----
Yahoo-Marketing-APT-1.03
http://search.cpan.org/~shenj/Yahoo-Marketing-APT-1.03/
an interface for Yahoo! Search Marketing's APT Web Services. 
----
ZConf-Bookmarks-0.1.0
http://search.cpan.org/~vvelox/ZConf-Bookmarks-0.1.0/
ZConf backed bookmark storage system. 
----
ZConf-Mail-0.2.0
http://search.cpan.org/~vvelox/ZConf-Mail-0.2.0/
Misc mail client functions backed by ZConf. 
----
Zymurgy-0.02
http://search.cpan.org/~rogerhall/Zymurgy-0.02/
The study and use of fermentations. 
----
Zymurgy-0.03
http://search.cpan.org/~rogerhall/Zymurgy-0.03/
The study and use of fermentations. 
----
jQuery-Loader-0.03
http://search.cpan.org/~rkrimen/jQuery-Loader-0.03/
Load (and cache) the jQuery JavaScript library 
----
mysubs-1.10
http://search.cpan.org/~chocolate/mysubs-1.10/
lexical subroutines 


If you're an author of one of these modules, please submit a detailed
announcement to comp.lang.perl.announce, and we'll pass it along.

This message was generated by a Perl program described in my Linux
Magazine column, which can be found on-line (along with more than
200 other freely available past column articles) at
  http://www.stonehenge.com/merlyn/LinuxMag/col82.html

print "Just another Perl hacker," # the original

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


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

Date: Tue, 24 Feb 2009 01:24:15 GMT
From: sln@netherlands.com
Subject: Re: Once again: Rolling Frame!
Message-Id: <vli6q4ljnok30rd1nnaassm9d74c7j3764@4ax.com>

On Sun, 22 Feb 2009 18:34:43 GMT, sln@netherlands.com wrote:

>On Sun, 22 Feb 2009 07:02:48 -0800 (PST), Marek <mstep@podiuminternational.org> wrote:
>
>>Hello all!
>>
>>
>>Once again thank you for all for your help with my "rolling frame" in
>>the thread "restrict a hash to 15 pairs and iterate over it" some
>>weeks ago! I learned a lot of all your suggestions!
>>
>>I need once more your help in creating a multi-level hash like
>>follows:
>>
>>We read in the line numbers, we count the lines ***and*** I need to
>>know, how many times these numbers occur in a frame of five lines ...
>>
>>So my hash should have these informations in pseudo Perl code:
>>
>>					number @(line, line, line) => how many times (each number)
>>
>>How to transform the genius code of the suggestion from Tad J
>>McClellan that the hash creation
>>
>>                                        $nums{$_}++ for values
>>%lines;
>>
>>contains meantime the line numbers?
>>
>>here the example code and best greetings to all
>>
>>marek
>>
[snip]

Your welcome. Since I save code fragments in a local folder,
thought this will give you more bang for the buck since you 
don't have to generate new %nums hash each time the frame advances.
This reduces overhead quite a bit.

Good luck!
-sln
-----------------------------------------------
## frame2.pl
##
use warnings;
use strict;
use Data::Dumper;

my $size = 5;  # 5 instead of 15 (for test).
my $line = 0;  # "line" counter.
my %Lines;     # Line buffer, up to $size lines:  line => number.
my %Nums;      # Number buffer, less than or equal to $size:  number => [line,line,line]

my ($nbr,$nbr_count,$aref,$off_frame_nbr);

while ( <DATA> )
{
	# Get digits
	/^\s*(\d+)\s*$/;
	next if (!defined $1);

	$nbr = $1;

	$line++;
	$Lines{ $line} = $nbr;
	unshift @{$Nums{ $nbr}}, $line;  # prepend line to array

	if ( keys %Lines == $size )
	{
		# print Dumper \%Lines;  # for debugging
		# print Dumper \%Nums;   # for debugging
		print "\nFrame ".($line-$size+1)."-$line\n";

		# Display/check whats in the Numbs buffer
		foreach $nbr ( sort { $a <=> $b } keys %Nums )
		{
			$aref = $Nums{ $nbr};
			$nbr_count = @$aref;
			# .... here can check if $nbr_count is excessive
			# ....
			printf "%3d: %3d times at lines (%s)\n", $nbr, $nbr_count, join(',', reverse @$aref);
		}
		print "---------\n";

		# Deplete line going out of frame
		$off_frame_nbr = $Lines{ $line - $size + 1};
		pop @{$Nums{ $off_frame_nbr}};
		delete $Nums{ $off_frame_nbr} if (!@{$Nums{ $off_frame_nbr}});

		# Maintain buffer size
		delete $Lines{ $line - $size + 1};
	}
}

__DATA__
01.01.98
7
31
33
14
7
7
35
16
20
20
13
55
1
1
7
7
9
20
21
20
7
20
0
0
0



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

Date: Mon, 23 Feb 2009 22:30:42 -0800 (PST)
From: Marek <mstep@podiuminternational.org>
Subject: Re: Once again: Rolling Frame!
Message-Id: <c72ffe3d-fdb0-4adc-a057-4fb8d70aac1e@v19g2000yqn.googlegroups.com>

On Feb 24, 2:24=A0am, s...@netherlands.com wrote:

> snip

Wow! This is great art! I am admiring such code, like a painting of
Ilya Repin or ( because you are from Netherlands :) Salomon van
Ruysdael. I will need weeks to fully understand, what is exactly going
on in your solution.

I know, the codex of honour in this group is to suggest first an own
attempt of code, before asking for help. But: Dear Master sln! I dare
not to ask a supplemental question. If you have no time, please simply
don't answer. I don't want to abuse your witness and ***art***. But
certainly I have a next big problem: How to work with these double
numbers, once they occur? If there appears a double number, for
example the 7 in line 5, I would like to see, whether it appears for a
third time in a frame of five steps later.

Thank you again


marek


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

Date: Mon, 23 Feb 2009 21:03:49 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: pseudo hash syntax in 5.10
Message-Id: <lifb76-mr1.ln1@osiris.mauzo.dyndns.org>


Quoth Patrick Viet <patrick.viet___GRR___@gmail.com.nospam>:
> 
> Here is my full test :
> 
> --------------------
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> 
> my $h = {
>    a => 'test1',
>    b => 'test2',
>    c => 'test3',
> };
> 
> my @list = qw(a b);
> 
> my ($var1,$var2) = @{%$h}{@list};
> 
> print "var1 : $var1 $var2\n";
> --------------------
> 
> $ ./test
> Can't use string ("3/8") as a HASH ref while "strict refs" in use at 
> ./test line 14.
> 
> And if I take out the % on the my var1,var2 line, I get what I expected:
> 
> $ ./test
> var1 : test1 test2
> 
> I've solved my problem in the sense that my software can work again, but 
> I don't understand what that message is, why it stopped working in 5.10, 
> and where that 3/8 crap comes from (?!)

It used to be possible to treat a hash as a reference to itself; that
is, this code

    use strict;

    my %h = ( a => 1 );
    print for %{ %h };

would print 'a1'. This was due to a bug in the reference-handling code.
As of 5.10, this bug has been fixed, so the expression %{ %h } evaluates
%h in scalar context (returning a string like "1/8") and then tries to
treat that as a symbolic reference.

Now, the expression you had was

    @{ %$h }{@list}

which is equivalent to

    my %h2 = %$h;
    @{ %h2 }{@list}

where the @{ }{} construction is the hash-slice form of %{ }: it's
related like this:

    %h      $h{foo}     @h{foo}
    %{ }    ${ }{foo}   @{ }{foo}

Ben



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

Date: Mon, 23 Feb 2009 22:40:07 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: pseudo hash syntax in 5.10
Message-Id: <gnv53m$1fsu$1@ariadne.rz.tu-clausthal.de>

Gunnar Hjalmarsson schrieb:
> Tim McDaniel wrote:
>> But, as Marc noted, using a real hash variableis better in this case,
>> avoiding the extra $ in lots of cases.
> 
> I'm not sure that Marc noted that. Besides, considering that the OP's 
> real code is:
> 
>     while(my $href = $sth->fetchrow_hashref) {
>       my $key = join('|',@{$href}{@cols_primary});
> 

I was in fact talking about his other post where he wrote:

 > Other script that produces the same result without hash ref :
 > use warnings;
 > use strict;
 >
 > my %h = (
 >   a => 'test1',
 >   b => 'test2',
 >   c => 'test3',
 > );
 >
 > my @list = qw(a b);
 >
 > my ($var1,$var2) = @{\%h}{@list};
 >
 > print "var1 : $var1 $var2\n";

Marc "Maluku" Lucksch



=begin OffTopic

And about this code:
 >     while(my $href = $sth->fetchrow_hashref) {
 >       my $key = join('|',@{$href}{@cols_primary});

I think the OP wants to build kind of a primary key out of some combined 
values of the row. (Just a wild guess)
Any database I know can use combined columns as keys (also primary keys) 
so I think that this code is quite useless with a good database design.

=end OffTopic

=cut


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

Date: Tue, 24 Feb 2009 00:49:55 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: pseudo hash syntax in 5.10
Message-Id: <slrngq6dh3.6q9.hjp-usenet2@hrunkner.hjp.at>

On 2009-02-23 20:48, Tim McDaniel <tmcd@panix.com> wrote:
> In article <gnv0c9$3o9$1@ss408.t-com.hr>, Matija Papec  <none@none.com> wrote:
>>Patrick Viet wrote:
>>
>>> my ($var1,$var2) = @{\%h}{@list};
>>
>>less crappy version:
>>my ($var1,$var2) = @h{@list};
>
> Please test your code before making suggestions.  With the original
>     my $h = {
>        a => 'test1',
>        b => 'test2',
>        c => 'test3',
>     };

That isn't the original in this case. It's:

      my %h = (
         a => 'test1',
         b => 'test2',
         c => 'test3',
      );

> As Marc (silently) noted, you have to change the assignment from
> $h = {...} to %h = (...).
>

Patrick already did this in the posting Matija replied to.

	hp


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

Date: Mon, 23 Feb 2009 17:53:21 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: pseudo hash syntax in 5.10
Message-Id: <slrngq6dnh.k23.tadmc@tadmc30.sbcglobal.net>

Tim McDaniel <tmcd@panix.com> wrote:

> If EXPR is indeed an expression, like %$h, it is evaluated, so far as
> I can tell, in a scalar context.  As Marc noted, that gives you a
> string that tells you information about the hash table (here, that
> there are 3 elements and 8 buckets, I think, but I can't find it
> quickly in the man pages).


It is in the "Scalar values" section of perldata.pod:

    If you evaluate a hash in scalar context, it returns false if the
    hash is empty.  If there are any key/value pairs, it returns true;
    more precisely, the value returned is a string consisting of the
    number of used buckets and the number of allocated buckets, separated
    by a slash.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Mon, 23 Feb 2009 15:12:54 -0800 (PST)
From: harijay <harijay@gmail.com>
Subject: struct module : getting at individual bits inside byte field
Message-Id: <d8458609-bdc9-4b9c-b473-acb88cdcb008@n20g2000vba.googlegroups.com>

In my last post I had asked about reading data from a binary file
using the struct module.
Thanks to some excellent help , I have managed to read in successfully
most of the header . These are some time-voltage traces from a digital
to analog converter for my experiments output according to a format
mentioned here : ( http://www.dataq.com/support/techinfo/ff.htm)

I have a question about how to bitmask a bunch of bytes read in from
such a binary formatted file .

For eg the spec says the first two bytes have different parameters in
different bits .
Byte 1	                                                Byte 0
SN16 SD9	SD8	SD7	SD6	SD5	SD4	SD3	SD2	SD1	SD0	T4	T3	T2	T1	T0

I am reading in the two bytes using the following code
import struct
f.seek(0)
element1_format = struct.Struct("<H")
(element1,) = element1_format.unpack(f.read(2))

Now element1 has type "str" . How do I apply a bitmask to this to get
at information in the component bits .

Since the entire file format has many such bitmasked fields and since
this is my first venture into binary formats and c-type structs , I
wanted to know how to read values inside a byte using python.

My few tries at using bitwise operators ( element1 & 0x001f) are
frustrated by messages that say " unsupported operand type(s) for &:
'str' and 'int' " .

How can I keep my string objects as bits and apply bitmasks to them

Any help in this will be greatly appreciated.
Thanks
hari


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

Date: Tue, 24 Feb 2009 01:07:05 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: struct module : getting at individual bits inside byte field
Message-Id: <gnvdn8$1kpr$1@ariadne.rz.tu-clausthal.de>

harijay schrieb:
> import struct
> Now element1 has type "str" . 
> ..using python.
> " unsupported operand type(s) for &: 'str' and 'int' " .

I'm going out on a limb here and guess this ain't really perl related, 
so it's the wrong newsgroup.


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

Date: Mon, 23 Feb 2009 16:16:17 -0800 (PST)
From: harijay <harijay@gmail.com>
Subject: Re: struct module : getting at individual bits inside byte field
Message-Id: <d98e157b-3113-4b8a-b1e3-293953a8a2b2@t11g2000yqg.googlegroups.com>

On Feb 23, 7:07=A0pm, Marc Lucksch <p...@marc-s.de> wrote:
> harijay schrieb:
>
> > import struct
> > Now element1 has type "str" .
> > ..using python.
> > " unsupported operand type(s) for &: 'str' and 'int' " .
>
> I'm going out on a limb here and guess this ain't really perl related,
> so it's the wrong newsgroup.

Whoops ..wrong newsgroup
Sorry perl folks.




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

Date: Mon, 23 Feb 2009 22:36:22 -0800 (PST)
From: David Filmer <usenet@DavidFilmer.com>
Subject: Trying to convert small C++ subroutine (by Peter Weinberger) to Perl
Message-Id: <b6b31e86-2229-4c4b-83f1-ac2c15fa6fea@l16g2000yqo.googlegroups.com>

Greetings.

In the book, "Programming in C++" by Stephen Dewhurst and Kathy Stark
(1989, Prentice Hall, page 14), I find a short hashing subroutine
("hash" as in cryptographic hash, not an associative array) by the
famous Peter Weinberger.

The subroutine accepts a string, and returns a numerical value. One
possible application of such a subroutine would be for storing (and
retrieving) of a vast number of files (many thousands or millions) -
the files could be stored in an arbitrary number of directories/
filesystems (ie, "hash buckets") and retrieved based on the hash value
of their filename.

I wish to convert this C++ subroutine to a Perl subroutine.  But my
knowledge of C++ is limited to what I learned in a class in college -
I have never coded in C++ and I am severely deficient in this
language.

Based on my limited C++ skills, I have made an effort to do this
conversion, but it does not produce the expected results.  I was
hoping someone here who was more familiar with C++ could point out the
error of my ways.

Here is subroutine from the book, based on Peter Weinberger's code:

int
hashpjw( char *s ) {
   const prime = 211;
   unsigned hash = 0, g;

   for( char *p = s; *p, p++ ) {
      hash = ( hash << 4 ) + *p;
      // assumes 32 bit int size
      if( g = hash & 0xf0000000 ) {
         hash ^= g >> 24;
         hsh ^= g;
      }
   }
   return hash % prime;
}


Here is my <lame>attempt</lame> to convert it to Perl:

#!/usr/bin/perl
use strict;

print hashpjw( 'bar', 123 );

sub hashpjw {
   my( $char, $s ) = @_;
   my $prime = 211;
   my ( $hash, $g, $p );
   for my $char( $p = $s, $p, $p++ ) {
      $hash = ( $hash << 4 ) + $p;
      if( $g = $hash & 0xf0000000 ) {
         $hash = $hash ^ ($g >> 24);
         $hash = $hash ^ $g;
      }
   }
   return $hash % $prime;
}
__END__

Any assistance is greatly appreciated!


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

Date: Tue, 24 Feb 2009 01:03:03 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: utf8 and chomp
Message-Id: <slrngq6e9n.6q9.hjp-usenet2@hrunkner.hjp.at>

On 2009-02-23 17:05, Josef Feit <jfeit@ics.muni.cz> wrote:
> The results are the same: the strings produced
> are different. I will try to force the utf8 etc,
> but it seems strange anyway.
>
> Josef
>
>
> #!/usr/bin/perl
> # ----------------------------
> # echo "náláx" >text.txt
> # thisscript text.txt
> # ----------------------------
> use warnings;
> use strict;
> use encoding 'utf-8';

I already wanted to advice against using "use encoding", because it
behaves rather unintuitively. But I couldn't see what's wrong until you
mentioned that reading from stdin works for you. 

Then it became clear.

From perldoc encoding:

       The encoding pragma also modifies the filehandle layers of STDIN
       and STDOUT to the specified encoding.

If you call your script like

> # thisscript text.txt

it does *not* read from STDIN, so the file will *not* automatically be
decoded from UTF-8. You should either explicitely open the file with the
correct encoding layer, or use "use open".

	hp


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

Date: Tue, 24 Feb 2009 00:52:06 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: utf8 and chomp
Message-Id: <slrngq6a6l.4dc.whynot@orphan.zombinet>

On 2009-02-23, Josef Feit <jfeit@ics.muni.cz> wrote:
> Utf8 and chomp problem:
>
> Thank you for replies.
> I tried to rewrite the script, but the problem seems
> to persist.
> UTF8 displayed OK, so I am sending the improved script.
>
> I tried it on my OpenSuse 11.0 Linux under cs_CZ.UTF-8
> locale and on the server (Debian I think, with
> LANG=en_US.UTF-8 etc. (and v5.8.8 Perl).
>
> The results are the same: the strings produced
> are different. I will try to force the utf8 etc,
> but it seems strange anyway.
>
> Josef
>
>
> #!/usr/bin/perl
> # ----------------------------
> # echo "náláx" >text.txt
> # thisscript text.txt
> # ----------------------------

Snap!  That's the problem -- everyone here are just a way lazy to dump
string into file, and run your script through something like this
instead:

	echo someutf8 | thisscript

I've just gone through your original script with debugger, and found out
that after C<$line = <>;> I<$line> is pure byte string.  And then after
C<chomp $line;> it automagically decodes into utf8 character(!) string.
Should I keep on explaining?  (No, no spoiler this time.)

*CUT*

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


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

Date: Tue, 24 Feb 2009 06:17:07 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: utf8 and chomp
Message-Id: <gnvvsh$1urb$1@ariadne.rz.tu-clausthal.de>

Eric Pozharski schrieb:
> I've just gone through your original script with debugger, and found out
> that after C<$line = <>;> I<$line> is pure byte string.  And then after
> C<chomp $line;> it automagically decodes into utf8 character(!) string.
> Should I keep on explaining?  (No, no spoiler this time.)

Ok now I am confused, do please explain.

Marc "Maluku" Lucksch


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

Date: Mon, 23 Feb 2009 16:12:24 -0800
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Where is the standard output coming from in the following program
Message-Id: <IXGol.23555$pX4.7983@newsfe08.iad>

grocery_stocker wrote:

> open my $out, '|-', 'bbs' or die "cannot open pipe to bbs";
> 
> print $out "find nope a=cdalten\n";

Going by that code, you've sent the command to (and get the output from)
the child process.
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

Date: Mon, 23 Feb 2009 16:14:47 -0800
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Where is the standard output coming from in the following program
Message-Id: <XZGol.23565$pX4.14362@newsfe08.iad>

Tim Greer wrote:

> grocery_stocker wrote:
> 
>> open my $out, '|-', 'bbs' or die "cannot open pipe to bbs";
>> 
>> print $out "find nope a=cdalten\n";
> 
> Going by that code, you've sent the command to (and get the output
> from) the child process.

Pardon, I worded that poorly.  Greg's explanation of the child process
inheriting the output from the parent process is a better way of
wording it (my reply makes it sound like it is inherited by the parent
from the child -- bad wording... oops).
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

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 V11 Issue 2231
***************************************


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