[19237] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1432 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Aug 3 00:10:34 2001

Date: Thu, 2 Aug 2001 21:10:16 -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: <996811816-v10-i1432@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Thu, 2 Aug 2001     Volume: 10 Number: 1432

Today's topics:
    Re: Matching Strings in an array <ahamm@sanderson.net.au>
    Re: Oh and another quick question <ren@tivoli.com>
        parallel execution of shell commands in Perl script <testaccount@tamu.edu>
        Parsing output into a <form> <phil.latio@f-in-stupid.co.uk>
    Re: Parsing output into a <form> <dbe@wgn.net>
    Re: serial ports/modems and chat2.pl (David Efflandt)
    Re: Setting DOS Variables (Was: Matt Wrights formmail a <dbe@wgn.net>
    Re: Setting DOS Variables (Was: Matt Wrights formmail a <godzilla@stomp.stomp.tokyo>
    Re: special characters/unicode <bart.lateur@skynet.be>
    Re: Splitting a text list into smaller lists? <krahnj@acm.org>
        Strange behavior? <friedman@math.utexas.edu>
        Subject : randomizing a hash <superstu@stanford.edu>
    Re: Subject : randomizing a hash <godzilla@stomp.stomp.tokyo>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 3 Aug 2001 10:26:13 +1000
From: "Andrew Hamm" <ahamm@sanderson.net.au>
Subject: Re: Matching Strings in an array
Message-Id: <3b69f00a$1@news.iprimus.com.au>

Yves Orton wrote in message
<74f348f7.0108020609.700ae943@posting.google.com>...
>
>Or if you have a lot of elements in both arrays maybe
>
>COVER: foreach $cname (@covers) {
>
> $cname!~/$_/ && next COVER foreach (@words); # Bail on negative match
>        print $cname." matches all words\n";
>}
>
Yup - good point and an excellent optimisation when there's a very strong
chance that there will be failure - and that sounds likely when there are
many elements in both arrays.

Permit me to translate to clearer but less compact code for or (probably)
newbie friend (since the line you offer is fairly dense):

COVER: foreach $cname (@covers) {
    foreach (@words) {
        next COVER if $cname !~ /$_/;
    }
    do this ....
}

--
Lamarr: My mind is a raging torrent, flooded with rivulets of thought
    cascading into a waterfall of creative alternatives.
Taggart: Gol-darn it, Mr. Lamarr, you use your tongue purdier than a
    twenty dollar who.re.
Lamarr: Sh!t - kicker...
    -- Blazing Saddles





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

Date: 02 Aug 2001 15:46:49 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: Oh and another quick question
Message-Id: <m3r8uugpfq.fsf@dhcp9-161.support.tivoli.com>

On Wed, 1 Aug 2001, james@zephyr.org.uk wrote:

> In message <slrn9mgn73.nnu.tadmc@tadmc26.august.net>, Tad McClellan
> <tadmc@augustmail.com> writes
>>
>>You should always, yes *always*, check the return value from open():
> 
> Unless you were writing a question of "What is wrong with the
> following Perl code?" surely?

Nope, because a potential answer to the question is that the open()
failed.  Posting code real code that checks for this eliminates that
possibility.

The same holds for strictures and warnings.

-- 
Ren Maddox
ren@tivoli.com


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

Date: Thu, 02 Aug 2001 17:14:58 -0500
From: testaccount <testaccount@tamu.edu>
Subject: parallel execution of shell commands in Perl script
Message-Id: <3B69D0E2.7DB98F90@tamu.edu>

Hi,

How can I make command1 and command2 run in parallel instead of
sequentially as shown below?

#!/usr/local/bin/perl
[snip]
system("command1");
system("command2");
[snip]
exit;

Thanks for any hints,

Bing



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

Date: Thu, 02 Aug 2001 22:59:02 GMT
From: "Phil Latio" <phil.latio@f-in-stupid.co.uk>
Subject: Parsing output into a <form>
Message-Id: <WWka7.48849$tb2.756266@news.easynews.com>

Basically I want to enter a domain name in a text box, press submit and then
get back a page displaying the contents of the meta tags. I can already do
this and simply display the output as text.

However, I want the output within <form></form> and that contents of each
meta tag field goes into the relevant text box (title, keywords,
description) which can then be submitted to a search engine.

The Open Directory Project http://www.dmoz.org has this type of function.

The problem is when I try and do any searching on this subject, one mention
of the meta tag in a search engine brings 1000's of those web positioning
links.

Any pointers will be gratefully accepted.

Phil







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

Date: Thu, 02 Aug 2001 18:53:31 -0700
From: "$Bill Luebkert" <dbe@wgn.net>
Subject: Re: Parsing output into a <form>
Message-Id: <3B6A041B.2DA8A1BA@wgn.net>

Phil Latio wrote:
> 
> Basically I want to enter a domain name in a text box, press submit and then
> get back a page displaying the contents of the meta tags. I can already do
> this and simply display the output as text.
> 
> However, I want the output within <form></form> and that contents of each
> meta tag field goes into the relevant text box (title, keywords,
> description) which can then be submitted to a search engine.
> 
> The Open Directory Project http://www.dmoz.org has this type of function.
> 
> The problem is when I try and do any searching on this subject, one mention
> of the meta tag in a search engine brings 1000's of those web positioning
> links.

If I understand what you are saying, all you need to do is retrieve the page 
using LWP and parse it with HTML::Parser or similar and than plug the values 
into a form.

Start by seeing if you can retrieve a page with LWP.  Once you have that, 
try to parse it for the tags you want with HTML::Parser.  Then it should 
be simple to put the results into a form and print back to the browser.

-- 
  ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852
 (_/   /  )    // //       DBE Collectibles   Mailto:dbe@todbe.com 
  / ) /--<  o // //      http://dbecoll.webjump.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/


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

Date: Fri, 3 Aug 2001 01:02:35 +0000 (UTC)
From: see-sig@from.invalid (David Efflandt)
Subject: Re: serial ports/modems and chat2.pl
Message-Id: <slrn9mju1a.ljg.see-sig@typhoon.xnet.com>

On Thu, 2 Aug 2001, Ronan O'Connor <ronan.oconnor@materna.nl> wrote:
> I'm looking for information on chat2.pl. Can anyone point me towards
> documentation.
> I'm planning to use chat2.pl to connect to a modem on the serial port.
> Anyone know of any useful modules to do this?

What OS?  Win32::SerialPort, has been ported as Device::SerialPort for
Unix and includes many example scripts.  I have used the latter to get the
response from AT commands in Linux.

-- 
David Efflandt  (Reply-To is valid)  http://www.de-srv.com/
http://www.autox.chicago.il.us/  http://www.berniesfloral.net/
http://cgi-help.virtualave.net/  http://hammer.prohosting.com/~cgi-wiz/


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

Date: Thu, 02 Aug 2001 18:33:50 -0700
From: "$Bill Luebkert" <dbe@wgn.net>
Subject: Re: Setting DOS Variables (Was: Matt Wrights formmail alert)
Message-Id: <3B69FF7E.41644793@wgn.net>

"Godzilla!" wrote:
> 
> $Bill Luebkert wrote:
> 
> > 2) Why not just set the vrbl in %ENV before the system call ?
> 
> >         $ENV{test} = 'successful';
> >         system ...
> 
> The concept here, inspired by an article, is to set
> a DOS environmental variable and have it "stick" even
> after a Perl script closes. Otherwords, you could start
> another script and access this variable; behavior just
> like a proper .bat DOS file.

