RE: Importing Identifiers from Main::
On Wednesday, June 29, 2005 3:10 AM, Sisyphus [SMTP:[EMAIL PROTECTED] wrote: > > "Perl in a Nutshell" is quite correct here, I think. If the module does not > have a package name (which is rarely the case) then $var, $::var, and > $main::var are all the same thing. > > D:\pscrpt>type trial.pm > > sub double_it { return $var * 2} > 1; > > D:\pscrpt>type try.pl > > use warnings; > use trial; > > $var = 17; > $var++; > $z = double_it(); > print $z, "\n"; > > D:\pscrpt>perl try.pl > 36 > > D:\pscrpt> > > I think it's only if trial.pm had a package name (as it normally would) that > 'double_it' would have to be coded as either: > > sub double_it { return $::var * 2} > > or: > > sub double_it { return $main::var * 2} > Rob, you get credit for a neat example and for an easy solution that may fit (obvious to you, but I never considered it)-- extend main's namespace over several files. The reason $var is recognized in double_it() is that trial.pm is still in main's namespace when trial.pm's subroutine is compiled. This means that "use trial" in try.pl just looked in the current directory, found a file called trial.pm, assumed it was a module, and went forward with the compilation, not caring whether trial.pm had a package declaration or not. There was nothing to import since trial.pm didn't export anything. But, according to Nutshell, trial.pm isn't a module. Note what Nutshell says under Modules (p. 160): "A module is a package defined in a file whose name is the same as the package." Now note what Nutshell says about packages under Namespaces and Packages (p. 160): "Each package starts with a package declaration. The package call takes one argument, the name of the package..." So, according to Nutshell, a module must start with a package declaration that includes the name of the package. (Therefore Nutshell still has a problem: $var isn't the same as $::var and $main::var if the symbol table changes due to a package declaration, which must be present according to their definition.) > > Your reference to "#define statements" and "constants" makes me wonder > whether you might want to make use of the constant pragma (see perldoc > constant), but since I can't quite get a picture of the precise scenario, I > can't be sure :-) > I'll look at this very carefully, not just for this case, but for similar situations. > > Maybe your question is perfectly clear to someone else . otherwise you > might have to provide a simple little demo module and script to illustrate > the problem. In fact my explanation wasn't clear to anyone, and I apologize. Please see my recent response to Bill Luebkert for a detailed elucidation. Your comments have been very helpful, and I am very appreciative. Regards, Neil ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Test if a string is a number?
Try this simple code : if ($x+0 != 0 or $x =~ /^t/i ) { return 1; } else { return 0; } -Message d'origine- De : [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] De la part de Lyle Kopnicky Envoyé : mercredi 29 juin 2005 23:53 À : perl-win32-users Objet : Test if a string is a number? Hi folks, I have a seemingly simple problem, but I can't find a satisfying solution. I have a function which tests to see if a value represents what I want to call "true". Here's a simplified version: if ($val =~ /true/i || $val =~ /t/i || $val != 0) { return 1; } else { return 0; } The text might be numeric or not. If it is numeric, I want to accept anything but zero as true. But, if I run this on a textual non-true value, such as "false", I get: Argument "false" isn't numeric in numeric ne (!=) at ... The code works, but I don't want to get the warning (I'm using 'warnings'). So, how can I test to see if it's a numeric value, before I try to use it as one? I tried using int() to convert it, but that gives the same warning. Any ideas? Thanks. -- Lyle Kopnicky Software Project Engineer Veicon Technology, Inc. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Active Directory: GPOs
Alan, Also, be aware that Group Policy refresh is not instant... by default, it refreshes every 90 minutes + a random interval of up to 30 minutes (so every 90 to 120 minutes). If you need the timing to be very precise, you should schedule gpupdate.exe to run on all your systems at a time just slightly after the time you change the GPO (give it a few minutes for the GPO to replicate to all domain controllers). Otherwise, your changes may be delayed by as much as 2 hours from the time you modify the GPO. Matt Clark Unit Head, Desktop Services IT Department UCSD Libraries >>> "Alan Peck" <[EMAIL PROTECTED]> 06/29/05 10:18PM >>> Thanx Matt, I try the first method, if that fails I at least can use the 2nd method of editing the GPO file directly. The file format of GPO is rather straight forward, thus can be easily manipulated using Perl scripts. What I could do is create copies of the GPO file elsewhere, modify them. Then have a perl script copy the required GPO file into the correct location at the appropriate time. Thanx all for your advice. Alun >>> "Matt Clark" <[EMAIL PROTECTED]> 06/29/05 8:17 PM >>> Alan, All of the settings within Administrative Templates are basically just registry settings, but the file format they're stored in is different than the normal registry hive file format. You may have some luck trying to modify them by use of the IGroupPolicyObject interface ( http://msdn.microsoft.com/library/en-us/policy/policy/igrouppolicyobject.asp ), which looks like it can load them and return standard registry handles for editing (see the OpenDSGPO, GetRegistryKey, and Save methods). Don't think that's been implemented in Perl, but I'm not entirely sure. Alternatively, you could just read and edit the file with your Perl script... the file format is here: http://msdn.microsoft.com/library/en-us/policy/policy/registry_policy_file_format.asp Hope that helps. Matt Clark Unit Head, Desktop Services IT Department UCSD Libraries >>> "Alan Peck" <[EMAIL PROTECTED]> 06/29/05 03:14AM >>> They are under the user part of the GPO, mostly under the administrative template. The main group of settings are for which applications may be used by the user, what is not allowed to be used, plus other handy windows environment settings. Alun >>> "Matt Clark" <[EMAIL PROTECTED]> 06/29/05 10:12 AM >>> Could you be more specific about what GPO settings you want to alter? Are they administrative templates, advertised applications, etc? Matt Clark Unit Head, Desktop Services IT Department UCSD Libraries >>> "Alan Peck" <[EMAIL PROTECTED]> 06/28/05 10:59PM >>> Is this possible using Perl scripts: To alter the values (including lists) of GPO's attributes? I need to impliment some form of time controlled GPOs for speciallized groups. I know Win2K3 still does not support GPO values merging (not inherintance or overriding) and no time control features thus must some how manipulate the GPOs using scripts. Thanx Alun -- Tshwane University of Technology -- This email is sent and received in terms of the Electronic Communications Policy of Tshwane University of Technology. In line with this policy, this email is private, priviledged and confidential. The full text of the Electronic Mail Disclaimer can be seen on the TUT web site at http://www.tut.ac.za/goto/emaildisclaimer or obtained by phoning (012) 318-5911 <<<>>> ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Importing Identifiers from Main::
- Original Message - From: "L. Neil Johnson" <[EMAIL PROTECTED]> To: "'Sisyphus'" <[EMAIL PROTECTED]> Cc: "'Win32-Users'" Sent: Thursday, June 30, 2005 4:22 PM Subject: RE: Importing Identifiers from Main:: > On Wednesday, June 29, 2005 3:10 AM, Sisyphus [SMTP:[EMAIL PROTECTED] > wrote: > > > > "Perl in a Nutshell" is quite correct here, I think. If the module does not > > have a package name (which is rarely the case) then $var, $::var, and > > $main::var are all the same thing. > > > > D:\pscrpt>type trial.pm > > > > sub double_it { return $var * 2} > > 1; > > > > D:\pscrpt>type try.pl > > > > use warnings; > > use trial; > > > > $var = 17; > > $var++; > > $z = double_it(); > > print $z, "\n"; > > > > D:\pscrpt>perl try.pl > > 36 > > > > D:\pscrpt> > > > > I think it's only if trial.pm had a package name (as it normally would) that > > 'double_it' would have to be coded as either: > > > > sub double_it { return $::var * 2} > > > > or: > > > > sub double_it { return $main::var * 2} > > > Rob, you get credit for a neat example and for an easy solution that may fit > (obvious to you, but I never considered it)-- extend main's namespace over > several files. The reason $var is recognized in double_it() is that trial.pm > is still in main's namespace when trial.pm's subroutine is compiled. This > means that "use trial" in try.pl just looked in the current directory, found a > file called trial.pm, assumed it was a module, and went forward with the > compilation, not caring whether trial.pm had a package declaration or not. > There was nothing to import since trial.pm didn't export anything. > > But, according to Nutshell, trial.pm isn't a module. Note what Nutshell says > under Modules (p. 160): "A module is a package defined in a file whose name is > the same as the package." Now note what Nutshell says about packages under > Namespaces and Packages (p. 160): "Each package starts with a package > declaration. The package call takes one argument, the name of the package..." > So, according to Nutshell, a module must start with a package declaration that > includes the name of the package. > Yep - I looked at the initial quote you provided from Nutshell which was "If the package name is null, the main package is assumed", and read that as meaning "If the package name of the module is null, the main package is assumed". The only way I could make sense of that was if you had a pm file that didn't declare a package name. But you're right - in view of the other text you've quoted it's obvious that my inclusion of "of the module" was way off. In fact their definition of a module as "a package defined in a file whose name is the same as the package" is probably *everybody's* definition so it's hard to come up with an excuse for taking the approach I did (but I'll continue to work on one :-) > (Therefore Nutshell still has a problem: $var isn't the same as $::var and > $main::var if the symbol table changes due to a package declaration, which must > be present according to their definition.) Yes, a package name must be present in a module, but not necessarily present in a script. And if the script doesn't declare a package name, then $var, $::var, and $main::var are all one and the same (within that script). Perhaps that's what they're getting at ?? I don't think they would be meaning that a "$var" within a module could ever be the same as "$main::var" or "$::var", because clearly that "$var" has been declared within a package and must therefore be "$package_name::var". Cheers, Rob ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Importing Identifiers from Main::
At 07:34 2005-06-30, L. Neil Johnson wrote: As mentioned previously, I got tired of editing the absolute indices (e.g., $trade[$i]->[6]) every time the order or meaning of an element of the anonymous arrays changed; so in main I defined typeglobs: This is what I meant when I said a hash is a good solution to this problem. You're not really interested in the order, you want to access each element by a convenient name. It sounds like a bit of data to keep in memory, so going from an array to a hash may not be feasible because of that, but if it is it would look like this instead: $trade[$i]->{"time_elapsed"} If your available memory can take this hit, I'd consider that approach a lot more maintainable. If not, go with either exported constants or simply $UPPER_CASE_VARIABLES as named indices into the array. And to me it looks like the solution would be more clear with each record being an object, but if you're not familiar with OO, don't start introducing it in this program. Try it out with something small and new. http://www.manning.com/books/conway is excellent. /J -- --- -- -- -- - - -- - Johan LindströmSourcerer @ Boss Casinos johanl AT DarSerMan.com Latest bookmark: "TCP Connection Passing" http://tcpcp.sourceforge.net/ dmoz: /Computers/Programming/Languages/JavaScript/ 12 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Importing Identifiers from Main::
L. Neil Johnson wrote: > Thank you for responding. Sorry about the ambiguity. The main program is a > sequence of computational processes that reads several files of 20K to 50K > records and splits them into 12 parallel arrays of that length. Analytic > routines generate an array (@trade) of 2K to 4K elements, each of which is a > reference to an anonymous array of, say, 7 elements; built like so: > > for ($iS=1; $iS<=$#sigs; $iS++) { > ... # processing... > push(@trade,[$j,$i,"TaS",$sig,$amp,$elT,$tre[$j]]); # append trade to > array > }#for > [0]begIx,[1]endIx,[2]indic,[3]type,[4]amplitude,[5]elapsedTime,[6]trend > > Further on, other processes access this data for analysis, and to build and > print other arrays, like so: > > for ($i=1;$i<=$#trade;$i++) { > ... # processing... > push (@tCycle, > [$j,$l,$m,$trade[$i]->[2],$phWin,$trade[$i-2+$sSC]->[4], > $trade[$i]->[5]+$trade[$i-1]->[5],[EMAIL PROTECTED]); > ... # processing... > }#for > > A module was designed (TSP.pm), which contains two subroutines, one which > prints descriptive statistics, and one which prints performance metrics for > the > @trade data. The subroutines, in general, access individual elements of the > anonymous arrays, like so: You could start that package off with a 'package main;' stmt which will put you in the same namespace as the calling module. > for ($i=0;$i<=$#{$rT};$i++) { You can use an array's scalar context to make this easier to read : for (my $i = 0; $i < @{$rT}; $i++) { > ... # processing... > $da = int(${$rD}[${$rT}[$i]->[0]]/86400)*86400; # $rT is ref to @trade > ... # processing... > }#for # using constants (but you have to export them to the other module like your glob refs): use constant xbi => 0; # index into data arrays, begin time use constant xei => 1; # index into data arrays, end time use constant xi => 2; # indicator use constant xs => 3; # signal use constant xa => 4; # amplitude use constant xt => 5; # elapsed time use constant xr => 6; # trend # no code change here push @tCycle, [$j, $l, $m, $trade[$ii]->[$xi], $phWin, $trade[$ii-2+$sSC]->[$xa], $trade[$ii]->[$xt] + $trade[$ii-1]->[$xt], [EMAIL PROTECTED]; # using a hash: my %IX = ( xbi => 0; # index into data arrays, begin time xei => 1; # index into data arrays, end time xi => 2; # indicator xs => 3; # signal xa => 4; # amplitude xt => 5; # elapsed time xr => 6; # trend }; # $xi then becomes $IX{xi} : push @tCycle, [$j, $l, $m, $trade[$ii]->[$IX{xi}], $phWin, $trade[$ii-2+$sSC]->[$IX{xa}], $trade[$ii]->[$IX{xt}] + $trade[$ii-1]->[$IX{xt}], [EMAIL PROTECTED]; > As mentioned previously, I got tired of editing the absolute indices (e.g., > $trade[$i]->[6]) every time the order or meaning of an element of the > anonymous > arrays changed; so in main I defined typeglobs: > > *xbi = \0;# index into data arrays, begin time > *xei = \1;# index into data arrays, end time > *xi = \2;# indicator > *xs = \3;# signal > *xa = \4;# amplitude > *xt = \5;# elapsed time > *xr = \6;# trend > > ...to allow the use of variables for the indices, like so: > > push (@tCycle, > [$j,$l,$m,$trade[$i]->[$xi],$phWin,$trade[$i-2+$sSC]->[$xa], > $trade[$i]->[$xt]+$trade[$i-1]->[$xt],[EMAIL PROTECTED]); > > My question is how best to get those typeglobs into the namespace of module > TSP.pm so that, when I make a change, it's once in main, and the change is > automatically propagated into TSP.pm. Right now, I just cut and paste the > typeglobs from main into TSP.pm. Thank you. -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Importing Identifiers from Main::
$Bill Luebkert wrote: > # using constants (but you have to export them to the other module like > your glob refs): > > use constant xbi => 0;# index into data arrays, begin time > use constant xei => 1;# index into data arrays, end time > use constant xi => 2;# indicator > use constant xs => 3;# signal > use constant xa => 4;# amplitude > use constant xt => 5;# elapsed time > use constant xr => 6;# trend > > # no code change here Actually there is, you have to remove the $'s: > push @tCycle, [$j, $l, $m, $trade[$ii]->[$xi], $phWin, > $trade[$ii-2+$sSC]->[$xa], $trade[$ii]->[$xt] + $trade[$ii-1]->[$xt], > [EMAIL PROTECTED]; push @tCycle, [$j, $l, $m, $trade[$ii]->[xi], $phWin, $trade[$ii-2+$sSC]->[xa], $trade[$ii]->[xt] + $trade[$ii-1]->[xt], [EMAIL PROTECTED]; -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Perl Constants (was RE: mystery)
At 05:29 PM 6/29/05 -0400, John Deighan wrote: >use strict; >use constant X => 23; >my $y = X - 2; >print("y = $y\n"); > >To my amazement, it actually printed out "y = 21". So, using the "use >constant" construct actually gave the correct result. In fact, I tried, >just for the heck of it, to change the assignment to "$y = X(-2);" and got >the error message "Too many arguments for main::X at C:\Scripts\test.pl >line 4, near "2)"" I think this has something to do with the prototyping on "functions". With the Posix constant/function it sucked up everything that could be considered an argument to it. An unlimited list. Which is the normal behavior. The use Constant construct on the other hand apparently wants zero arguments and therefore of course will not try to suck up an unlimited list of arguments. Same as if u make a subroutine with a scalar prototype and u try to pass it an array. -- REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=-- "...ne cede males" 0100 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Test if string is a number?
Wow there's been a lot of heavy duty code proposed to do something so simple. The answer is in how Perl converts between the two. print "is a number" if $var eq $var + 0; print "not a number" if $var ne $var + 0; Say $var is "bob". In the first case we see if "bob" is string equal to bob + 0 or is "bob eq 0". Obviously not. Say $var is 5. In the second case we see if "5" is not string equal to 5 + 0 or is "5 ne 5". In this setup we're forcing the variable into numeric context and then back into string context. How the variable survives that procedure depends on whether it is number like or not number like. At 08:31 PM 6/29/05 -0700, Lyle Kopnicky wrote: >Thanks folks. I think I'll go with looks_like_number from >Scalar::Util. I like to use library routines where possible. I don't >know how I overlooked that, since I poked through Scalar::Util earlier. > >It just seems bizarre to me that something like that isn't a builtin. I >mean, you can't even tell strings from numbers? I'm used to working in >strongly typed languages. -- REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=-- "...ne cede males" 0100 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: mystery
"use constant" defines constants something like this: sub NAME() { return 42;} The prototype prevents anything following a reference to the constant from being accepted as an argument. What's more, Perl will inline the function call if the return value is a constant _expression_ in most cases. The Posix module is almost entirely in XS, and clearly the part that defines the constants does not use prototypes. --Eric Amick Columbia, MD ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Test if string is a number?
> Wow there's been a lot of heavy duty code proposed to do something so > simple. The answer is in how Perl converts between the two. > > print "is a number" if $var eq $var + 0; > print "not a number" if $var ne $var + 0; That fails on 1e7. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Test if string is a number?
Title: Re: Test if string is a number? Chris Wagner wrote, on Thu 6/30/2005 08:48 : Wow there's been a lot of heavy duty code proposed to do something so: simple. The answer is in how Perl converts between the two.:: print "is a number" if $var eq $var + 0;: print "not a number" if $var ne $var + 0; Except if $var is, say, '0.00'. Then $var + 0 is '0', and won't eq $var. Joe == Joseph P. Discenza, Sr. Programmer/Analyst mailto:[EMAIL PROTECTED] Carleton Inc. http://www.carletoninc.com 574.243.6040 ext. 300 fax: 574.243.6060 Providing Financial Solutions and Compliance for over 30 Years ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Test if string is a number?
Chris Wagner wrote: > Wow there's been a lot of heavy duty code proposed to do something so > simple. The answer is in how Perl converts between the two. > > print "is a number" if $var eq $var + 0; > print "not a number" if $var ne $var + 0; > > Say $var is "bob". In the first case we see if "bob" is string equal to bob > + 0 or is "bob eq 0". Obviously not. > Say $var is 5. In the second case we see if "5" is not string equal to 5 + > 0 or is "5 ne 5". > > In this setup we're forcing the variable into numeric context and then back > into string context. How the variable survives that procedure depends on > whether it is number like or not number like. Some of us use strict and warnings. What happens with this ? : use strict; use warnings; my $var = undef; print "is a number" if $var eq $var + 0; print "not a number" if $var ne $var + 0; and my $var = 'bob'; print "is a number" if $var eq $var + 0;# will give a warning if $var not a number -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Test if string is a number?
Title: Re: Test if string is a number? How about regexp? /^\-?(\d+\.?\d*|\.\d+)$/ - Original Message - From: Joe Discenza To: Chris Wagner ; perl-win32-users Sent: Thursday, June 30, 2005 4:48 PM Subject: RE: Test if string is a number? Chris Wagner wrote, on Thu 6/30/2005 08:48 : Wow there's been a lot of heavy duty code proposed to do something so: simple. The answer is in how Perl converts between the two.:: print "is a number" if $var eq $var + 0;: print "not a number" if $var ne $var + 0; Except if $var is, say, '0.00'. Then $var + 0 is '0', and won't eq $var. Joe == Joseph P. Discenza, Sr. Programmer/Analyst mailto:[EMAIL PROTECTED] Carleton Inc. http://www.carletoninc.com 574.243.6040 ext. 300 fax: 574.243.6060 Providing Financial Solutions and Compliance for over 30 Years ___Perl-Win32-Users mailing listPerl-Win32-Users@listserv.ActiveState.comTo unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Test if string is a number?
For a long time I'm using the function below to test for a number, being quite satisfied with it ... # (nmb) number # Returns decimal value of the contents if argument # is a number (integer if octal or hexadecimal), # otherwise returns empty string ('') sub number($) { local $_ = shift; s/^\s*(.*)\s*$/$1/; return '' unless /^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$|^[+-]?0x[0-9a-fA-F]+$/; s/\+//; my $sign = s/^-// ? '-' : ''; /^0[0-7]+$|^0x[0-9a-fA-F]+$/ ? $sign.oct : $sign.$_*1; } Greetings -- Andreas __ Delphi Electronics & Safety FUBA Reception Systems Andreas Kamentz Electrical Engineer / SW Antenna Engineering [EMAIL PROTECTED] Tel: (+49) 5063.990.541 Fax: (+49) 5063.990.99541 Mailing address FUBA Automotive GmbH & Co. KG TecCenter D-31162 Bad Salzdetfurth / Germany -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Siebe Tolsma Sent: Thursday, June 30, 2005 5:31 PM To: Joe Discenza; perl-win32-users@listserv.ActiveState.com Subject: Re: Test if string is a number? How about regexp? /^\-?(\d+\.?\d*|\.\d+)$/ - Original Message - From: Joe Discenza To: Chris Wagner ; perl-win32-users Sent: Thursday, June 30, 2005 4:48 PM Subject: RE: Test if string is a number? Chris Wagner wrote, on Thu 6/30/2005 08:48 : Wow there's been a lot of heavy duty code proposed to do something so : simple. The answer is in how Perl converts between the two. : : print "is a number" if $var eq $var + 0; : print "not a number" if $var ne $var + 0; Except if $var is, say, '0.00'. Then $var + 0 is '0', and won't eq $var. Joe == Joseph P. Discenza, Sr. Programmer/Analyst mailto:[EMAIL PROTECTED] Carleton Inc. http://www.carletoninc.com 574.243.6040 ext. 300fax: 574.243.6060 Providing Financial Solutions and Compliance for over 30 Years ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs Note: The information contained in this message may be privileged and confidential and thus protected from disclosure. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by replying to the message and deleting it from your computer. Thank you. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Test if string is a number?
At 09:48 AM 6/30/05 -0500, Joe Discenza wrote: >Except if $var is, say, '0.00'. Then $var + 0 is '0', and won't eq $var. 0.00 is not a valid internal representation of a number. That can only exist as a string. Same goes for "1e7". That is a print formated number, not a valid internal number. $var = 1e7 and print $var -> 1000. $var = 0.00 and print $var -> 0. If u want to include "numberish" strings then u need some eval's to digest the various number formats. ... if eval $var eq $var + 0; -- REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=-- "...ne cede males" 0100 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Test if string is a number?
[EMAIL PROTECTED] wrote: > 0.00 is not a valid internal representation of a number. > That can only exist as a string. I think "u" need to re-read the subject of this thread. - Mark. ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Test if string is a number?
Title: RE: Test if string is a number? Chris Wagner wrote, on Thu 6/30/2005 12:41 : At 09:48 AM 6/30/05 -0500, Joe Discenza wrote:: >Except if $var is, say, '0.00'. Then $var + 0 is '0', and won't eq $var.:: 0.00 is not a valid internal representation of a number. That can only: exist as a string. Same goes for "1e7". That is a print formated number,: not a valid internal number. $var = 1e7 and print $var -> 1000. $var =: 0.00 and print $var -> 0. If u want to include "numberish" strings then u: need some eval's to digest the various number formats. ... if eval $var eq: $var + 0; IIRC (no longer have the message), the OP had *string* data to check for numerics. Your method would miss, say, a CSV full of prices if they ended ".00" or ".50". I bet you're right that "eval($var) eq $var + 0" works; have you benchmarked it against all the other (regex, e.g.) methods presented? Joe == Joseph P. Discenza, Sr. Programmer/Analyst mailto:[EMAIL PROTECTED] Carleton Inc. http://www.carletoninc.com 574.243.6040 ext. 300 fax: 574.243.6060 Providing Financial Solutions and Compliance for over 30 Years ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Test if string is a number?
At 12:16 PM 6/30/05 -0500, Joe Discenza wrote: >I bet you're right that "eval($var) eq $var + 0" works; have you benchmarked it against all the other (regex, e.g.) methods presented? I haven't benchmarked it but I can garuntee that it's faster than a regex. Anything's faster than that. ;) This should cover everything: foreach $var ("1e7", 0, 5, "6", "0.00", -4, "abc", "f4fc") { if ($var eq $var + 0) { print "$var is a pure number\n"; } elsif (eval $var eq $var + 0) { print "$var is a number\n" } else { print "$var is not a number\n"; } } 1e7 is a number 0 is a pure number 5 is a pure number 6 is a pure number 0.00 is a number -4 is a pure number abc is not a number f4fc is not a number -- REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=-- "...ne cede males" 0100 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Test if string is a number?
Title: RE: Test if string is a number? Chris Wagner wrote, on Thu 6/30/2005 14:41 : At 12:16 PM 6/30/05 -0500, Joe Discenza wrote:: >I bet you're right that "eval($var) eq $var + 0" works; have you: >benchmarked it against all the other (regex, e.g.) methods presented?:: I haven't benchmarked it but I can garuntee that it's faster than a regex.: Anything's faster than that. ;) This should cover everything: Thanks for playing. I compared your function with a regex I whipped up (/^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/, which gave the same results, except for distinguishing "pure" numbers, on your dataset): Benchmark: timing 10 iterations of chris, regex... chris: 12 wallclock secs (11.86 usr + 0.00 sys = 11.86 CPU) @ 8432.41/s (n=10) regex: 2 wallclock secs ( 1.56 usr + 0.00 sys = 1.56 CPU) @ 63979.53/s (n=10) Regex is pretty fast. Eval is usually pretty slow. Joe == Joseph P. Discenza, Sr. Programmer/Analyst mailto:[EMAIL PROTECTED] Carleton Inc. http://www.carletoninc.com 574.243.6040 ext. 300 fax: 574.243.6060 Providing Financial Solutions and Compliance for over 30 Years ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Test if string is a number?
Joe Discenza wrote: > Chris Wagner wrote, on Thu 6/30/2005 14:41 > > : At 12:16 PM 6/30/05 -0500, Joe Discenza wrote: > : >I bet you're right that "eval($var) eq $var + 0" works; have you > : >benchmarked it against all the other (regex, e.g.) methods presented? > : > : I haven't benchmarked it but I can garuntee that it's faster than a regex. > : Anything's faster than that. ;) This should cover everything: > > Thanks for playing. I compared your function with a regex I whipped up > (/^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/, which gave the same > results, except for distinguishing "pure" numbers, on your dataset): > > Benchmark: timing 10 iterations of chris, regex... > chris: 12 wallclock secs (11.86 usr + 0.00 sys = 11.86 CPU) @ > 8432.41/s (n=10) > regex: 2 wallclock secs ( 1.56 usr + 0.00 sys = 1.56 CPU) @ > 63979.53/s (n=10) > > Regex is pretty fast. Eval is usually pretty slow. 1) I don't consider Chris's solution to be a legit solution until it doesn't issue any warnings or error messages. 2) I got these numbers from my test (strict/warnings turned off) : Rate RE STRTOD IFLLN RE 7530/s -- -72% -75% -75% STRTOD 26948/s 258% -- -10% -11% IF 29782/s 295%11% ---2% LLN30274/s 302%12% 2% -- looks_like_number seems to be the winner. I used the same two lines of code in each sub to clean up the input: $var =~ s/^\s+//; $var =~ s/\s+$//; return 0 if not defined $var or $var eq ''; use strict; use warnings; use Scalar::Util qw(looks_like_number); use POSIX qw(strtod); use Benchmark qw(timethese cmpthese); # test output of subs before benchmarking foreach my $sub (\&sub1, \&sub2, \&sub3, \&sub4) { foreach ("1e7", 0, 5, "6", "0.00", -4, "abc", "f4fc", '+.1', undef) { my $val = defined $_ ? $_ : 'undef'; my $ret = &$sub ($_); print $ret ? "sub1 ($ret): '$val' is a number\n" : "sub1 ($ret): '$val' is not a number\n"; $ret = &$sub ($_); print $ret ? "sub2 ($ret): '$val' is a number\n" : "sub2 ($ret): '$val' is not a number\n"; $ret = &$sub ($_); print $ret ? "sub3 ($ret): '$val' is a number\n" : "sub3 ($ret): '$val' is not a number\n"; $ret = &$sub ($_); print $ret ? "sub4 ($ret): '$val' is a number\n" : "sub4 ($ret): '$val' is not a number\n"; } } no warnings;# needed for Chris solution no strict; # needed for Chris solution my $count = 100; cmpthese ($count, { RE => 'main (\&sub1)', IF => 'main (\&sub2)', STRTOD => 'main (\&sub3)', LLN => 'main (\&sub4)', }); sub main { my $cref = shift; foreach ("1e7", 0, 5, "6", "0.00", -4, "abc", "f4fc", '+.1', undef) { &$cref ($_); } } sub sub1 { my $var = shift; $var =~ s/^\s+//; $var =~ s/\s+$//; return 0 if not defined $var or $var eq ''; if ($var eq $var + 0) { return 1; } elsif (eval $var eq $var + 0) { return 2; } return 0; } sub sub2 { my $var = shift; $var =~ s/^\s+//; $var =~ s/\s+$//; return 0 if not defined $var or $var eq ''; return 1 if $var =~ /^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/; return 0; } sub sub3 { my $var = shift; $var =~ s/^\s+//; $var =~ s/\s+$//; return 0 if not defined $var or $var eq ''; $! = 0; my ($num, $unparsed) = strtod ($var); return 0 if $unparsed or $!; return 1; } sub sub4 { my $var = shift; $var =~ s/^\s+//; $var =~ s/\s+$//; return 0 if not defined $var or $var eq ''; # return looks_like_number ($var); # code stripped from LLN: return 1 if (/^[+-]?\d+$/); # is a +/- integer return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # a C float return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i); } __END__ -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Test if string is a number?
At 02:28 PM 6/30/05 -0500, Joe Discenza wrote: >Regex is pretty fast. Eval is usually pretty slow. Yeah ur right about the eval. I did a triple head to head with ur regex and eval/no eval. The eq without the eval demolishes all. Rate evalRE noeval eval3397/s -- -87% -96% eval "$var" eq $var + 0; RE 25253/s 643%-- -72% /^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/ noeval 90909/s 2576% 260% -- $var eq $var + 0; -- REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=-- "...ne cede males" 0100 ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
system() Conundrum
I have a Perl script that receives programs and other files from a host system for editing. Once the file is transferred the script launches my text editor and passes it the name of the file. It works fine for me and several colleagues. However, on one user's brand new system (with XP Pro and Perl v5.8.6) it won't launch the editor, instead it brings up the windows dialog for selecting which program to use. To make it flexible, I read an environment variable that contains the path to the editor. Here's the relevant bits of code: $EDITOR = $ENV{BASIC_EDITOR}; ... if ($EDITOR) { system( "start $EDITOR $file" ); } It seems that windows is seeing something like: start SomeFile with the editor path being omitted. To track down the cause of this I setup some one line test scripts we could run on this user's system. This example launches Win.ini in notepad: system( "start \"c:/program files/ultraedit/uedit32.exe\" c:/windows/WIN.ini" ); Again, somehow the program name is disappearing or being ignored. However, running this next script in "c:\program files\UltraEdit" opens Win.ini in UltraEdit like I'ld expect: system( "start uedit32.exe c:/windows/WIN.ini" ); Why would it work this way and only on one user's system?? Roy Olsen ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: system() Conundrum
Roy Olsen wrote: > I have a Perl script that receives programs and other files from a host > system for editing. Once the file is transferred the script launches my > text editor and passes it the name of the file. It works fine for me and > several colleagues. However, on one user's brand new system (with XP Pro > and Perl v5.8.6) it won't launch the editor, instead it brings up the > windows dialog for selecting which program to use. > > To make it flexible, I read an environment variable that contains the path > to the editor. Here's the relevant bits of code: > > $EDITOR = $ENV{BASIC_EDITOR}; > ... > if ($EDITOR) { >system( "start $EDITOR $file" ); > } > > It seems that windows is seeing something like: > > start SomeFile > > with the editor path being omitted. To track down the cause of this I > setup some one line test scripts we could run on this user's system. This > example launches Win.ini in notepad: > > system( "start \"c:/program files/ultraedit/uedit32.exe\" > c:/windows/WIN.ini" ); > > Again, somehow the program name is disappearing or being ignored. However, > running this next script in "c:\program files\UltraEdit" opens Win.ini in > UltraEdit like I'ld expect: > > system( "start uedit32.exe c:/windows/WIN.ini" ); > > Why would it work this way and only on one user's system?? This works fine for me (XP-Pro Perl 5.8.6 B811) : use strict; use warnings; my $EDITOR = $ENV{EDITOR} || 'D:/Util/Vim/Vim63/vim.exe'; my $file = shift || 'D:/windows/WIN.ini'; system "start $EDITOR $file"; # or also works without the start # system "$EDITOR $file"; __END__ -- ,-/- __ _ _ $Bill LuebkertMailto:[EMAIL PROTECTED] (_/ / )// // DBE CollectiblesMailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_http://dbecoll.tripod.com/ (My Perl/Lakers stuff) ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: system() Conundrum
What about using Win32::Process::Create? - Original Message - From: "Roy Olsen" <[EMAIL PROTECTED]> To: Sent: Friday, July 01, 2005 11:56 AM Subject: system() Conundrum > > I have a Perl script that receives programs and other files from a host > system for editing. Once the file is transferred the script launches my > text editor and passes it the name of the file. It works fine for me and > several colleagues. However, on one user's brand new system (with XP Pro > and Perl v5.8.6) it won't launch the editor, instead it brings up the > windows dialog for selecting which program to use. > > To make it flexible, I read an environment variable that contains the path > to the editor. Here's the relevant bits of code: > > $EDITOR = $ENV{BASIC_EDITOR}; > ... > if ($EDITOR) { >system( "start $EDITOR $file" ); > } > > It seems that windows is seeing something like: > > start SomeFile > > with the editor path being omitted. To track down the cause of this I > setup some one line test scripts we could run on this user's system. This > example launches Win.ini in notepad: > > system( "start \"c:/program files/ultraedit/uedit32.exe\" > c:/windows/WIN.ini" ); > > Again, somehow the program name is disappearing or being ignored. However, > running this next script in "c:\program files\UltraEdit" opens Win.ini in > UltraEdit like I'ld expect: > > system( "start uedit32.exe c:/windows/WIN.ini" ); > > Why would it work this way and only on one user's system?? > > Roy Olsen > > > > > > ___ > Perl-Win32-Users mailing list > Perl-Win32-Users@listserv.ActiveState.com > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs > ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs