[19274] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1469 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 8 18:15:40 2001

Date: Wed, 8 Aug 2001 15:15:19 -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: <997308919-v10-i1469@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 8 Aug 2001     Volume: 10 Number: 1469

Today's topics:
        Returning Array problem <jclover@nati.org>
    Re: Returning Array problem (Tad McClellan)
    Re: Returning Array problem (Malcolm Dew-Jones)
    Re: Returning Array problem <mjcarman@home.com>
    Re: Returning Array problem <ren@tivoli.com>
    Re: Returning Array problem (Tad McClellan)
        Simple sorting question (JR)
    Re: Simple sorting question <cberry@cinenet.net>
    Re: Splitting a text list into smaller lists? <goldbb2@earthlink.net>
    Re: String manipulation <ren@tivoli.com>
    Re: String manipulation <ren@tivoli.com>
    Re: The perlish way to write this? <goldbb2@earthlink.net>
    Re: The perlish way to write this? <uri@sysarch.com>
    Re: Timed out if no input <bcaligari@fireforged.com>
    Re: Timed out if no input (Rene Nyffenegger)
        urllist & install <leclerc.fabrice@wanadoo.fr>
    Re: Where are those !'s coming from? <bart.lateur@skynet.be>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 8 Aug 2001 13:38:55 -0500
From: "Jonathan Clover" <jclover@nati.org>
Subject: Returning Array problem
Message-Id: <tn31gal3v3g324@corp.supernews.com>

Well, I have a very unique problem that I cannot find a solution to a the
current time. It deals with returning an array from a subroutine. For some
reason when this code is executed:

my ($lname, $fname, $user, $payed_for, $to_email, $curr_sess) = user_vars()
|| diehtml("Couldn't run user_vars");

from within my program none of the initialized variables are initialized.
Here is the code of user_vars():

sub user_vars {
my @row;
my $cur = CGI->new();
my $uname = $ENV{REMOTE_USER};
unless($uname =~ /^([^<\s]*)$/) {###Untaint User Variabales###
diehtml("couldn't untaint name: $uname\n");
}
$uname = $1;

my $r_browser = $ENV{HTTP_USER_AGENT};
unless($r_browser =~ /^([^<]*)$/) {###Untaint User Variabales###
diehtml("couldn't untaint browser\n");
}
$r_browser = $1;

###Get User Information from users database###
my $dbh = DBI->connect("DBI:CSV:f_dir=data")
or diehtml("Cannot connect: " . $DBI::errstr);

my $sql = "select * from users where uname = '$uname'";
my $sth = $dbh->prepare($sql)
or diehtml("Cannot prepare: " . $dbh->errstr());
$sth-> execute() or diehtml("Cannot execute:" . $sth->errstr());
@row = $sth->fetchrow_array();
$sth->finish();
$dbh->disconnect();
print $row[0],$row[1],$row[2],$row[3],$row[4],$row[5],"\n";
return @row;
}

The print statement in user_vars() prints out the correct user information
from the database, but when @row is returned to the first bit of code the
scalars from @row are not stored/initialize the values in the my(...)
declaration.
Any help would be greatly appreaciated since the modulazation of my code is
dependant upon this subroutine.
Jonathan Clover
jclover@nati.org





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

Date: Wed, 8 Aug 2001 14:29:12 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Returning Array problem
Message-Id: <slrn9n317o.ndv.tadmc@tadmc26.august.net>

Jonathan Clover <jclover@nati.org> wrote:

>Well, I have a very unique problem that I cannot find a solution to a the
>current time. It deals with returning an array from a subroutine. 


It is not possible to return an array from a Perl subroutine.

I assume you meant "return a list" instead.

Perl FAQ, part 4:

   "What is the difference between a list and an array?"


>For some
>reason 


The reason is precedence.


>when this code is executed:
>
>my ($lname, $fname, $user, $payed_for, $to_email, $curr_sess) = user_vars()
>|| diehtml("Couldn't run user_vars");
 ^^
 ^^ change that to "or"