Once set in %ENV, any commands you issue via system etc will inherit 
the value, so in that respect it will stick as long as your process 
or one of its children is in control.

On NT, you can do it with Win32::AdminMisc::SetEnvVar (or so I've heard - 
unable to verify).  That doesn't help much on 9X.

Another possible NT solution (untested stolen snippet):

use Win32::Registry;

# Set up globals
{
        $RegistryKey    = '';
        $Variable       = "MYVAR";
        $Value          = "Something";
}

$HKEY_CURRENT_USER->Create( "Environment", $RegistryKey )||die $!;
$RegistryKey->SetValueEx( $Variable, 0, REG_SZ, $Value );
$RegistryKey->Close();


Apparently 9X doesn't save ENV in registry (unless it's encoded), but it 
must exist somewhere in memory in order to start new processes.

-- 
  ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852
 (_/   /  )    // //       DBE Collectibles   Mailto:dbe@todbe.com 
  / ) /--<  o // //      http://dbecoll.webjump.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/


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

Date: Thu, 02 Aug 2001 19:35:55 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Setting DOS Variables (Was: Matt Wrights formmail alert)
Message-Id: <3B6A0E0B.D1A8BB93@stomp.stomp.tokyo>

$Bill Luebkert wrote:
 
> Godzilla! wrote:
> > $Bill Luebkert wrote:

> > > 2) Why not just set the vrbl in %ENV before the system call ?
> >
> > >         $ENV{test} = 'successful';
> > >         system ...

> > The concept here, inspired by an article, is to set
> > a DOS environmental variable and have it "stick" even
> > after a Perl script closes. Otherwords, you could start
> > another script and access this variable; behavior just
> > like a proper .bat DOS file.
 
> Once set in %ENV, any commands you issue via system etc will inherit
> the value, so in that respect it will stick as long as your process
> or one of its children is in control.
 
> On NT, you can do it with Win32::AdminMisc::SetEnvVar (or so I've heard -
> unable to verify).  That doesn't help much on 9X.
 
> Another possible NT solution (untested stolen snippet):
 
> use Win32::Registry;
 
> # Set up globals
> {
>         $RegistryKey    = '';
>         $Variable       = "MYVAR";
>         $Value          = "Something";
> }
 
> $HKEY_CURRENT_USER->Create( "Environment", $RegistryKey )||die $!;
> $RegistryKey->SetValueEx( $Variable, 0, REG_SZ, $Value );
> $RegistryKey->Close();
 
> Apparently 9X doesn't save ENV in registry (unless it's encoded), but it
> must exist somewhere in memory in order to start new processes.


Well appears you are almost as imaginative as I am. Others
say you cannot set DOS environmental variables via Perl due
to being a child process. This is true. However, as I often
say, I am a rogue programmer whom delights in the unusual.

As expected, your shell= .. command.com /E ... etc.. invokes
a second command instance. You can set variables and all this
way but encounter a problem of kicking off Perl, for me, both
Apache and Perl. In a sense, you are rebooting without actually
shutting down. 

* misses slow but very powerful Win3.x *

Adding a shell command to a Win9.x - Win.me machine autoexec.bat
would scare the living nightlights out of most; it is a pure DOS
boot. Easy enough to continue on with the typical "win" command.

Anyhow, with Windows being shelled out to explorer.exe or, in my
case, more powerful and now fast, progman.exe left over from
Win3.x, with this GUI shell being booted, this kills any
possibility of a true DOS shell interface for Win binary
Apache and Win binary Perl.

On setting DOS environment memory, which turned out to not be
the problem, simply open a DOS window and select properties.
Find the memory tab and make adjustments there. Mr. Gates is
making sure we can no longer enjoy the full powers of DOS.

* understands future Windows will not have DOS *

One of my first tests to determine if my system was actually
running out of environment memory was to DOS set a variable.
No problem. I ran mem/debug and inspected memory usage; plenty
left over for environment variables. Nonetheless, trying to
set a variable via a batch file executed by Perl cops that
"out of environment space" which turns out to be a generic
error message; a child process cannot set a parent variable.

* wonders if a batch file executing another batch file ... nah.. *

Your Win32::AdminMisc::SetEnvVar is of interest. During my
play time, I messed around with Win32::Whatever and did
pull environment variables without a hitch. I will be opening
up this module and your suggested module to determine if I can
literally reverse engineer what they do; make 'em run backwards
and set variables.

This comment of yours about Win 9.x and storage of environment
variables, well, this is done almost the same way as Win 3.x via
an autoexec.bat file. However, none of the .bat nor the .sys nor
the .cfg files for boot, contain any information regarding setting
memory for environment variables; it's magic I suppose or, more
realistic, hidden in an .ini file somewhere. No matter, this isn't
the problem.

As all say, the real problem is Perl being a forked up child
and not being able to con its parent into doing something,
like turning over the keys to the 'Vette.

* is chastised for buying her girl a Corvette *

