[19679] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1874 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 5 03:05:51 2001

Date: Fri, 5 Oct 2001 00:05:12 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1002265512-v10-i1874@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 5 Oct 2001     Volume: 10 Number: 1874

Today's topics:
        add a record to the beginnig of a file <hubert.ming@iggi.lu.ch>
        Address already in use! <e@nospam:[arix.com]>
    Re: Address already in use! <mgilfix@eecs.tufts.edu>
    Re: Efficient code? <goldbb2@earthlink.net>
    Re: Efficient code? <mgilfix@eecs.tufts.edu>
    Re: How to manipulate data from database <goldbb2@earthlink.net>
        https form post using image instead of submit button (perl-student)
        Huge files and arrays <jzh@earthlink.net>
    Re: Huge files and arrays (Bernard El-Hagin)
    Re: Is Perl for CGI going the way of the dodo? (Tim Hammerquist)
    Re: Is Perl for CGI going the way of the dodo? <ilya@martynov.org>
    Re: Is Perl for CGI going the way of the dodo? (Randal L. Schwartz)
    Re: Multiplexing strings (Damian James)
    Re: Multiplexing strings <joe+usenet@sunstarsys.com>
    Re: Multiplexing strings <dtweed@acm.org>
    Re: Multiplexing strings <joe+usenet@sunstarsys.com>
    Re: Multiplexing strings <goldbb2@earthlink.net>
        Nested loops and scoping $_ <nobody@nowhere.com>
    Re: Nested loops and scoping $_ <wyzelli@yahoo.com>
    Re: Nested loops and scoping $_ <nobody@nowhere.com>
    Re: Nested loops and scoping $_ <wyzelli@yahoo.com>
    Re: new to perl (Chris Fedde)
    Re: parallel http-requests with LWP::Parallel <goldbb2@earthlink.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 5 Oct 2001 09:10:22 +0200
From: "Hubert Ming" <hubert.ming@iggi.lu.ch>
Subject: add a record to the beginnig of a file
Message-Id: <9pjm1t$6al$1@rex.ip-plus.net>

dear perl gurus
is there a way how i can add a new recrod to the beginning of a file. with
the following statement

print DB "$record\n";

all records a added at the end of my (flat) ascii-file. thanx for a hint

hubert





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

Date: Fri, 05 Oct 2001 03:15:49 GMT
From: "ekkis" <e@nospam:[arix.com]>
Subject: Address already in use!
Message-Id: <FB9v7.14113$3e6.1772864577@newssvr14.news.prodigy.com>

hi, I have a piece of code:

    ($name, $aliases, $proto) = getprotobyname('tcp');
    ($name, $aliases, $PORT) = getservbyport($PORT, 'tcp')
        if $PORT !~ /^\d+$/;

    $sockaddr = 'S n a4 x8';
    socket(S, AF_INET, SOCK_STREAM, $proto) || die "socket(): $!";
    bind(S, pack($sockaddr, AF_INET, $PORT, "\0\0\0\0"))
        || die "bind(): $!";

    listen(S, 10) || die "listen(): $!";

which works just fine... however, I have a problem: if I run this and
terminate the program, when I run it again I get a message that the address
is already in use.

this obviously means that I'm failing to do something upon termination;
there's something I'm not cleaning up - like an unbind()... but in all the
stuff I've read there seems to be no indication of what this "cleanup" might
be.

if I wait a little while I can run the program again and everything's fine.

can anyone point me in the right direction?

1k tia - e








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

Date: 5 Oct 2001 05:19:51 GMT
From: Michael Gilfix <mgilfix@eecs.tufts.edu>
Subject: Re: Address already in use!
Message-Id: <slrn9rqgn8.rqb.mgilfix@bofh.concordia.ca>


Hash: SHA1

  Hi. This is actually meant to happen, as defined in the TCP protocol
defintion, if the connection is not shutdown properly. The idea is to wait
on the socket to receive any packets that still might be floating around
the network. On some systems, that wait can be up to two minutes and this
isn't a bad idea for server programs because another instance of the program
might start up and receive a stale packet that was meant for the previous
instance.

  How are you shutting down the program? A simple control-c? You should be
calling close () on the socket to close gracefully. If you're terminating
with ctrl-c, try mapping the signal to something like:

  $SIG{'INT'} = \&{ close (SOCKET); }

  You can also fix this though by setting the SO_REUSEADDR option on the