Perl is seeing:

   my ($lname, $fname, $user, $payed_for, $to_email, $curr_sess) = 
      (user_vars() || diehtml("Couldn't run user_vars") );

due to the high precendence of ||. So user_vars() is being called
in a scalar context.


Or, use parens so precendence will not be an issue:

  v                                                                           v
  v                                                                           v
  (my ($lname, $fname, $user, $payed_for, $to_email, $curr_sess) = user_vars())
  || diehtml("Couldn't run user_vars");



>from within my program none of the initialized variables are initialized.
                        ^^^^

I do not believe you.

I'll bet that $lname is getting the number of elements in @row.


>Here is the code of user_vars():
>
>sub user_vars {
>my @row;


>return @row;
>}


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 8 Aug 2001 12:22:29 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: Returning Array problem
Message-Id: <3b719175@news.victoria.tc.ca>

Jonathan Clover (jclover@nati.org) wrote:
: Well, I have a very unique problem that I cannot find a solution to a the
: current time. It deals with returning an array from a subroutine. For some
: reason when this code is executed:

: my ($lname, $fname, $user, $payed_for, $to_email, $curr_sess) = user_vars()
: || diehtml("Couldn't run user_vars");

The precedence of || is higher than = so what you are doing is assigning
the result of the boolean operator to the list of variables.  The result
of the boolean operator will not be the list returned from user_vars, just
the last element in the list.


The easiest solution might be

	my ($lname, $fname, $user, $payed_for, $to_email, $curr_sess) 
	   = user_vars() ;

	diehtml("Couldn't run user_vars") if not defined ($curr_sess);

which checks that the routine returned at least enough values to set
the last variable.

Simply letting the user_vars "die" might make sense, depending on the rest
of your app.



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

Date: Wed, 08 Aug 2001 14:31:40 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: Returning Array problem
Message-Id: <3B71939C.954128FB@home.com>

Jonathan Clover wrote:
> 
> For some reason when this code is executed:
> 
> my ($lname, $fname, $user, $payed_for, $to_email, $curr_sess) =
>     user_vars() || diehtml("Couldn't run user_vars");
> 
> from within my program none of the initialized variables are
> initialized.

'||' has a higher precedence than '=' which means that your code is
parsed as

my ($lname, $fname, $user, $payed_for, $to_email, $curr_sess) =
   (user_vars() || diehtml("Couldn't run user_vars"));

This puts user_vars() into scalar context. I expect that $lname is
getting the value 6, and everything else is staying undefined.

Get rid of the "|| diehtml ..." part. Perl won't fail to call your
subroutine. If there's a possibility that the sub itself might fail,
have it do its own error handling.

-mjc


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

Date: 08 Aug 2001 14:19:35 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: Returning Array problem
Message-Id: <m34rrie4vs.fsf@dhcp9-161.support.tivoli.com>

On Wed, 8 Aug 2001, jclover@nati.org wrote:

> my ($lname, $fname, $user, $payed_for, $to_email, $curr_sess) =
> user_vars() || diehtml("Couldn't run user_vars");

"||" binds more tightly than "=" and is forcing "user_vars()" into
scalar context.  Change "||" to "or", or parenthesize the assignment
and that should fix the problem.

-- 
Ren Maddox
ren@tivoli.com


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

Date: Wed, 8 Aug 2001 15:35:47 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Returning Array problem
Message-Id: <slrn9n354j.nit.tadmc@tadmc26.august.net>

Malcolm Dew-Jones <yf110@vtn1.victoria.tc.ca> wrote:
>Jonathan Clover (jclover@nati.org) wrote:
>: Well, I have a very unique problem that I cannot find a solution to a the
>: current time. It deals with returning an array from a subroutine. For some
>: reason when this code is executed:
>
>: my ($lname, $fname, $user, $payed_for, $to_email, $curr_sess) = user_vars()
>: || diehtml("Couldn't run user_vars");
>
>The precedence of || is higher than = so what you are doing is assigning
>the result of the boolean operator to the list of variables.  


So far so good.


>The result
>of the boolean operator will not be the list returned from user_vars, 


user_vars() does not return a list. It is called in scalar context.
There is no such thing as a list in scalar context.


>just
>the last element in the list.


We do not know what will be returned without looking inside of
the body of user_vars(). 

(and when we do that, we see that it will _not_ return the last 
 element in the list, it will return the number of elements in @row)


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 8 Aug 2001 13:42:05 -0700
From: tommyumuc@aol.com (JR)
Subject: Simple sorting question
Message-Id: <319333f5.0108081242.290f501f@posting.google.com>

I have a tab-delimited file with fields similiar to the ones below:

01   001   00001   23432
01   005   00002   23422
02   003   00001   23421
04   001   00001   23423
08   002   00005   23423
 ..
 ..
 ..

I need to sort this by the third column so that the output is as
follows:

02   003   00001   23421
04   001   00001   23423
01   001   00001   23432
01   005   00002   23422
08   002   00005   23423
 ..
 ..
 ..

The only sorting I've ever done to this point was taken care of by
@someData = sort { ($a) cmp ($b) } @someData, which worked fine when
the column I wanted sorted by were in positions 1 and 2, respectively.

How can I sort on the third column in the above examples?

Thanks very much.


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

Date: Wed, 08 Aug 2001 21:19:18 -0000
From: Craig Berry <cberry@cinenet.net>
Subject: Re: Simple sorting question
Message-Id: <Xns90F791B069D90cberrycinenetnet1@207.126.101.92>

tommyumuc@aol.com (JR) wrote in 
news:319333f5.0108081242.290f501f@posting.google.com:

> I have a tab-delimited file with fields similiar to the ones below:
> 
> 01   001   00001   23432
> 01   005   00002   23422
> 02   003   00001   23421
> 04   001   00001   23423
> 08   002   00005   23423

> I need to sort this by the third column so that the output is as
> follows:
> 
> 02   003   00001   23421
> 04   001   00001   23423
> 01   001   00001   23432
> 01   005   00002   23422
> 08   002   00005   23423

Not clear on how you're sorting values which are the same at the third 
column.

> The only sorting I've ever done to this point was taken care of by
> @someData = sort { ($a) cmp ($b) } @someData, which worked fine when
> the column I wanted sorted by were in positions 1 and 2, respectively.
> 
> How can I sort on the third column in the above examples?

Your sort function block is superfluous, as is the use of parens around $a 
and $b inside it.  But anyway:


#!/usr/bin/perl -w
# grtdemo - demo of GR Transform sorting for clpm
# Craig Berry (20010808)

use strict;

print map { /\d+\t(.*)/s }
      sort
      map { (split)[2] . "\t$_" }
      <DATA>;

__DATA__
01	001	00001	23432
01	005	00002	23422
02	003	00001	23421
04	001	00001	23423
08	002	00005	23423


-- 
Craig Berry <http://www.cinenet.net/~cberry/>
"That which is now known, was once only imagined." - William Blake



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

Date: Wed, 08 Aug 2001 17:23:48 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Splitting a text list into smaller lists?
Message-Id: <3B71ADE4.EFAE1D08@earthlink.net>

ctverlane@NOSPAM.blackdahlia.zzn.com wrote:
> 
> I apologize in advance for the cluelessness of this question, but I'm
> desperate, so here goes.
> 
> I'm an Oracle dba.  I have a long list of users (25000) of them, in a
> delimited text file.  What I want to do is use Perl to take the long
> list and divide it up into 500 lists of 50 users each.  So the task
> breaks down to 1) dividing the list into groups of 50, using the
> delimiter to show where each username ends, and then 2) writing each
> list to a separate file.

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

$/ = "4"; # input delimiter
$\ = "4"; # output delimiter
my $list = "000";
while( <> ) {
	if( ($. % 50) == 1 ) {
		fileno OUT and close( OUT ) ||
			die "Couldn't close out$list: $!\n";
		open( $ofh, ">", "out" . ++$list ) or
			die qq{open(OUT, ">out$list"): $!\n};
	}
	chomp;
	print;
}
fileno OUT and close( OUT ) ||
	die "Couldn't close out$list: $!\n";
__END__

This untested code should print to files named "file001" ... "file500".