Least she doesn't drive mine anymore.

Have you checked out the newest Corvettes? Fastest ever
made. Zero to sixty in four seconds. My Mako Shark takes
an average four point five seconds. Shucks.

I am certain there is a way. I believe an API interface of sorts
can accomplish this. Is this a worthwhile adventure? Nope. Cannot
think of any reason or need to set "sticky" variables. However,
it is an adventure which I like.

Thanks for all these tips. You have provided good investigative
leads for my playtime activities.


Godzilla!
--

* wonders what happen to the Matt Wright thread *


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

Date: Fri, 03 Aug 2001 01:00:02 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: special characters/unicode
Message-Id: <estjmt8h4u4aknp2nsdp8sda9454r7k7uf@4ax.com>

Stuart Aaron White wrote:

>Can perl read foreign language characters from a file, hash or array?
>How would I type them in a file, hash or array so that they could be read?
>Can I type them just as they appear, or do I need to use some sort of
>numbered code that represents them?
>Does perl write foreign language characters?

Commonly, Perl knows only bytes. If your foreign characters are in the
character set you use for displaying text, like ISO-Latin-1, then perl
can produce them. Like that.

But do note that for example, DOS and Windows use different codes for
the same characters, or rather: display different characters for the
same bytes.

-- 
	Bart.


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

Date: Thu, 02 Aug 2001 23:16:16 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Splitting a text list into smaller lists?
Message-Id: <3B69DFD7.BBAE494@acm.org>

Yves Orton wrote:
> 
> "John W. Krahn" <krahnj@acm.org> wrote in message news:<3B68C7B2.897DA9E9@acm.org>...
> >
> > #!/usr/bin/perl -w
> > use strict;
> >
> > my $file = 'userfile01';
> > my @in;
> > # Read the file of 25000 users from stdin:
> > while ( <> ) {
> >
> >     # Parse the line, using the numeral 4 as the delimiter.
> >     push @in, split /4/;
> >
> >     while ( @in >= 50 ) {
> >         open OUT, "> $file" or warn "Cannot write to $file: $!" and
> > next;
> >         print splice( @in, 0, 50 );
> >         $file++;
> >         close OUT;
> >         }
> >     }
> 
> John, nice use of the ++ operator there..  A couple or minor
> corrections though...  I personally wouldn't just jump to the next in
> case of an open() failure, id die() instead.  Also the above will fail
> to print out the last line if the number of names isnt an even
> multiple of 50. (And to get really picky :-) I am printing the names
> with a join as I think maybe they arent so useful all strung
> together.) My slight modification would be:

Well, if we're going to get picky ;-) then:


> #!/usr/bin/perl -w
> use strict;
> 
> my $file = 'userfile01';

After posting I realised that with 500 output files the numerical suffix
should be longer.

my $file = 'userfile001';

> my @in;
> # Read the file of 25000 users from stdin:
> while ( <STDIN> ) { #using explicit for clarity

Using <STDIN> means you are resticted to STDIN only while using <> means
you can accept input from STDIN or the command line.

>     chomp;
>     # Parse the line, using the numeral 4 as the delimiter.
>     push @in, split /4/;
>     my $flush=eof(STDIN); #flush the array if eof on input
>     while ( @in >= 50 || $flush) {

No need for the $flush variable or the "last if $flush;" statement.

     while ( @in >= 50 || eof( STDIN ) ) {

>         open OUT, "> $file" or die "Cannot write to $file: $!";
>         print OUT join("\n",splice( @in, 0, 50 ))."\n";
>         $file++;
>         close OUT;
>         last if $flush; #bail if there aint any more input
>     }
> }

Or you could do it this way  :-)

#!/usr/bin/perl -w
use strict;

my $file = 'userfile001';
my @in;
# Read the file of 25000 users from stdin or the command line:
while ( <> ) {
    local $, = local $\ = "\n";
    chomp;
    # Parse the line, using the numeral 4 as the delimiter.
    push @in, split /4/;

    while ( @in >= 50 ) {
        open OUT, "> $file" or die "Cannot write to $file: $!";
        print OUT splice( @in, 0, 50 );
        $file++;
        close OUT;
        redo if eof() and @in;
        }
    }



