[29816] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1059 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Nov 24 09:09:39 2007

Date: Sat, 24 Nov 2007 06:09:05 -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           Sat, 24 Nov 2007     Volume: 11 Number: 1059

Today's topics:
    Re: A little help on perl hashes.. <nobull67@gmail.com>
    Re: How to get the string Cartesian Products of 2 list <rvtol+news@isolution.nl>
    Re: How to get the string Cartesian Products of 2 list xueweizhong@gmail.com
    Re: How to get the string Cartesian Products of 2 list <bik.mido@tiscalinet.it>
    Re: How to get the string Cartesian Products of 2 list <bik.mido@tiscalinet.it>
    Re: How to get the string Cartesian Products of 2 list <bik.mido@tiscalinet.it>
    Re: How to get the string Cartesian Products of 2 list xueweizhong@gmail.com
    Re: searching for a pattern for multiple matches <rvtol+news@isolution.nl>
    Re: SvUOK always fails on 64bit platform <sisyphus359@gmail.com>
    Re: SvUOK always fails on 64bit platform <rvtol+news@isolution.nl>
        union of 1 and 0 strings <avilella@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 24 Nov 2007 03:31:40 -0800 (PST)
From: Brian McCauley <nobull67@gmail.com>
Subject: Re: A little help on perl hashes..
Message-Id: <ee0dee28-33ca-4283-88bf-520a5170c3f2@s12g2000prg.googlegroups.com>

On Nov 23, 3:31 pm, nolo contendere <simon.c...@fmr.com> wrote:

>
> use strict; use warnings;
> use Data::Dumper;
>
> my %ids;

Good start.

>
> while ( <DATA> ) {
>     next if /^\s*$/;
>     chomp;
>     my ( $userid, $uid, $mid ) = split;
>     $ids{$uid} = { userid    => $userid,
>                    mid       => $mid,
>                    managerid => '' };
>
> }

I tend to follow the rule "always use the most natural representation
of things unless there is a reason to do otherwise". (It's one of my
top-five rules of programming).

In this case the "natural representation" of a hash not (yet) having a
managerid entry would be for it not to have a managerid entry.

>
> my $itr = 0;

You never use $itr

> for my $key ( keys %ids ) {

Since you never use $key except to look up $ids{$key} it would be more
efficient to loop over the values directly.

>     my $managerid;

You are declaring this in the wrong scope.

>     my $mid = $ids{$key}{mid};
>
>     # find userid where uid = this $mid
>     if ( exists $ids{$mid} ) {

There's nothing wrong with using exists() here but it is redundant
since we know that %ids will contain no false values.

>         $managerid = lookup_userid( $mid );

This is where you should have declared $managerid (but since you only
use it once it's maybe not worth it at all).

>         $ids{$key}{managerid} = $managerid;
>     }
>
> }
>
> for my $uid ( keys %ids ) {
>     unless ( $ids{$uid}{managerid} ) { $ids{$uid}{managerid} =
> $ids{$uid}{userid}; };

More simply

$ids{$uid}{managerid} ||= $ids{$uid}{userid};

However it would probably make sense to distinguish in some way
entries with no mid from those with an invalid mid.

>     print "$ids{$uid}{userid}\t$ids{$uid}{managerid}\n";
>
> }
>
> #=========
> # subs
> #=========
>
> sub lookup_userid {
>     my ( $mid ) = @_;
>     return $ids{$mid}{userid};
>
> }

This function is probably too small to be worth it. Particularly given
the global variable.

The reasoning goes like this

Accessors - good

Global variables - bad

More complexity - bad

Nett gain - negative


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

Date: Sat, 24 Nov 2007 13:53:07 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: How to get the string Cartesian Products of 2 list
Message-Id: <fi9ajf.1k0.1@news.isolution.nl>

Dr.Ruud schreef:
> Ted Zlatanov schreef:
> 
>> local $" = ','; # List separator
>> print glob "{@{['a'..'c']}}{@{[1..3]}}\n";
> 
> Command line version:
> 
> perl -wle '
>   $"=",";
>   print for glob "{@{[ q{a}..q{c} ]}}{@{[ 1..3 ]}}"
> '

Others:

perl -le'
  sub c{local $"=","; "{@_}"}
  print for glob c("a".."c").c(1..3)
'

perl -le'
  sub c{"{". join(",", @_) ."}"}
  print for glob c("a".."c").c(1..3)
'

-- 
Affijn, Ruud

"Gewoon is een tijger."


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

Date: Sat, 24 Nov 2007 05:26:24 -0800 (PST)
From: xueweizhong@gmail.com
Subject: Re: How to get the string Cartesian Products of 2 list
Message-Id: <eac7689c-11ab-40c6-9ac6-f6751b5c8270@i12g2000prf.googlegroups.com>

> >> local $" = ','; # List separator
> >> print glob "{@{['a'..'c']}}{@{[1..3]}}\n";

Is this difference below a language feature or bug?

>perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{064}]}}>'
a001,a002,a003,a004,a005,a006,a007,a008,a009,a010,a011,a012,a013,a014,a015,a016,a017,a018,a019,a020,a021,a022,a023,a024,a025,a026,a027,a028,a029,a030,a031,a032,a033,a034,a035,a036,a037,a038,a039,a040,a041,a042,a043,a044,a045,a046,a047,a048,a049,a050,a051,a052,a053,a054,a055,a056,a057,a058,a059,a060,a061,a062,a063,a064

>perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{065}]}}>'
a

-Todd


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

Date: Sat, 24 Nov 2007 14:53:12 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: How to get the string Cartesian Products of 2 list
Message-Id: <9magk3p3mh1qijhs7ef980jgsqqhsvkqgj@4ax.com>

On Fri, 23 Nov 2007 06:30:10 -0800 (PST), xueweizhong@gmail.com wrote:

>perl glob in perl5.8 is not power enough than the bash3.0 glob, it
>doesn't support .. operator which is fatal to this issue:

I didn't know that bash 3.* did that. In any case, that's good.

In perl, you can currently use a workaround:

>#!/bin/perl
>
>sub X {split / /,qx/bash -c "echo @_"/}
>
>print "glob:\n";
>print join "\n", glob "{a..b}{1..2}";

local ($",$,)=(",", "\n");
print glob "{@{['a'..'b']}}{@{[1..2]}}"

Of course, someone may implement a bash30_glob() to add to File::Glob.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Sat, 24 Nov 2007 14:55:52 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: How to get the string Cartesian Products of 2 list
Message-Id: <u7bgk3p6nqshhr4ftrv5qt3m64d3l920dn@4ax.com>

On Fri, 23 Nov 2007 07:59:26 -0800 (PST), xueweizhong@gmail.com wrote:

>> {
>>     local $" = ','; # List separator
>>
>>     my @letters = 'a'..'c';
>>     my @numbers = 1..3;
>>
>>     my @globbed = glob "{@letters}{@numbers}";
>>
>>     print "@globbed\n";
>>
>> }
>I do think these lines are worse than using loop BLOCK. What I want is
>an single expression, not statements with so many nosiy lines.

You can have a single statement, but that's probably even more noisy:

my @globbed = do { local $"=',';
  glob "{@{['a'..'c']}}{@{[1..3]}}" };


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Sat, 24 Nov 2007 14:58:53 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: How to get the string Cartesian Products of 2 list
Message-Id: <jgbgk354fg3mdf3hlfhbm9fj50j0asivs5@4ax.com>

On Sat, 24 Nov 2007 05:26:24 -0800 (PST), xueweizhong@gmail.com wrote:

>> >> local $" = ','; # List separator
>> >> print glob "{@{['a'..'c']}}{@{[1..3]}}\n";
>
>Is this difference below a language feature or bug?
>
>>perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{064}]}}>'
>a001,a002,a003,a004,a005,a006,a007,a008,a009,a010,a011,a012,a013,a0

Feature. Try

  perl -le 'print for "aa".."bb"'


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Sat, 24 Nov 2007 06:08:41 -0800 (PST)
From: xueweizhong@gmail.com
Subject: Re: How to get the string Cartesian Products of 2 list
Message-Id: <27d0ad61-e392-48a7-b1ef-85b518715814@s12g2000prg.googlegroups.com>

> >>perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{064}]}}>'
> >a001,a002,a003,a004,a005,a006,a007,a008,a009,a010,a011,a012,a013,a0
>
> Feature. Try
>
>   perl -le 'print for "aa".."bb"'
>
You misunderstood me. What i want to understand is the difference
between the output of 2 commands below:
1. perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{064}]}}>'
2. perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{065}]}}>'

The 2nd one doesn't out of our expection, can you try this to see
what's going on?:
$perl -e 'local $" = ","; print join ",", <a{@{[q{001}..q{065}]}}>'
a


-Todd



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

Date: Sat, 24 Nov 2007 14:08:42 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: searching for a pattern for multiple matches
Message-Id: <fi9bfl.1i0.1@news.isolution.nl>

jhu schreef:

> Can anyone tell me using Perl how to find all matching patterns. So if
> I have this one big string of say a html web page which contains
> multiple IP's and my pattern which is \d{1,3}\.\d{1,3}\.\d{1,3}\.
> \d{1,3} then how do I go about setting up my code in some sort of
> while loop to find all instances of matches ?


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

use Regexp::Common;

local $\ = "\n";

(my $big_string = join "", <DATA>) =~ s/\s*\n\s*/ /g;

print $big_string;
print "-" x 40;

my @ips = $big_string =~ m/$RE{net}{IPv4}/g;

print for @ips;

__DATA__
abc 123.45.6.78 xyz
abc 234.45.68.91 xyz
abc 123.456.78.9 xyz
abc 1.2.3.4 xyz
abc 123.45.678 xyz
abc 234.45.68.910 xyz
abc 1.2.3.4 xyz


See also:
  http://search.cpan.org/~abigail/Regexp-Common/lib/Regexp/Common/net.pm


> Also can someone in
> simple terms explain what a back reference is ? Is it simply the
> pattern found ?

A backreference is a sub-pattern. (The word pattern is traditionally
reserved for the whole search pattern.)

