[26844] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8868 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 19 14:05:27 2006

Date: Thu, 19 Jan 2006 11:05:04 -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, 19 Jan 2006     Volume: 10 Number: 8868

Today's topics:
    Re: hash mystery xhoster@gmail.com
    Re: limiting a loop to 100 <xx087@freenet.carleton.ca>
        List of lists - baffled <news@lawshouse.org>
    Re: List of lists - baffled <thepoet_nospam@arcor.de>
    Re: List of lists - baffled <news@lawshouse.org>
    Re: randomly choose some uniq elements of an array <noreply@gunnar.cc>
    Re: randomly choose some uniq elements of an array <news@chaos-net.de>
    Re: randomly choose some uniq elements of an array xhoster@gmail.com
    Re: randomly choose some uniq elements of an array <noreply@gunnar.cc>
    Re: randomly choose some uniq elements of an array xhoster@gmail.com
        traversing a hash two using serveral conditions <me@invalid.domain>
    Re: traversing a hash two using serveral conditions <mark.clementsREMOVETHIS@wanadoo.fr>
    Re: Validating massive amounts of external links xhoster@gmail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 19 Jan 2006 15:42:18 GMT
From: xhoster@gmail.com
Subject: Re: hash mystery
Message-Id: <20060119104218.207$6f@newsreader.com>