John
-- 
use Perl;
program
fulfillment


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

Date: Thu, 2 Aug 2001 20:41:19 -0500
From: "Chas Friedman" <friedman@math.utexas.edu>
Subject: Strange behavior?
Message-Id: <3b6a03d6_1@feed1.realtime.net>

If someone can explain the following behavior, I'd be most appreciative:
If  I run perl on a file containing:

%try=(22,33,44,55);
print %try->{22}; print "\n";
print \%try->{22}; print "\n";
$href=\%try;
print $href->{22}; print "\n";
@tryagain=(1,2,3);
print @tryagain[1]; print "\n";

the output is:

33
SCALAR(0x17de910)
33
2

I can't understand:
  1. why   print %try->{22};  isn't an error
  2. why the 2nd and 3rd lines of output are different
  3. why   print @tryagain[1];  isn't an error
      (Note: print %try{22};  does produce an error message.)

Thanks for any comments!
                                          chas friedman





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

Date: Thu, 2 Aug 2001 17:03:07 -0700
From: Stuart Aaron White <superstu@stanford.edu>
Subject: Subject : randomizing a hash
Message-Id: <Pine.GSO.4.31.0108021702570.29193-100000@epic23.Stanford.EDU>

----- Message Text -----

I'm trying to randomize a hash using the rand() function.  I decided that
I can't do it that way, so I used the keys() function to get an array of
the keys, randomized the keys like rand(@keyarray), and now want to guess
the
value based on the key.

So I stored a random key into a scalar variable like so
$randomKey = "$keyarray[rand($keyarray)]";
chomp ($randomKey);

and am taking a user's guess of the value that matches the key.
I then compare the user's guess to the value that is supposed to match the
key and store it in a scalar like this:

$randomValue = $originalarray{"$randomKey"};
chomp ($randomValue);

So when I compare the user's guess to $randomValue, even if the user
guesses the correct value, the test says it's wrong.

I just want to randomize the hash, pick a random key, have the user guess
the value based on that random key, and tell the user if he is correct or
not.  Can someone tell me what is wrong with how I'm doing this?

thanks


Stuart
650-701-1658


Stuart
650-701-1658




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

Date: Thu, 02 Aug 2001 19:01:40 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Subject : randomizing a hash
Message-Id: <3B6A0604.83D8C7FE@stomp.stomp.tokyo>

Stuart Aaron White wrote:

> I'm trying to randomize a hash using the rand() function.

> ...now want to guess the value based on the key.

> and am taking a user's guess of the value that matches the key.
> I then compare the user's guess to the value that is supposed to match the
> key and store it in a scalar like this:


KISS, Keep It Simple Silly.

For a guessing game like this, a very simple associative array
works very nice and, is inherently quicker and more efficient
than use of a hash.

Nothing wrong with a hash but as you know, a hash is less
efficient than a simple array as is an array less efficient
than a simple string.

My test script clearly doesn't intend to provide for user
input but rather exemplifies an underlying technique using
a simple associative array in conjunction with quick and
efficient substring / index. You will discover my method
of randomly selecting an array element is also quicker
and more efficient than randomizing an entire array or
randomizing an entire hash. Additionally, this allows
you to keep your array neat and tidy for later addition
of questions and answers; alphabetical, numerical, whatever.


Godzilla!
--

#!perl

print "Content-type: text/plain\n\n";

@Array = qw (Lucy:Lawless Marie:Antoinette Seven:OfNine Calamity:Jane);

$user_guess = "Bozo";

&Question;

$user_guess = "Jane";

&Question;

sub Question
 {
  $question = $Array[rand(@Array)];

  print  substr ($question, 0, index ($question, ':')), ": Her last name is?\n\n";

  if ($user_guess eq substr ($question, rindex ($question, ":") + 1))
   { print "$user_guess! Correct! You must be an intelligent woman.\n\n"; }
  else
   { print "$user_guess! Wrong! You must be a typical man.\n\n"; }
 }


exit;


PRINTED RESULTS:
________________

Lucy: Her last name is?

Bozo! Wrong! You must be a typical man.

Calamity: Her last name is?

Jane! Correct! You must be an intelligent woman.


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

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


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