socket with:

  setsockopt SOCKET, SOL_SOCKET, SO_REUSEADDR, 1;

  Where 'SOCKET' is your socket descriptor. That tells TCP not to linger
on shutdown. But closing the socket properly is preferrable.

              -- Mike

On Fri, 05 Oct 2001 03:15:49 GMT, ekkis <e@nospam> wrote:
> hi, I have a piece of code:
> 
>     ($name, $aliases, $proto) = getprotobyname('tcp');
>     ($name, $aliases, $PORT) = getservbyport($PORT, 'tcp')
>         if $PORT !~ /^\d+$/;
> 
>     $sockaddr = 'S n a4 x8';
>     socket(S, AF_INET, SOCK_STREAM, $proto) || die "socket(): $!";
>     bind(S, pack($sockaddr, AF_INET, $PORT, "\0\0\0\0"))
>         || die "bind(): $!";
> 
>     listen(S, 10) || die "listen(): $!";
> 
> which works just fine... however, I have a problem: if I run this and
> terminate the program, when I run it again I get a message that the address
> is already in use.
> 
> this obviously means that I'm failing to do something upon termination;
> there's something I'm not cleaning up - like an unbind()... but in all the
> stuff I've read there seems to be no indication of what this "cleanup" might
> be.
> 
> if I wait a little while I can run the program again and everything's fine.
> 
> can anyone point me in the right direction?

Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE7vULULZA6dOoLC0ERAh0vAJ4/9o6Uk3ae3UauR53GB5g4N35pzgCeJpXc
1jQ/rAkjIHeuPIeuAIoJ+5Q=
=EoG5
-----END PGP SIGNATURE-----


-- 
Michael Gilfix
mgilfix@eecs.tufts.edu


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

Date: Thu, 04 Oct 2001 21:10:03 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Efficient code?
Message-Id: <3BBD086B.B21DA3C7@earthlink.net>

S Warhurst wrote:
> 
> Sorry if you guys are getting fed up of helping me.. ignore me if you are ;)
> 
> 1) If one wants to go through each line of a 100,000 line text file,
> looking for one of 50 different strings, is the quickest way to do it
> (in terms of processor time) to use 50 IF... ELSIF... commands? (some
> of these do require regexp matching.. eg: "I like cornflakes for
> breakfast" where cornflakes could be any breakfast cereal).

Hmm, for each line of the 1E5 line file, you want to know whether or not
at least one of the 50 strings is part of that line...

Here are a number of methods, though which one is fastest, I don't know.

# noone's suggested this.
my $regex = join "|", map "(?:$_)", qw/list of 50 regexes/;
while( <THEFILE> ) {
    next unless /$regex/o;
    # do stuff with $_
}

# this is most similar to David Wall's and Logan Shaw's suggestions.
my @regex = map qr/$_/, qw/list of 50 regexes/;
OUTER: while( <THEFILE> ) {
   INNER: {
     for my $re { last INNER if /$re/ }
     next OUTER;
   }
   # do stuff with $_
}

# This is sortof like Joe Schaefer's suggestion, but I only process
# each line once.
$_ = do { local $/; <THEFILE> };
$_ .= "\n" if substr($_, -1, 1) ne "\n";
# study($_);
my %lines = ( length() => length()+1 );
my $skip = 0; # lines at the beginning of the file which matched.
foreach my $re (qw/list of 50 regexes/) {
    pos = $skip;
    while( /$re/mg ) {
        my $start = rindex( $_, "\n", $-[0] ) + 1;
        my $end = ($lines{$start} ||= index($_, "\n", $+[0]) + 1));
        $end = $lines{$end} while $lines{$end};
        pos = $end;
    }
    $skip = $lines{$skip} while $lines{$skip};
}
delete $lines{length()};
foreach my $start ( sort { $a <=> $b } keys %lines ) {
    my $line = substr( $_, $start, $lines{$start} );
    # do stuff with $line
}

I think this code should be faster than Joe Schaefer's, since mine
avoids processing any line of the input file more than once, and it
doesn't try to keep track of line numbers, only starts and ends of
lines.  You can speed up the execution time even more if those 50
regexen are arranged so that the ones which match the most lines are
near the beginning of the list, and the ones which match fewest lines
are near the end.

I don't bother to qr the regexen... this makes the code a bit more
portable, since that feature isn't available on Mac.  Whether qr makes a
difference in speed in this type of code, I don't know, you'll have to
benchmark -- remember that a regex isn't recompiled every time, only
those times when the string actually changes, so my code doesn't compile
res any more often than his does.

Since this code collects all the locations, then sorts and processes
them at the end, it may *appear* slow.  If you don't care if they're
processed in order, you can make the code appear to run faster if you
change this line:
        my $end = ($lines{$start} ||= index($_, "\n", $+[0]) + 1));
to this:
        unless( my $end = $lines{$start} ) {
            $end = $lines{$start} = index($_, "\n", $+[0]) + 1);
            my $line = substr( $_, $start, $end );
            # process $line here.
        }
and of course get rid of the foreach/sort/keys%lines loop at the end.

As in Joe's, you have to be careful that your regexen don't match
multiple lines, or the code will act funny.  Since \s *can* match
newlines, don't use it, use [ \t] instead.  It won't *break* exactly,
but some lines might get processed more than once, if they are the
second+th line of an earlier match.

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: 5 Oct 2001 05:38:45 GMT
From: Michael Gilfix <mgilfix@eecs.tufts.edu>
Subject: Re: Efficient code?
Message-Id: <slrn9rqhql.rqb.mgilfix@bofh.concordia.ca>


Hash: SHA1

  This also seems to me like a good time to use the study function.
That might yield a good speed increase as well (depending on the nature
of the data). You'll probably just want to change what's below to
include:

  my @list = ( qr/.../, ...);

  my $line;
  while ($line =~ <DATA>) {
    study $line;
    foreach (@list) {
       ...
    }
  }

  I'm curious as to what performance difference one might see.

On 2 Oct 2001 23:38:02 -0500, Logan Shaw <logan@cs.utexas.edu> wrote:
> Another way you can do this is to quote the regular expressions using
> "qr".  I think this will be faster since they won't have to keep
> getting rebuilt into finite state machines[1].  Doing that instead
> would result in code like this:
> 
>     my @list = ( qr/string1/, qr/string2/, qr/"[^"]+"/, qr/(this|that) stuff/ );
> 
>     my $line;
>     while ($line = <DATA>)
>     {
> 	foreach my $regex (@list)
> 	{
> 	    print "$regex matched at $.\n" if $line =~ $regex;
> 	}
>     }
> 
> I just ran this code on 78,000 line file, and it took about 1.75s, so
> with 50 regexps and 100,000 lines, it should still be well under a
> minute.

Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE7vUdLLZA6dOoLC0ERAmITAKCLSUm+FsAR5vtrOEgACP/qvsQd6wCdFO+x
v5dKNlMfH0BCmss/iCgb94U=
=WBBK
-----END PGP SIGNATURE-----


-- 
Michael Gilfix
mgilfix@eecs.tufts.edu


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

Date: Thu, 04 Oct 2001 23:47:24 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: How to manipulate data from database
Message-Id: <3BBD2D4C.58EB97D9@earthlink.net>

SAS wrote:
> 
> Hi,
> 
> I am using Perl DBI to extract data from oracle and then put the data
> in xml format. I am joining all of the tables to get the result, so I
> only execute one sql select statement.  This is the format I recieve
> in:
> 
> Fname,Lname,SSID,p_num,T1,T2,T3,address1,address2,city,state,e_num
> 
> The problem is that I have alot of 1 to many results.  That is for
> each Fname,Lname, I could get up to 1000 p_num. So each result
> recieved will have Fname,Lname,SSID,T1,T2,T3 repeated up to 1000 times
> and the p_num ofcourse is different.  So in my while statement, I am
> taking each result returned from the database and put it in xml
> format...but I get alot of repeated fields. How do I manipulate the
> data so that I don't include the repeated fields in my xml document?

Leave p_num out of the list of what's to be fetched in the SQL query,
and insert the word "DISTINCT" between the word "SELECT" and the list of
fields you want to fetch.

If Oracle doesn't support the DISTINCT keyword, you'll have to do this
part yourself.

$sth = $dbi->prepare( ... );
$sth->execute( ... );
my @fields = grep $_ ne "p_num", @{$sth->{NAME}};
my %rows;
while( my $row = $sth->fetchrow_hashref ) {
    push @{$rows{ join $; => @row{@fields} }} => $row->{p_num};
}
while( my ($row, $p_nums) = each %rows ) {
    my @row = split $; => $row;
    print "<MYRECORDTYPE";
    print " ", $_, "=", shift(@$row) foreach @fields;
    print ">";
    print "<p_num>$_</p_num>" for @$p_nums;
    print "</MYRECORDTYPE>";
}