-- 
I need more taglines. This one is getting old.


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

Date: 08 Aug 2001 13:46:11 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: String manipulation
Message-Id: <m3hevie6fg.fsf@dhcp9-161.support.tivoli.com>

On Wed, 08 Aug 2001, krahnj@acm.org wrote:

> Micheal Hue wrote:
>> 
>> I want to convert a string with the format YYYYMMDDHHMMSS into an
>> array, first item contains 4 digits, the rest 2 digits.
>> 
>> my @dstr = $my_string =~
>> /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/;
>> 
>> Is there better way ?
> 
> I don't know if this is "better".
> 
> my @dstr = unpack 'a4a2a2a2a2a2', $my_string;

Or:

my @dstr = unpack 'a4' . 'a2' x 5, $my_string;

-- 
Ren Maddox
ren@tivoli.com


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

Date: 08 Aug 2001 13:56:35 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: String manipulation
Message-Id: <m3d766e5y4.fsf@dhcp9-161.support.tivoli.com>

On Wed, 08 Aug 2001, bart.lateur@skynet.be wrote:

> Brent Dax wrote:
> 
>>    push @dstr, /(\d{4})/g;
>>
>>    push @dstr, $1 while(/(\d\d)/g);
> 
> I think the first statement will push the lot onto @dstr, because
> the regex is matched in list context.
> 
> 	$_ = '20010808091821';
> 	push @dstr, /(\d{4})/g;
> 	push @dstr, $1 while /(\d{2})/g;
> 	$" = ':';
> 	print "@dstr\n";
> -->
> 	2001:0808:0918:20:01:08:08:09:18:21
> 
> Indeed. It even resets pos() at the end of the match, because of the
> failure.

Actually, I think in list context //g always resets pos() -- but I
guess that would follow from there always being a failure at the end.

> And there is no need for a loop in the second statement.
> 
> 	/^(\d{4})/g and @dstr = $1;
> 	push @dstr, /\d\d/g;

I had not realized that //g in list context even honored pos().  It
makes sense, but it doesn't really fall out from a reading of the
documentation.

-- 
Ren Maddox
ren@tivoli.com


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

Date: Wed, 08 Aug 2001 17:40:58 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: The perlish way to write this?
Message-Id: <3B71B1EA.A2DB704@earthlink.net>

BCC wrote:
[snip]
> Thanks Uri (And everyone else too!) for the nice tips.  One question
> on the here doc though.  I agree that it is the best suited for this
> kind of thing, but visually it is a pain in the a$$.  Maybe someone
> knows a way around this particular annoyance with here docs?
> 
> Example:
> sub A_Test_Sub {
>    if ($someValue == 1) {
>       if ($someOtherValue eq "yes") {
>          $str = <<HTML;
>           <a href=http://www.slashdot.com>Slashdot</a>
> HTML
> ^^^^
>       }
>    } elsif ($someValue == 2) {
>       # do stuff
>    } elsif ($someValue == 3) {
>       # do other stuff
>    }
> }
> 
> Notice the HTML end must have no leading whitespace... visually very
> distracting (for me anyaway).  I know you can do $str =<<"    HTML",
> but then in my rapidly changing development environment (read
> correcting my mistakes :) everytime an indent changes I have to go and
> recount spaces so my here doc works again.  Maybe some neat little
> regex works?

Maybe in the future, we'll be allowed to use an RE instead of a literal
string, so you'll be able to do

  $str = <<qr/^\s*HTML$/;
    asdfasldkfjasd
  HTML

With whatever amount of whitespace you want in front of "HTML"

-- 
I need more taglines. This one is getting old.


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

Date: Wed, 08 Aug 2001 21:59:42 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: The perlish way to write this?
Message-Id: <x7elqm9prk.fsf@home.sysarch.com>