With a backreference, you reuse something that is already found by an
earlier capture in the pattern:

    m/ ([aeiou]) \1 /x;     # two vowels, equal
    m/ [aeiou]{2} /x;       # two vowels, maybe equal


Now read both perlre and perlretut.

In perlre you'll find:
"Referring back to another part of the match is called a backreference."

In perlretut you'll find:
"Backreferences are simply matching variables that can be used inside a
regexp.  This is a really nice feature - what matches
later in a regexp can depend on what matched earlier in the regexp."

-- 
Affijn, Ruud

"Gewoon is een tijger."



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

Date: Sat, 24 Nov 2007 04:34:00 -0800 (PST)
From: sisyphus <sisyphus359@gmail.com>
Subject: Re: SvUOK always fails on 64bit platform
Message-Id: <e59ff288-ec7c-474d-add1-0494ddd4fb64@i12g2000prf.googlegroups.com>

On Nov 24, 3:08 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Ben Morrow <b...@morrow.me.uk>:
 .
 .
> Maybe I'll report a bug after 5.10's out.

Good luck with it. Somewhere, within the p5p archives there should be
buried a thread called "-Duse64bitint on Cygwin (Win32)" where I
raised the issue (without receiving any sympathy).

I also made a couple of attempts to raise the issue on perlmonks ( eg
http://www.perlmonks.org/index.pl?node_id=617840 ), where again, it
was not received with any sympathy.

Ben, I lack the expertise and self confidence to pursue the matter
with any conviction, but if you can get somewhere with this, that's
all the better.

Cheers,
Rob


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

Date: Sat, 24 Nov 2007 14:31:17 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: SvUOK always fails on 64bit platform
Message-Id: <fi9cka.1eo.1@news.isolution.nl>

Ben Morrow schreef:
> sisyphus:
>> Ben Morrow:

>>> 64bit value                         SvUOK
>>> 0       .. (1<<63 - 1)              false
>>> (1<<63) .. (1<<64 - 1)              true
>>
>> There's an interesting little catch here. On many 64-bit builds of
>> perl you'll find that whilst SvUOK returns true for 1<<63, it returns
>> false for 2**63 (because, on perls that don't also have long double
>> support, 2**63 is an NV). If your perl is also built with long double
>> support, then this issue does not arise.
>
> Yes: I noticed 2**63 was a NV, which is why I used << instead :). It
> seems that since ** is pow(3), it counts as a floating-point function.
>
> This all illustrates another point, which is that depending on Perl
> passing a particular representation of data to XS is usually a bad
> idea, *especially* with IV/UV/NV which aren't even distinguishable
> from Perl-space.
>
>> I personally find that behaviour quite annoying ... but I seem to be
>> the only one, and it has been put to me that this would *not* be
>> trivial to fix .... so I reckon the behaviour is here to stay :-)
>
> Well, yeah: given that ** is a floating-point operation, you no longer
> have integer precision in the result:
>
> ~% perl -le'print for (1<<55)-1, sprintf "%.0f", 2**55-1'
> 36028797018963967
> 36028797018963968
>
> though I have to say I don't really understand why this does a
> floating-point subtract, when there's a whole lot of careful code in
> both pp_pow and pp_subtract that tries to do arithmetic in integers
> where possible... Hmmm. The logic in pp_pow goes something like
>
>     if (base or power non-integral)
>         use pow(3)
>
>     if (power negative)
>         use pow(3)
>
>     if (base is a power of 2) {
>         calculate by hand, in NVs
>
>         This gives a NV-only result when the result doesn't fit in a
>         NV with integer precision.
>     }
>     else {
>         if (
>             x**power fits in an UV, where x is the smallest power of 2
>             greater than base
>         ) {
>             calculate by hand, in UVs
>
>             This gives a IV/UV result.
>         }
>         else
>             use pow(3)
>         }
>     }
>
> but I don't really understand why the 'power of 2' branch isn't moved
> inside the second 'else', so small powers of 2 to small powers are
> calculated in UVs where possible.
>
> For that matter, even if you are going to calculate in NVs, the result
> is always going to be accurate (since it's a power of 2) so when the
> result fits in an IV/UV at all the result should be I/UOK. That seems
> like an easy bug to fix, so I must be missing something...

Let's ask p5p.

-- 
Affijn, Ruud

"Gewoon is een tijger."



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

Date: Sat, 24 Nov 2007 03:40:24 -0800 (PST)
From: "avilella@gmail.com" <avilella@gmail.com>
Subject: union of 1 and 0 strings
Message-Id: <80ba7bd5-117d-4ec5-b871-b50d1fbd0d39@o6g2000hsd.googlegroups.com>

Hi,

Question:

I have two strings:

  DB<32> x $seqchoice
0  '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 '
  DB<33> x $outgroup
0  '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 '

and I want to make the "union" of the "ones" in $outgroup to
$seqchoice, so that I end up with:

  DB<32> x $seqchoice
0  '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 0 '
  DB<33> x $outgroup
0  '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 '

notice the "1" in the penultimate position in $seqchoice.

Any ideas of a regexp that would do for me?


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

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


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