This code is untested.

I assume that your data isn't binary, so the default value of $; should
be safe to use.

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: 4 Oct 2001 23:12:24 -0700
From: keerthidharam@iis.com (perl-student)
Subject: https form post using image instead of submit button
Message-Id: <cc7de4d5.0110042212.48d8eb99@posting.google.com>

How to post a form with an image instead of submit button?  The
following code is failing to post correct credentials.  Should I get a
client certificate first? How to associate variables to appropriate
forms if a page contains multiple forms.
   Please help me.

	use English;
	use CGI;
	use integer;
	use HTML::Form;
	use LWP::UserAgent;
	
	# Create a user agent object
	$ua = new LWP::UserAgent;
	$ua->agent('Mozilla/5.0');

	# Create a request
	my $req = new HTTP::Request GET => 'https://foo.com/scripts/';

	# Get response (login page)
	$resp=$ua->request($req);

	#Prepare request Object
	$req = new HTTP::Request POST => "$login_form_action_target";
	$req->content_type('application/x-www-form-urlencoded');
	$req->content('UserName=user&password=password&image=go.gif');

	#Store the Response in Response Object
	$res=$ua->request($req);
	
	#Check the outcome of the response
	if ($res->is_success) 
	{
	print $res->content;
	}
	else
	{
	print "Bad luck this time\n";
	}


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

Date: Fri, 05 Oct 2001 05:01:47 GMT
From: "jzh" <jzh@earthlink.net>
Subject: Huge files and arrays
Message-Id: <%8bv7.3508$3i3.406175@newsread1.prod.itd.earthlink.net>

I have a file ($file in below example) which is very large about 5million
lines about 5gb (extrapolated based on 500,000 lined file of  500mb total
size)

in my code i am passing the input file into an array then looping through
the array to perform my logical search.

I perform many passes thru this files parsing the data using the @file
is there a way to minimize the memory usage whicle accomplishing the same
goal?

part of working code:

#
# Open File
#
sub open_file {
  open (FH, "$file") or die "Err File open";
  @file=<FH>;
  close (FH);
  open (OUT, ">$out") or die "Err File open";
}

thanks joey







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

Date: Fri, 5 Oct 2001 05:44:26 +0000 (UTC)
From: bernard.el-hagin@lido-tech.net (Bernard El-Hagin)
Subject: Re: Huge files and arrays
Message-Id: <slrn9rqho8.8li.bernard.el-hagin@gdndev25.lido-tech>

On Fri, 05 Oct 2001 05:01:47 GMT, jzh <jzh@earthlink.net> wrote:
>I have a file ($file in below example) which is very large about 5million
>lines about 5gb (extrapolated based on 500,000 lined file of  500mb total
>size)
>
>in my code i am passing the input file into an array then looping through
>the array to perform my logical search.
>
>I perform many passes thru this files parsing the data using the @file
>is there a way to minimize the memory usage whicle accomplishing the same
>goal?
>
>part of working code:
>
>#
># Open File
>#
>sub open_file {
>  open (FH, "$file") or die "Err File open";
>  @file=<FH>;
>  close (FH);
>  open (OUT, ">$out") or die "Err File open";
>}


You put a 5gb file into memory? Hmmm...


Anyway, if you want to loop through a file without reading it
into memory do this:


open( FH, $file ) or die $!;
while( <FH> ){
	# do something with each line
}


Cheers,
Bernard


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

Date: Fri, 05 Oct 2001 02:25:54 GMT
From: tim@vegeta.ath.cx (Tim Hammerquist)
Subject: Re: Is Perl for CGI going the way of the dodo?
Message-Id: <slrn9rq7bm.4b7.tim@vegeta.ath.cx>

Me parece que Gregory Toomey <nobody@nowhere.com> dijo:
[ snip ]
> I reasons I like Perl (& Python) are
> 1. I use a Linux web hosting service, and I like telnet and vi! Programming
> in ASP and using FTP to upload files sucks.

This also helps when doing Apache/mod_perl/Apache::ASP.  I can just
telnet in, edit in whatever vi app is available, and have a basic page
up and running in 1-5 minutes.

> 2. They can be used for much more than cgi. You can write one liner or a
> 10,000 line OO system. (Just look at how many lines a "hello world" Java
> program has.) You can run a script in a telnet session or thru a web server.