monkeys paw <steve@statenet.com> wrote:
> For some reason i'm forced to use a scalar for
> ONE hash element and not the rest. Here is how the
> code behaves:
>
> This works:
>
> $qry = new CGI;
>
> foreach (@names) {
>    if ($_ =~ /^newsummary(.*)$/ && $qry->param($_)) {
>    # I have no clue why 'id' must be a scalar to work. [sf]
>    my $id = $qry->param('id' . $1);

CGI's param can return a list if called in a list context.  The hash
assignment is a list context.  If you call it directly in the hash
assignment, and it happens to return a list of size other than 1, than your
hash gets mis-aligned.

If you want to avoid the temp variable you could put "scalar()" around the
call to param.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: 19 Jan 2006 15:10:42 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: limiting a loop to 100
Message-Id: <slrndsvavi.j8f.xx087@smeagol.ncf.ca>

At 2006-01-18 08:29PM, Abigail <abigail@abigail.nl> wrote:
>  Glenn Jackman (xx087@freenet.carleton.ca) wrote on MMMMDXXIII September
>  MCMXCIII in <URL:news:slrndstfhl.j8f.xx087@smeagol.ncf.ca>:
>  []      foreach ((keys %myhash)[0..99]) {
>  []         # do stuff
>  []      }
>  
>  
>  That doesn't too well if %myhash happens to contain less than 100 elements.

Thanks for pointing that out.

-- 
Glenn Jackman
Ulterior Designer


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

Date: Thu, 19 Jan 2006 14:16:17 +0000
From: Henry Law <news@lawshouse.org>
Subject: List of lists - baffled
Message-Id: <1137680178.16023.0@ersa.uk.clara.net>

I'm doing some simple list-of-lists processing, where the inner lists 
are collections of field data from a database.  I'm completely failing 
to see some mistake I've made, probably an obvious one.

I can't post executable code because you can't replicate my environment 
(the database and so forth) but I'm posting a sample case and the 
console output from it, and you'll see where I'm stuck.  (The example 
below is from ActiveState Perl but the same problem happens under Linux)

# ------------------ tryit.pl --------------------------
#! /usr/bin/perl
use strict;
use warnings;

# "use" statements omitted for clarity; they export "%globals".
use DBI;
use Data::Dump qw(dump);

my @hosts = ();

my $hdbh = db_connect(\%globals);   # Opens MySQL connection via DBI/DBD
my $hsth = $hdbh->prepare("SELECT host_name,id FROM host;");
$hsth->execute;
while (my $array_ref = $hsth->fetchrow_arrayref) {
	print "\$array_ref:",dump($array_ref),"\n";
	push @hosts,$array_ref;
	print "\@hosts:",dump(@hosts),"\n";
}
$hsth->finish;

# ------------------ results from console --------------------
F:\>tryit.pl
$array_ref:["foo", 1]
@hosts:["foo", 1]
$array_ref:["main-atx", 2]
@hosts:do {
   my $a = ["main-atx", 2];
   ($a, $a);
}

The second array reference looks to be in /exactly/ the same format as 
the first, and when the first is pushed onto the outer list all looks 
OK, yet when the second array ref is pushed onto the list everything 
goes haywire.    Help?

-- 

Henry Law       <><     Manchester, England


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

Date: Thu, 19 Jan 2006 16:11:07 +0100
From: Christian Winter <thepoet_nospam@arcor.de>
Subject: Re: List of lists - baffled
Message-Id: <43cfac0c$0$20772$9b4e6d93@newsread4.arcor-online.net>

Henry Law wrote:
> I'm doing some simple list-of-lists processing, where the inner lists 
> are collections of field data from a database.  I'm completely failing 
> to see some mistake I've made, probably an obvious one.
> 
[code snipped that pushes the return value of fetchrow_arrayref onto
an array]

 From the documentation for fetchrow_arrayref (perldoc DBI):

[quote]
Note that the same array reference is returned for each fetch, so
don't store the reference and then use it after a later fetch. Also,
the elements of the array are also reused for each row, so take care
if you want to take a reference to an element. See also "bind_columns".
[/quote]

So if you want to use the elements later on, you have to
create a copy of their values by invoking fetchrow_array
and storing a reference to that new array.

-Chris


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

Date: Thu, 19 Jan 2006 15:39:44 +0000
From: Henry Law <news@lawshouse.org>
Subject: Re: List of lists - baffled
Message-Id: <1137685186.6246.0@echo.uk.clara.net>

Christian Winter wrote:
> Henry Law wrote:
> 
>> I'm doing some simple list-of-lists processing, where the inner lists 
>> are collections of field data from a database.  

>  From the documentation for fetchrow_arrayref (perldoc DBI):
> 
> [quote]
> Note that the same array reference is returned for each fetch, so
> don't store the reference and then use it after a later fetch. 

Ohhhh ... I never looked at the DBI documentation when debugging this. 
Now I see what's happening and can fix it easily. Thank you very much.

-- 

Henry Law       <><     Manchester, England


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

Date: Thu, 19 Jan 2006 15:37:16 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: randomly choose some uniq elements of an array
Message-Id: <439mctF1m9cgvU1@individual.net>

Martin Kissner wrote:
> I want to choose a number of files from a directory randomly.
> 
> This is what I have so far (after reading "perlodc -f "How do I shuffle
> an array randomly?"").

<snip>

> my @shuffled;
> while (@files) {
> 	# the FAQ says this is bad

No need to worry unless the array is large.

>     push(@shuffled, splice(@files, rand @files, 1));
> }
> 
> for (1..3) {
>     print pop @shuffled,"\n";
> }
> -------
> 
> I am pretty sure that there is a better an simpler solution.
> Any suggestions will be appreciated.

It appears as if you don't need to shuffle the whole array.

     for (1..3) { print splice(@files, rand @files, 1), "\n" }

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Thu, 19 Jan 2006 15:43:40 +0100
From: Martin Kissner <news@chaos-net.de>
Subject: Re: randomly choose some uniq elements of an array
Message-Id: <slrndsv9cs.4l2.news@maki.homeunix.net>

Gunnar Hjalmarsson wrote :

>> I am pretty sure that there is a better an simpler solution.
>> Any suggestions will be appreciated.
>
> It appears as if you don't need to shuffle the whole array.
>
>      for (1..3) { print splice(@files, rand @files, 1), "\n" }

Thank you very much.
I knew that there must be something simple like this ;).

Best Regards
Martin

-- 
perl -e '$S=[[73,116,114,115,31,96],[108,109,114,102,99,112],
[29,77,98,111,105,29],[100,93,95,103,97,110]];
for(0..3){for$s(0..5){print(chr($S->[$_]->[$s]+$_+1))}}'


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

Date: 19 Jan 2006 16:09:09 GMT
From: xhoster@gmail.com
Subject: Re: randomly choose some uniq elements of an array
Message-Id: <20060119110909.246$QE@newsreader.com>

Martin Kissner <news@chaos-net.de> wrote:
> hello together,
>
> I want to choose a number of files from a directory randomly.

Is "a number" always going to be 3, or at least small?