>>>>> "BG" == Benjamin Goldberg <goldbb2@earthlink.net> writes:

  >> Notice the HTML end must have no leading whitespace... visually very
  >> distracting (for me anyaway).  I know you can do $str =<<"    HTML",
  >> but then in my rapidly changing development environment (read
  >> correcting my mistakes :) everytime an indent changes I have to go and
  >> recount spaces so my here doc works again.  Maybe some neat little
  >> regex works?

  BG> Maybe in the future, we'll be allowed to use an RE instead of a literal
  BG> string, so you'll be able to do

  BG>   $str = <<qr/^\s*HTML$/;
  BG>     asdfasldkfjasd
  BG>   HTML

  BG> With whatever amount of whitespace you want in front of "HTML"

perl6 will have more flexible here doc indenting with removal of common
white space prefixes (the amount before the end token). this feature has
been requested by many people. maybe it will be first supported in a
later version of 5.

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Search or Offer Perl Jobs  --------------------------  http://jobs.perl.org


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

Date: Wed, 8 Aug 2001 19:07:13 -0000
From: "B. Caligari" <bcaligari@fireforged.com>
Subject: Re: Timed out if no input
Message-Id: <9krr9v01esq@enews3.newsguy.com>


"Jim" <test@test.com> wrote in message
news:3b7150da.23742565@newscore.theplanet.net...
> Hi,
> I guess this is simple to do but I'm not having much luck.
> I want to wait 5 seconds for a Y/N ans to a question
> "Do you want to continue? "
> If I get a Y within 5 seconds I print "Continuing" if I
> get a N or nothing the script exits.

check out the docs for 'alarm'
perldoc -f alarm

however i think it won;'t work under windows

B




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

Date: 8 Aug 2001 20:11:19 GMT
From: rene.nyffenegger@gmx.ch (Rene Nyffenegger)
Subject: Re: Timed out if no input
Message-Id: <Xns90F769BF524A7gnuegischgnueg@130.133.1.4>

>>Hi,
>>I guess this is simple to do but I'm not having much luck.
>>I want to wait 5 seconds for a Y/N ans to a question
>>"Do you want to continue? "
>>If I get a Y within 5 seconds I print "Continuing" if I
>>get a N or nothing the script exits.
>
>
>Are you running the script under Unix? Then an alarm should do:
>
>print "Yes or No:";
>eval{
>    local $SIG{ALRM} = sub { die };
>    alarm 5;
>    $yn = <STDIN>;
>    print "You typed yn\n";
>    alarm 0;
>    1;
>} or print "you're late!\n";
>
>However, alarm is not implemented under windows. 

Under windows, Term::ReadKey could do the job.


-- 
Recherchen im Schweizerischen Handelsamtsblatt:
http://www.adp-gmbh.ch/SwissCompanies/Search.php3


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

Date: Wed, 08 Aug 2001 23:43:24 +0200
From: Fabrice Leclerc <leclerc.fabrice@wanadoo.fr>
Subject: urllist & install
Message-Id: <leclerc.fabrice-11F1B1.23432408082001@news.wanadoo.fr>

Hi,
I'm trying to install a program which requires the installation of two 
perl modules. In the 1st step of the installation, using 'perl 
Makefile.PL', I was asked to give the URL of a CPAN site. Unfortunately, 
I entered a wrong URL and I couldn't have the 1st step of the 
installation to complete because it couldn't download the two required 
perl modules. In the warning message, it is said that it's possible to 
edit the urllist in the configuration file. However, I was not able to 
localize the configuration file. Do you know where to find the 
configuration file and how to edit the urllist ?


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

Date: Wed, 08 Aug 2001 20:01:43 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Where are those !'s coming from?
Message-Id: <ms53ntsdtlc7rkaifqh4hab0721dq99h5u@4ax.com>

Simon Andrews wrote:

>Some text editors use a ! to denote that a
>line was wrapped, thus a ! can just indicate that there's too much data
>on a line for the editor to display.  The ! would not be there in your
>actual data.

Looking at the data with a hex file viewer, that sounds like a good
idea. That way you can see what the hell there really is. See the Google
category
<http://directory.google.com/Top/Computers/Software/Freeware/Editors/Hex_Editors/>
for a list of (free) tools. You can also write a simple file-to-hexdump
converter in perl, but I doubt if it's that handy for larger files.

-- 
	Bart.


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

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


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