Let's see, first import the System module.  Define the outlying class,
then define an inner main() method, then dereference objects in the
System module 2 to 3 times to access the println() method, and pass the
string.  Anything else adds at least 2 lines of code.  Then compile,
then execute.  All this versus:

$ perl -e 'print "Hello world!\n";

Unlike Java, the code doesn't grow exponentially if you want to read
some input from STDIN before processing and outputing it.

> 3. With 2Ghz CPUs, the compile/link/execute model is old fashioned. I get
> complex Perl/mysql/cgi scripts taking 1-2 seconds elapsed.

Wow. Very similar time frame on my 450MHz PIII Linuz box.

And I just want to clear up the common misconception that the OP seems
to have real quick:

    CGI is not Perl, nor vice versa.

    Perl is one way to implement a CGI app.  You can use Java to perform
    CGI, but see, there's a reason Perl was chosen as a popular CGI
    language.

    CGI may very well be falling into obsolescence, but that does not
    sign Perl's death certificate, nor does it mean that CGI will die
    any time soon.  CGI will be around for a while simply because so
    many people use it.  This is similar to the reason many managers
    insist on Micro$oft products, even when there are so many faster,
    more stable, and more cost-efficient solutions.  Oh yeah, and these
    same solutions often have fewer bugs.

HTH. I would think a college campus would have more *nix faithfuls than
the workplace at large.  Or does an increasingly liberal outlook
coincide with an increasingly commercial product base?

Something for me to ponder.  ;)

HTH
Tim
-- 
Of course, that's just my opinion.  I could be wrong.
    -- Dennis Miller


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

Date: 05 Oct 2001 10:05:42 +0400
From: Ilya Martynov <ilya@martynov.org>
Subject: Re: Is Perl for CGI going the way of the dodo?
Message-Id: <87lmiqpqo9.fsf@abra.ru>

>>>>> On Fri, 05 Oct 2001 02:25:54 GMT, tim@vegeta.ath.cx (Tim Hammerquist) said:

TH> And I just want to clear up the common misconception that the OP seems
TH> to have real quick:

TH>     CGI is not Perl, nor vice versa.

TH>     Perl is one way to implement a CGI app.  You can use Java to perform
TH>     CGI, but see, there's a reason Perl was chosen as a popular CGI
TH>     language.

TH>     CGI may very well be falling into obsolescence, but that does not
TH>     sign Perl's death certificate, nor does it mean that CGI will die
TH>     any time soon.  CGI will be around for a while simply because so
TH>     many people use it.  This is similar to the reason many managers
TH>     insist on Micro$oft products, even when there are so many faster,
TH>     more stable, and more cost-efficient solutions.  Oh yeah, and these
TH>     same solutions often have fewer bugs.

I would like to add that if you want to develop in Perl it doesn't
necessary means CGI. There is exist AxKit, HTML::Mason. Apache::ASP
and other technologies. For example I hardly remember when I wrote CGI
last time (I work as a web developer).

-- 
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/)                                    |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80  E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/)                          |
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


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

Date: 04 Oct 2001 23:44:55 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Is Perl for CGI going the way of the dodo?
Message-Id: <m1adz6twk8.fsf@halfdome.holdit.com>

>>>>> "Ilya" == Ilya Martynov <ilya@martynov.org> writes:

Ilya> I would like to add that if you want to develop in Perl it doesn't
Ilya> necessary means CGI. There is exist AxKit, HTML::Mason. Apache::ASP
Ilya> and other technologies. For example I hardly remember when I wrote CGI
Ilya> last time (I work as a web developer).

Or even web.  More than once I've seen someone post here "I've been
hacking Perl for 10 years and have yet to do anything with the web."

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


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

Date: 5 Oct 2001 01:07:38 GMT
From: damian@qimr.edu.au (Damian James)
Subject: Re: Multiplexing strings
Message-Id: <slrn9rq1ph.nup.damian@puma.qimr.edu.au>

On 4 Oct 2001 15:46:21 -0700, Dimitri said:
>I have N=3 strings of equal length, and I want to multiplex them by taking
>M=2 characters from each string (the length of each string is divisible by
>M). Example :
>
>Input:
>
>$str1 = "aabbccddeeff";
>$str2 = "AABBCCDDEEFF";
>$str3 = "001122334455";
>
>Output :
>
>$out = "aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55";
>
>Besides the obvious for loop :
>
>$out = ""; $len = length($str1);
>
>for ($i = 0; $i < $len; $i += 2) {
> $out .= substr($str1, $i, 2) . substr($str2, $i, 2) . substr($str3, $i, 2);
>}
>
>Is there a more elegant (faster) way?