> This is what I have so far (after reading "perlodc -f "How do I shuffle
> an array randomly?"").
>
> -------
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my $dir = "/path/to/folder";
> opendir DH, $dir || die "can not open $dir: $!";
> my @files = grep !/^\.\.?$|/ ,readdir DH;
> closedir DH;
>
> my @shuffled;
> while (@files) {
>         # the FAQ says this is bad
>     push(@shuffled, splice(@files, rand @files, 1));
> }

Apparently you have read the FAQ.  So why did you implement the method the
FAQ said was bad, rather than one of the methods that it said were good?
Reading the FAQ so that you can do exactly what they tell you *not* to do
seems like an odd programming strategy.

>
> for (1..3) {
>     print pop @shuffled,"\n";
> }
> -------
>
> I am pretty sure that there is a better an simpler solution.

Yes, the FAQ pointed you to two of them.  Now, the fact that you only take
the first 3 elements allows you to engage in certain optimizations, but
they are almost surely not worthwhile and will make things more complex
rather than simpler.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Thu, 19 Jan 2006 17:37:31 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: randomly choose some uniq elements of an array
Message-Id: <439tecF1jsvv7U1@individual.net>

xhoster@gmail.com wrote:
> Martin Kissner <news@chaos-net.de> wrote:
>>
>>my @shuffled;
>>while (@files) {
>>        # the FAQ says this is bad
>>    push(@shuffled, splice(@files, rand @files, 1));
>>}
> 
> Apparently you have read the FAQ.  So why did you implement the method the
> FAQ said was bad, rather than one of the methods that it said were good?
> Reading the FAQ so that you can do exactly what they tell you *not* to do
> seems like an odd programming strategy.

Usually correct, of course. But please check out this thread, where this 
issue was discussed in considerable detail: 
http://groups.google.com/group/comp.lang.perl.misc/browse_frm/thread/da705489201879ba

>>I am pretty sure that there is a better an simpler solution.
> 
> Yes, the FAQ pointed you to two of them.

No, it didn't. The FAQ discusses shuffling of a whole array, while the 
OP wanted to pick a few elements.

> Now, the fact that you only take
> the first 3 elements allows you to engage in certain optimizations, but
> they are almost surely not worthwhile and will make things more complex
> rather than simpler.

Xho, did you read the rest of the thread before posting your follow-up? 
http://groups.google.com/group/comp.lang.perl.misc/browse_frm/thread/b9b4eeb7aabaf998

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: 19 Jan 2006 17:03:44 GMT
From: xhoster@gmail.com
Subject: Re: randomly choose some uniq elements of an array
Message-Id: <20060119120344.901$dC@newsreader.com>

Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
> xhoster@gmail.com wrote:
> > Martin Kissner <news@chaos-net.de> wrote:
> >>
> >>my @shuffled;
> >>while (@files) {
> >>        # the FAQ says this is bad
> >>    push(@shuffled, splice(@files, rand @files, 1));
> >>}
> >
> > Apparently you have read the FAQ.  So why did you implement the method
> > the FAQ said was bad, rather than one of the methods that it said were
> > good? Reading the FAQ so that you can do exactly what they tell you
> > *not* to do seems like an odd programming strategy.
>
> Usually correct, of course. But please check out this thread, where this
> issue was discussed in considerable detail:
> http://groups.google.com/group/comp.lang.perl.misc/browse_frm/thread/da70
> 5489201879ba

Er, what I got out of that thread was that when the list is small, the FAQ
way is better but who cares as they are generally fast anyway, and when the
list is large, the FAQ way is much better.

I'm all for questioning/challening the FAQs, I do it occasionally myself.
But if you are doing so, you might want to come out and explicitly state
your question or challenge.

>
> >>I am pretty sure that there is a better an simpler solution.
> >
> > Yes, the FAQ pointed you to two of them.
>
> No, it didn't. The FAQ discusses shuffling of a whole array, while the
> OP wanted to pick a few elements.

As I said, the optimizations that this allows are almost certainly not
worthwhile.  In my book, that doesn't make them better solutions.  Keeping
all of his code as it is but replacing the splice-based shuffle with a the
FAQ shuffle is better if the list of files is very large.

>
> > Now, the fact that you only take
> > the first 3 elements allows you to engage in certain optimizations, but
> > they are almost surely not worthwhile and will make things more complex
> > rather than simpler.
>
> Xho, did you read the rest of the thread before posting your follow-up?

There is no such thing as "the rest of the thread".  Usenet posts show up
when they show up.  That will be a different time and order for different
people.