Don't know about faster, certainly less memory efficient, but in any case
a workable Other Way To Do It:

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

my @str = qw ( aabbccddeeff AABBCCDDEEFF 001122334455 );
my @seq = map { [ /(..)/g ] } @str;
my $out = join '',
		  map {  $seq[0]->[$_], $seq[1]->[$_], $seq[2]->[$_] }
          0..5;
print "$out\n";

I'm sure someone can think of a neater way to handle that map().

Cheers,
Damian
-- 
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
$:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/}  __END__
Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub


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

Date: 04 Oct 2001 22:26:13 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Multiplexing strings
Message-Id: <m3zo76izzu.fsf@mumonkan.sunstarsys.com>

mauroid@csi.forth.gr (Dimitri) writes:

> Input:
> 
> $str1 = "aabbccddeeff";
> $str2 = "AABBCCDDEEFF";
> $str3 = "001122334455";
> 
> Output :
> 
> $out = "aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55";
> 

Look for swrite() in perlform:

  % perldoc perlform

~>

  @args = ($str1, $str2, $str3);
  $out = swrite("^<^<^<~~", @args); # empties @args

Joe Schaefer
-- 
$.=$[|3*rand;$_=67014523;END{print@_};*UNIVERSAL::AUTOLOAD=sub{&A;pop->();++
$#;*A};$.++,*[=*]=*\=sub{$].=pop.(--$#%2?q: ::qq::)};*#=sub{split m, (?<=^.{2})
(.{$.}),x,shift};@#=qw[just another Perl hacker];*A=*AUTOLOAD=sub{split"::",$A;
&]};map{splice@#,$_,1,&#($#[$_])}3,1;$_="@{[@#[m$.$g]]}";s;P;,p;;eval;$\.="\n"



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

Date: Fri, 05 Oct 2001 04:33:31 GMT
From: Dave Tweed <dtweed@acm.org>
Subject: Re: Multiplexing strings
Message-Id: <3BBD36C8.2FF9D622@acm.org>

Although the swrite() idea is smallest source code (not counting
swrite itself, of course), it takes almost 5 times as long to
execute on my system as Dimitri's original substr-based effort.

I also tried 'split'ing each string into 2-character substrings
(@a1 = split /(..)/, $str1;) and then concatenating the pieces,
but even that took 3 times as long to run.

I think the original substr idea, ugly though it may be, wins.

-- Dave Tweed


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

Date: 05 Oct 2001 00:57:44 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Multiplexing strings
Message-Id: <m3vghuiszb.fsf@mumonkan.sunstarsys.com>

Dave Tweed <dtweed@acm.org> writes:

> Although the swrite() idea is smallest source code (not counting
> swrite itself, of course), it takes almost 5 times as long to
> execute on my system as Dimitri's original substr-based effort.
> 
> I also tried 'split'ing each string into 2-character substrings
> (@a1 = split /(..)/, $str1;) and then concatenating the pieces,
> but even that took 3 times as long to run.
> 
> I think the original substr idea, ugly though it may be, wins.

Wins what?  I didn't realize there was a prize to be had for 
avoiding subroutine calls in the solution.

-- 
Joe Schaefer            "Familiarity breeds contempt -- and children."
                                               --Mark Twain



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

Date: Fri, 05 Oct 2001 03:06:37 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Multiplexing strings
Message-Id: <3BBD5BFD.D6150E2B@earthlink.net>

Dimitri wrote:
> 
> I have N=3 strings of equal length, and I want to multiplex them by
> taking M=2 characters from each string (the length of each string is
> divisible by M). Example :
> 
> Input:
> 
> $str1 = "aabbccddeeff";
> $str2 = "AABBCCDDEEFF";
> $str3 = "001122334455";
> 
> Output :
> 
> $out = "aaAA00bbBB11ccCC22ddDD33eeEE44ffFF55";

while( length $str1 ) {
	$out .= substr( $_, 0, 2, "" )
		for( $str1, $str2, $str3 );
}

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Fri, 5 Oct 2001 12:18:57 +1000
From: "Gregory Toomey" <nobody@nowhere.com>
Subject: Nested loops and scoping $_
Message-Id: <hK8v7.50277$812.78949@newsfeeds.bigpond.com>

Hi, I'm trying to work out the correct Perl idiom.

In the inner loop, $_ refers to elements of list2. Is there a syntax for
accessing elements of list1 from the inner loop?
In general , for nested functions and operations that use $_, can you you
access $_ in each scope?

my @list1=('1','11','111');
my @list2=('2','22','222');

for (@list1) {
        print "outer $_\n";
        for(@list2) {
                print "inner $_\n";
        }
}

gtoomey




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

Date: Fri, 5 Oct 2001 12:09:23 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: Nested loops and scoping $_
Message-Id: <AX8v7.6$E96.624@wa.nnrp.telstra.net>

"Gregory Toomey" <nobody@nowhere.com> wrote in message
news:hK8v7.50277$812.78949@newsfeeds.bigpond.com...
> Hi, I'm trying to work out the correct Perl idiom.
>
> In the inner loop, $_ refers to elements of list2. Is there a syntax for
> accessing elements of list1 from the inner loop?
> In general , for nested functions and operations that use $_, can you you
> access $_ in each scope?
>
> my @list1=('1','11','111');
> my @list2=('2','22','222');
>
> for (@list1) {
>         print "outer $_\n";
>         for(@list2) {
>                 print "inner $_\n";
>         }
> }
>

Why not just :

foreach my $list1 (@list1) {
        print "outer $list1\n";
        foreach my $list2 (@list2) {
                print "inner $list2\n";
        }
}

Wyzelli
--
($a,$b,$w,$t)=(' bottle',' of beer',' on the wall','Take one down, pass it
around');
for(reverse(1..100)){$s=($_!=1)?'s':'';$c.="$_$a$s$b$w\n$_$a$s$b\n$t\n";
$_--;$s=($_!=1)?'s':'';$c.="$_$a$s$b$w\n\n";}print"$c*hic*";





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

Date: Fri, 5 Oct 2001 12:45:48 +1000
From: "Gregory Toomey" <nobody@nowhere.com>
Subject: Re: Nested loops and scoping $_
Message-Id: <q79v7.50293$812.79436@newsfeeds.bigpond.com>

"Wyzelli" <wyzelli@yahoo.com> wrote in message
news:AX8v7.6$E96.624@wa.nnrp.telstra.net...
> "Gregory Toomey" <nobody@nowhere.com> wrote in message
> news:hK8v7.50277$812.78949@newsfeeds.bigpond.com...
>
> Why not just :
>
> foreach my $list1 (@list1) {
>         print "outer $list1\n";
>         foreach my $list2 (@list2) {
>                 print "inner $list2\n";
>         }
> }

That's what I do now. I thought there might by a syntax for accessing the
nested $_. It becomes more complex when using $_ with nested map, grep, for.

gtoomey




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

Date: Fri, 5 Oct 2001 13:51:16 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: Nested loops and scoping $_
Message-Id: <7rav7.13$E96.917@wa.nnrp.telstra.net>

"Gregory Toomey" <nobody@nowhere.com> wrote in message
news:q79v7.50293$812.79436@newsfeeds.bigpond.com...
> "Wyzelli" <wyzelli@yahoo.com> wrote in message
> news:AX8v7.6$E96.624@wa.nnrp.telstra.net...
> > "Gregory Toomey" <nobody@nowhere.com> wrote in message
> > news:hK8v7.50277$812.78949@newsfeeds.bigpond.com...
> >
> > Why not just :
> >
> > foreach my $list1 (@list1) {
> >         print "outer $list1\n";
> >         foreach my $list2 (@list2) {
> >                 print "inner $list2\n";
> >         }
> > }
>
> That's what I do now. I thought there might by a syntax for accessing the
> nested $_. It becomes more complex when using $_ with nested map, grep,
for.

From what I understand of my reading of the 'local' function, the previous
value is pushed onto a hidden stack whilst using the newly localised
variable (I think that is what happens with $_ when you enter a nested
level - maybe a more learned Perler can enlighten me/us) which implies that
the global version is not accessible until leaving the current scope.  It
certainly is not accesible via $main::$_ or similar calls, which makes sense
really, since what would happen when you went beyond two levels of nesting?

Wyzelli
--
($a,$b,$w,$t)=(' bottle',' of beer',' on the wall','Take one down, pass it
around');
for(reverse(1..100)){$s=($_!=1)?'s':'';$c.="$_$a$s$b$w\n$_$a$s$b\n$t\n";
$_--;$s=($_!=1)?'s':'';$c.="$_$a$s$b$w\n\n";}print"$c*hic*";





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

Date: Fri, 05 Oct 2001 02:53:50 GMT
From: cfedde@fedde.littleton.co.us (Chris Fedde)
Subject: Re: new to perl
Message-Id: <2h9v7.980$Owe.353441792@news.frii.net>

In article <c3b9937a.0110041538.6263087b@posting.google.com>,
anirban chakraborti <anirbanchakraborti@hotmail.com> wrote:
>hi guys,
>        I am very new to perl.I have some specific needs like i need
>to be able to ftp files from server ,store them in a database, make
>c/c++ calls ,and use embedded sql .My platform is windows 98.
>                                   Please Advice,
>                                            Anirban

Search google groups for past postings on those subjects.
Familarize your self with CPAN and www.perl.org.  Pay special attention to 
Net::FTP, Inline and the DBI.

Welcome and good luck

-- 
    This space intentionally left blank


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

Date: Thu, 04 Oct 2001 22:57:17 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: parallel http-requests with LWP::Parallel
Message-Id: <3BBD218D.F83BD8A0@earthlink.net>

felix wrote:
> 
> Hi,
> 
> I'm trying to make some parallel request to my website. This is not
> the problem, but I think If go a logical problem and I am actually not
> able to see it... :-((  . I am reading the URL's out of my database
> and want to call 5 of them parallel with this part of my code
> 
> foreach my $categoryid (@CategoryValues){
>         my $StoreUrl = $StoreCatalogUrl . $sid . $categoryid;
>         my $StoreRequest = HTTP::Request->new('GET', $StoreUrl);
>         push (@RequestCount, $StoreRequest);
> 
>         if (scalar @RequestCount eq $MaxRequestCount){
>            foreach my $requests (@RequestCount){
>                # Create $MaxRequestPerTime http-requests
>                print "Registering '" . $requests->url . "'\n"
>                    if (my $res = $ua->register ($requests));
>            }
> 
>            my $entries = $ua->wait();
>            foreach (keys %$entries){
>                my $response = $entries->{$_}->response;
>                print "Answer for '", $response->request->url, "' \n";}
>               @RequestCount = ();
>            print "Scalar of REQEUSTCOUNTER = ". scalar @RequestCount .
> "\n";}
>         else { print "not enough url's yet";}
>     }
> 
> this calls first 5 than 10 than 15 ... urls. but I what i need is that
> it always calls 5 urls....

The problem is that after you've called the first 5, they are still
registered with $ua.  You need to either unregister them somehow [I'm
not sure if this is possible], or else discard $ua and make a new one.

Try code like this:

my $ua = LWP::Parallel::UserAgent->new;
my $count = 0;
foreach my $categoryid (@CategoryValues) {
    my $StoreUrl = $StoreCatalogUrl . $sid . $categoryid;
    $ua->register(HTTP::Request->new('GET', $StoreUrl));
    if( ++$count == $MaxRequestCount ) {
        foreach (values %{$ua->wait}) {
            my $res = $_->response;
            print "Answer for '", $res->request->url, "' was \t",
                $res->code, ": ", $res->message, "\n";
        }
        $ua->initialize(); # resets it.  This is what you were missing.
    }
}
# and don't forget to get the leftovers!
if( $count ) {
    my $entries = $ua->wait();
    foreach (values %$entries){
        my $res = $_->response;
        print "Answer for '", $res->request->url, "' was \t",
            $res->code, ": ", $res->message, "\n";
    }
    $ua->initialize();
}

I think I should mention that you can limit the number of requests which
LWP::Parallel will do simultaneously by using the max_hosts and max_req
methods of LWP::Parallel::UserAgent.

I don't see why you want to artificually limit it, though... I would go
with the maximum number of requests at a time I could manage.

my $ua = LWP::Parallel::UserAgent->new;
foreach my $categoryid (@CategoryValues) {
    my $url = $StoreCatalogUrl . $sid . $categoryid;
    my $data = '';
    $ua->register(HTTP::Request->new('GET', $url), sub {
        my ($chunk, $response) = @_;
        print "Retrieving $StoreUrl\n" if !length $data;
        $data .= $chunk;
        return if length $chunk;
        print "Answer for '$url' was \t",
            $response->code, ": ", $data, "\n";
    } );
}
$ua->wait;

-- 
"I think not," said Descartes, and promptly disappeared.


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

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


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