If you are refering to your post where you propose switching from the
original n*n+k algorithm to a k*n algorithm, I don't recall whether I read
it before or after I suggested going with a n+k algorithm which I consider
to be simpler as well as more scalable.  So even if I did read your post
first, I still thought my opinion was worth stating.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Thu, 19 Jan 2006 17:31:48 +0200
From: Me <me@invalid.domain>
Subject: traversing a hash two using serveral conditions
Message-Id: <KMOdnYqglMh6LVLeRVn-rA@is.co.za>

I have an array of hash with the contains some of the following data:
	Direction
	Name
	Usage
	Day
	Month
	Year
	Protocol


 From this, I need to get an array of a"Usage"  data for a given 
"Protocol" for a x number of "Day"(s), two direcions.

	Protocol1 = [45, 67 ,87, 76, 75] : Would be usage for
						5 day for "Protocol1".


Snippet of what I have so far:
################
foreach my $p(@$protocols) {

	foreach my $c ($day1..$day2) {

		foreach my $ref (@usage) { #the array with the data
			my $day = $ref->{Day};
			if ($day == $) {
		            push(@in_proto,$ref->{Usage})
				if ($ref->{direction} eq "In");
		            push(@out_proto,$ref->{Usage})
				if ($ref->{direction} eq "Out");

			}

		}

	}

}
################


Am I going in the right direction?


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

Date: Thu, 19 Jan 2006 18:47:14 +0100
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: traversing a hash two using serveral conditions
Message-Id: <43cfd0b2$0$6651$8fcfb975@news.wanadoo.fr>

Me wrote:
> I have an array of hash with the contains some of the following data:
>     Direction
>     Name
>     Usage
>     Day
>     Month
>     Year
>     Protocol
> 

Hi - that isn't a very good description of your data structure. We would 
need to see an exact sample of your data, eg as per the output of 
Data::Dumper. The smallest subset possible that fully demonstrates the 
structure would be fine.

> 
>  From this, I need to get an array of a"Usage"  data for a given 
> "Protocol" for a x number of "Day"(s), two direcions.
> 
>     Protocol1 = [45, 67 ,87, 76, 75] : Would be usage for
>                         5 day for "Protocol1".
> 
> 
> Snippet of what I have so far:
> ################
> foreach my $p(@$protocols) {
> 
>     foreach my $c ($day1..$day2) {
> 
>         foreach my $ref (@usage) { #the array with the data
>             my $day = $ref->{Day};
>             if ($day == $) {
>                     push(@in_proto,$ref->{Usage})
>                 if ($ref->{direction} eq "In");
>                     push(@out_proto,$ref->{Usage})
>                 if ($ref->{direction} eq "Out");
> 
>             }
> 
>         }
> 
>     }
> 
> }
> ################
> 
> 
> Am I going in the right direction?

syntax error at - line 9, near ")
                 if"
syntax error at - line 17, near "}"
Execution of - aborted due to compilation errors.

You need to copy and paste working code, preferably with

use strict;
use warnings;

at the top.

Mark


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

Date: 19 Jan 2006 16:17:31 GMT
From: xhoster@gmail.com
Subject: Re: Validating massive amounts of external links
Message-Id: <20060119111731.512$wK@newsreader.com>

Lars Haugseth <njus@larshaugseth.com> wrote:
> I have a database with a table containing (among others) the columns
> URL, STATUS and LASTCHECKED. Stored in the table are thousand of records
> containg links to external resources (all using the HTTP protocol.)
>
> I need to develop a tool to periodically go through all the links stored
> in the table, and update their status (ALIVE, TEMPORARY UNAVAILABLE,
> DEAD) based on some criteria. For example, DEAD might mean that the
> resource has not responded successfully for the last N days (for some
> value of N.)
>
> Checking each link in sequence could take several days, depending on the
> timeout limit when doing a request. Thus, the checking should be
> performed in multiple threads/processes.

How about using non-blocking IO rather than threads/processes?

> For now, I'm only interested in the HTTP response code and whether there
> was a timeout, but later I may wish to validate the actual content
> returned as well.
>
> I'm just about to start implementing this using LWP::UserAgent and doing
> everything else from scratch, but if any modules or tools exists that
> would reduce the effort I have to put into this, I would be very grateful
> if someone were to direct me to them.

Have you looked at ParallelUserAgent?

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8868
***************************************


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