Re: Printing to a file while socket is open..
Craig S Monroe wrote: Hello All, I am a newbie to the list and have a question. I have searched for an answer through the docs, but cannot seem to locate a resolution. I have written a script that opens a socket to a particular device, and issues it commands. I would then like it to write the results to a file, wait, then issue the commands again and append the results to the same file. My issue: I am having difiiculty writing the output to a file. The file is created, but nothing is written and the script hangs. It worked previously, when I had the script close the socket, then write the results, but I need to keep it open. I have really hacked up the script(indecision on items) so please look past the rubble. It will be cleaned up when it is done. I have enclosed the portion of the script. The part I am having difficulty with is toward the end when printing to DATAFILE. ... open(DATAFILE,$dataFile)|| die cannot append to $dataFile : $!; You could unbuffer the output to the file here (the below maintains STDOUT as the selected FH): select ((select (DATAFILE), $| = 1)[0]); ... -- ,-/- __ _ _ $Bill Luebkert ICQ=14439852 (_/ / )// // DBE Collectibles http://www.todbe.com/ / ) /-- o // // Mailto:[EMAIL PROTECTED] http://dbecoll.webjump.com/ -/-' /___/__/_/_http://www.freeyellow.com/members/dbecoll/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: directory again
Does anyone know where I can find information on passing parameters (flags) on the command line? thank you tanya -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Thursday, May 31, 2001 9:13 AM To: '[EMAIL PROTECTED]' Subject: Re: directory again Hi, I need to be able to recursively go through a directory and if i encounter a file folder, perform the same actions on the files in that file folder. is there a simple way to do this, like with an if-statement? thanks tanya graham Tanya, Please try to avoid replying with someone else's post, and just changing the subject, it's confusing. Everyone here seems to have gone far out of their way to respond to your earlier directory dilemma, including me, so allow me to now voice a few suggestions. From your previous post, and this one I think it's fair to assume you are very new to perl. The reason I like this mailing list so much is that they are very kind to newbies, and I've never seen a flame war on this list. Being new, it's often easy to not even know where the documentation is. First, check the FAQ's, they are installed in HTML format when you install ActiveState perl, many easy problems can be resolved right there. Many people in response to your previous post pointed towards perldoc (perldoc -f opendir ) as a tool to find information about function and module usage. Another tool is PPM, which you can use to search for modules and install them from ActiveState's (or any other ) ppm repository. Often it's good to run a search for what you're looking for through there (such as dir or file in your case), install a few likely sounding packages and then run perldoc on them to see if they offer the features you are really looking for. You can type help in ppm for usage help, and perldoc perldoc will give you more than you ever wanted to know about how to use perldoc. Also look at the O'Reilly series of perl books, as they are invaluable resources. You can find info on them at www.perl.com. I apoligize to all for this long post, but while I think none of us has a problem helping, I personally have a problem when someone doesn't help themself first. To answer your current question, you may want to take a look at the File::Find module as this will recurse through a directory tree and can perform a specified callback (subroutine) on each file it finds. Sorry for the long post, Chuck ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
regular expression question
Dear all, I have a question about a regular expression, for example $OK = c:\\temp\\test\\test1\\test2; How can I do to make me get $OK1 = c:\\temp\\test\\test1; That means I do not want the last part of the directory. Thanks in advance! Lixin ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: regular expression question
$OK = c:\\some\\dir\\file; $OK =~ /(.*)(\\.*)$/; $OK1 = $1; ...of course it isn't portable across operating systems, and it assumes that filenames can't contain \. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of [EMAIL PROTECTED] Sent: Thursday, May 31, 2001 4:17 PM To: [EMAIL PROTECTED] Subject: regular expression question Dear all, I have a question about a regular expression, for example $OK = c:\\temp\\test\\test1\\test2; How can I do to make me get $OK1 = c:\\temp\\test\\test1; That means I do not want the last part of the directory. Thanks in advance! Lixin ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: directory again
Unix: man perlrun Windows: Open the perl documentation and read the section titled perlrun. Both: Take two aspirin. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Tanya Graham Sent: Thursday, May 31, 2001 4:02 PM To: '[EMAIL PROTECTED]' Subject: RE: directory again Does anyone know where I can find information on passing parameters (flags) on the command line? thank you tanya -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Thursday, May 31, 2001 9:13 AM To: '[EMAIL PROTECTED]' Subject: Re: directory again Hi, I need to be able to recursively go through a directory and if i encounter a file folder, perform the same actions on the files in that file folder. is there a simple way to do this, like with an if-statement? thanks tanya graham Tanya, Please try to avoid replying with someone else's post, and just changing the subject, it's confusing. Everyone here seems to have gone far out of their way to respond to your earlier directory dilemma, including me, so allow me to now voice a few suggestions. From your previous post, and this one I think it's fair to assume you are very new to perl. The reason I like this mailing list so much is that they are very kind to newbies, and I've never seen a flame war on this list. Being new, it's often easy to not even know where the documentation is. First, check the FAQ's, they are installed in HTML format when you install ActiveState perl, many easy problems can be resolved right there. Many people in response to your previous post pointed towards perldoc (perldoc -f opendir ) as a tool to find information about function and module usage. Another tool is PPM, which you can use to search for modules and install them from ActiveState's (or any other ) ppm repository. Often it's good to run a search for what you're looking for through there (such as dir or file in your case), install a few likely sounding packages and then run perldoc on them to see if they offer the features you are really looking for. You can type help in ppm for usage help, and perldoc perldoc will give you more than you ever wanted to know about how to use perldoc. Also look at the O'Reilly series of perl books, as they are invaluable resources. You can find info on them at www.perl.com. I apoligize to all for this long post, but while I think none of us has a problem helping, I personally have a problem when someone doesn't help themself first. To answer your current question, you may want to take a look at the File::Find module as this will recurse through a directory tree and can perform a specified callback (subroutine) on each file it finds. Sorry for the long post, Chuck ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: directory again
i found it...for some reason since i couldn't find parameters in the index i freaked out, and after a few minutes i realized i could look up arguments...been a long day... tanya -Original Message- From: Trever Furnish [mailto:[EMAIL PROTECTED]] Sent: Thursday, May 31, 2001 2:33 PM To: Tanya Graham; [EMAIL PROTECTED] Subject: RE: directory again Unix: man perlrun Windows: Open the perl documentation and read the section titled perlrun. Both: Take two aspirin. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Tanya Graham Sent: Thursday, May 31, 2001 4:02 PM To: '[EMAIL PROTECTED]' Subject: RE: directory again Does anyone know where I can find information on passing parameters (flags) on the command line? thank you tanya -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Thursday, May 31, 2001 9:13 AM To: '[EMAIL PROTECTED]' Subject: Re: directory again Hi, I need to be able to recursively go through a directory and if i encounter a file folder, perform the same actions on the files in that file folder. is there a simple way to do this, like with an if-statement? thanks tanya graham Tanya, Please try to avoid replying with someone else's post, and just changing the subject, it's confusing. Everyone here seems to have gone far out of their way to respond to your earlier directory dilemma, including me, so allow me to now voice a few suggestions. From your previous post, and this one I think it's fair to assume you are very new to perl. The reason I like this mailing list so much is that they are very kind to newbies, and I've never seen a flame war on this list. Being new, it's often easy to not even know where the documentation is. First, check the FAQ's, they are installed in HTML format when you install ActiveState perl, many easy problems can be resolved right there. Many people in response to your previous post pointed towards perldoc (perldoc -f opendir ) as a tool to find information about function and module usage. Another tool is PPM, which you can use to search for modules and install them from ActiveState's (or any other ) ppm repository. Often it's good to run a search for what you're looking for through there (such as dir or file in your case), install a few likely sounding packages and then run perldoc on them to see if they offer the features you are really looking for. You can type help in ppm for usage help, and perldoc perldoc will give you more than you ever wanted to know about how to use perldoc. Also look at the O'Reilly series of perl books, as they are invaluable resources. You can find info on them at www.perl.com. I apoligize to all for this long post, but while I think none of us has a problem helping, I personally have a problem when someone doesn't help themself first. To answer your current question, you may want to take a look at the File::Find module as this will recurse through a directory tree and can perform a specified callback (subroutine) on each file it finds. Sorry for the long post, Chuck ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: directory again
$#ARGV contains the number of arguments minus one in @ARGV, i.e. the first argument is $ARGV[0]. Sample code: # Check that the required number of arguments have been passed. if ($#ARGV 0) { Help(); $InputFile = GetUserInput('Enter the input filename',0); } else {$InputFile = $ARGV[0];} # Check for optional second argument. if ($#ARGV 0) {$NbrPages = $ARGV[1];} # Check for optional third argument. if ($#ARGV 1) {$Width = $ARGV[2];} else {$Width = 80} In the above code, if no arguments were entered on the command line, the Help() function will be called, else the $InputFile will be assigned the first argument. If there is more than one argument, $NbrPages will be assigned the second argument. If there is more than two arguments, $Width will be assigned the third argument, etc. For options (flags) such as -d, check out the Getopt module. For example: use Getopt::Std; # Check if the -h option was entered. our $opt_h; getopts('h'); # If the -h option was entered, print the help and exit the program. if (defined $opt_h) { Help(); exit(1); } In the above code, it is checking for a -h option to be entered. Note that you must declare a variable for each option. So if we use -d and -h as options, we would need to change the our statement above to be something like: our ($opt_d,$opt_h) You can then simply test if they are defined to determine whether or not they have been entered. All of this information is in the Camel book, if you don't have it, get it, if you have it, please read it. Dirk Bremer - Systems Programmer II - AMS Department - NISC 636-922-9158 ext. 652 fax 636-447-4471 mailto:[EMAIL PROTECTED] - Original Message - From: Tanya Graham [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, May 31, 2001 4:02 PM Subject: RE: directory again Does anyone know where I can find information on passing parameters (flags) on the command line? thank you tanya -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Thursday, May 31, 2001 9:13 AM To: '[EMAIL PROTECTED]' Subject: Re: directory again Hi, I need to be able to recursively go through a directory and if i encounter a file folder, perform the same actions on the files in that file folder. is there a simple way to do this, like with an if-statement? thanks tanya graham Tanya, Please try to avoid replying with someone else's post, and just changing the subject, it's confusing. Everyone here seems to have gone far out of their way to respond to your earlier directory dilemma, including me, so allow me to now voice a few suggestions. From your previous post, and this one I think it's fair to assume you are very new to perl. The reason I like this mailing list so much is that they are very kind to newbies, and I've never seen a flame war on this list. Being new, it's often easy to not even know where the documentation is. First, check the FAQ's, they are installed in HTML format when you install ActiveState perl, many easy problems can be resolved right there. Many people in response to your previous post pointed towards perldoc (perldoc -f opendir ) as a tool to find information about function and module usage. Another tool is PPM, which you can use to search for modules and install them from ActiveState's (or any other ) ppm repository. Often it's good to run a search for what you're looking for through there (such as dir or file in your case), install a few likely sounding packages and then run perldoc on them to see if they offer the features you are really looking for. You can type help in ppm for usage help, and perldoc perldoc will give you more than you ever wanted to know about how to use perldoc. Also look at the O'Reilly series of perl books, as they are invaluable resources. You can find info on them at www.perl.com. I apoligize to all for this long post, but while I think none of us has a problem helping, I personally have a problem when someone doesn't help themself first. To answer your current question, you may want to take a look at the File::Find module as this will recurse through a directory tree and can perform a specified callback (subroutine) on each file it finds. Sorry for the long post, Chuck ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: directory again
-Original Message- Behalf Of Dirk Bremer $#ARGV contains the number of arguments minus one in @ARGV, i.e. the first argument is $ARGV[0]. Sample code: # Check that the required number of arguments have been passed. if ($#ARGV 0) { Help(); $InputFile = GetUserInput('Enter the input filename',0); } else {$InputFile = $ARGV[0];} # Check for optional second argument. if ($#ARGV 0) {$NbrPages = $ARGV[1];} # Check for optional third argument. if ($#ARGV 1) {$Width = $ARGV[2];} else {$Width = 80} Alternatively... my($InputFile, $NbrPages, $Width) = @ARGV; # Check that the required number of arguments have been passed. if (!defined $InputFile or $InputFile eq ) { Help(); $InputFile = GetUserInput('Enter the input filename',0); } # Check for optional second argument (make sure it is undef.) ($NbrPages = undef) if (defined $NbrPages and $NbrPages eq ); # Check for optional third argument. ($Width = 80) if (!defined $Width or $Width eq ); ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: regular expression question
No regex in answer here but... Is this what you want? use File::Basename; $OK = c:\\temp\\test\\test1\\test2; $OK1 = dirname($OK); print dir of $OK is $OK1\n; - Original Message - From: [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, May 31, 2001 5:17 PM Subject: regular expression question Dear all, I have a question about a regular expression, for example $OK = c:\\temp\\test\\test1\\test2; How can I do to make me get $OK1 = c:\\temp\\test\\test1; That means I do not want the last part of the directory. Thanks in advance! Lixin ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
sockets problem
In the script below when I send a START msg to the socket it is successfully received. When I send a STOP msg it is not. What am I doing wrong here? James use strict; use IO::Socket; my $client; my $server_port = 5010; my $server = IO::Socket::INET-new(LocalPort = $server_port, Type = SOCK_STREAM, Reuse = 1, Listen= 5 ) or die Couldn't be a tcp server on port $server_port : $@\n$!\n; while ( $client = $server-accept() ) { if ( $client =~ /START/ ){ msgBox(Starting client...); logEvent(Starting client...); } elsif ( $client =~ /STOP/ ) { msgBox(Stopping client...); logEvent(Stopping client...); } } ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: regular expression question
Not really a regex approach but here is my submittion: $OK = c:\\temp\\test\\test1\\test2; $your_answer = substr($OK, 0, rindex($OK, \\)); # print $your_answer,\n; -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Thursday, May 31, 2001 2:17 PM To: [EMAIL PROTECTED] Subject: regular expression question Dear all, I have a question about a regular expression, for example $OK = c:\\temp\\test\\test1\\test2; How can I do to make me get $OK1 = c:\\temp\\test\\test1; That means I do not want the last part of the directory. Thanks in advance! Lixin ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: sockets problem
-Original Message- Behalf Of James McDermet In the script below when I send a START msg to the socket it is successfully received. When I send a STOP msg it is not. What am I doing wrong here? James use strict; use IO::Socket; my $client; my $server_port = 5010; my $server = IO::Socket::INET-new(LocalPort = $server_port, Type = SOCK_STREAM, Reuse = 1, Listen= 5 ) or die Couldn't be a tcp server on port $server_port : $@\n$!\n; while ( $client = $server-accept() ) { if ( $client =~ /START/ ){ msgBox(Starting client...); logEvent(Starting client...); } elsif ( $client =~ /STOP/ ) { msgBox(Stopping client...); logEvent(Stopping client...); } } Only a guess... # Extract something from a stream if ( $client =~ /START/ ){ ... # Extract it again (it was already extracted) } elsif ( $client =~ /STOP/ ) { ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: sockets problem
James McDermet wrote: In the script below when I send a START msg to the socket it is successfully received. When I send a STOP msg it is not. What am I doing wrong here? James use strict; use IO::Socket; my $client; my $server_port = 5010; my $server = IO::Socket::INET-new(LocalPort = $server_port, Type = SOCK_STREAM, Reuse = 1, Listen= 5 ) or die Couldn't be a tcp server on port $server_port : $@\n$!\n; while ( $client = $server-accept() ) { if ( $client =~ /START/ ){ msgBox(Starting client...); logEvent(Starting client...); } elsif ( $client =~ /STOP/ ) { msgBox(Stopping client...); logEvent(Stopping client...); } } You're doing 2 reads on the socket instead of 1, try more like: while ($client = $server-accept()) { $_ = $client; # save data in $_ if (/START/) { msgBox (Starting client...); logEvent (Starting client...); } elsif (/STOP/) { msgBox (Stopping client...); logEvent (Stopping client...); } } -- ,-/- __ _ _ $Bill Luebkert ICQ=14439852 (_/ / )// // DBE Collectibles http://www.todbe.com/ / ) /-- o // // Mailto:[EMAIL PROTECTED] http://dbecoll.webjump.com/ -/-' /___/__/_/_http://www.freeyellow.com/members/dbecoll/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
lag test #2 posted 6:44 PM PDT 5/31/2001
Test message; my last lag test message was delivered to the list two days after it was posted. -Bennett [EMAIL PROTECTED] http://www.peacefire.org (425) 649 9024 ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
win32::eventlog on remote machine
Using the following code right out of the ActiveState docs works to read my own computer event log, but when I run it against a remote computer (replacing VIC20 with servername) it returns nothing, just executes and ends with no output or error... I am checking remote NT4 logs from a Win2K server... could that be the problem?: c ** use Win32::EventLog; $handle=Win32::EventLog-new(System, VIC20) or die Can't open Application EventLog\n; $handle-GetNumber($recs) or die Can't get number of EventLog records\n; $handle-GetOldest($base) or die Can't get number of oldest EventLog record\n; while ($x $recs) { $handle-Read(EVENTLOG_FORWARDS_READ|EVENTLOG_SEEK_READ, $base+$x, $hashRef) or die Can't read EventLog entry #$x\n; if ($hashRef-{Source} eq EventLog) { Win32::EventLog::GetMessageText($hashRef); print Entry $x: $hashRef-{Message}\n; } $x++; } ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: Mail::Mailer
Do perl -MCPAN -e 'install Mail::Mailer' This will get it off CPAN for you. David Hello guys, Does anyone have a copy of Mail::Mailer they can email me. Or if their is an equivalent please let me know. Thanks alot, Mark Bergeron A Duck! ___ GO.com Mail Get Your Free, Private E-mail at http://mail.go.com ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
AW: (no subject)
Ah! It starts again! I german; my is english learn also better -Ursprüngliche Nachricht- Von: VeeraRaju_Mareddi [SMTP:[EMAIL PROTECTED]] Gesendet am: Donnerstag, 31. Mai 2001 12:34 An: '[EMAIL PROTECTED]'; '[EMAIL PROTECTED]'; '[EMAIL PROTECTED]' Betreff: (no subject) Hi Everybody I wanna scan some specific ports on a remote machine(25th Port), Whether they are active in state or not. Please provide me basic idea for this to be worked out. If anybody of u have idea ,concept please let me. I want to this to be Done through perl script. Ur help would be grateful Regards Raju ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Port Scanning (was RE: (no subject))
Please check the archive of perl-win32-users : this was asked and answered yesterday! Please don't cross post to so many groups: this reply to so many is just to save others time. lee ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Round number
Hi! I'm getting some information from my database and after some mathematical operations i get a decimal number and i would like to round it...how can i do that? For ex: if i have: 3,2 i want 3 3,5 i want 4 thanx A. Vasconcelos ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: Round number
I'm getting some information from my database and after some mathematical operations i get a decimal number and i would like to round it...how can i do that? A lot of people will say int! int!, but you can find a more powerful routine called round at the URL below. It allows you to choose what place you'd like to round a number to. http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/sbeating/sb/sb-lib.pl?rev=1.11content-type=text/vnd.viewcvs-markup Morbus Iff .sig on other machine. http://www.disobey.com/ http://www.gamegrene.com/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: Round number
If you want to simply round to the nearest integer, then do $number = int($number + 0.5) Therefore, n.4999... and below become n while n.5... and above become n+1. The only problem with this simple fix is that it is biased because slightly more numbers will rounded up to the next higher value than are rounded down. Merrill -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Thursday, May 31, 2001 8:58 AM To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: Round number Hi! I'm getting some information from my database and after some mathematical operations i get a decimal number and i would like to round it...how can i do that? For ex: if i have: 3,2 i want 3 3,5 i want 4 thanx A. Vasconcelos ___ Perl-Win32-Database mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-database ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: Round number
Have you seen the docs? If yes, please ignore; otherwise: perldoc -q round:- Does Perl have a round() function? What about ceil() and floor()? Trig functions? Remember that int() merely truncates toward 0. For rounding to a certain number of digits, sprintf() or printf() is usually the easiest route. printf(%.3f, 3.1415926535); # prints 3.142 The POSIX module (part of the standard Perl distribution) implements ceil(), floor(), and a number of other mathematical and trigonometric functions. use POSIX; $ceil = ceil(3.5);# 4 $floor = floor(3.5); # 3 In 5.000 to 5.003 Perls, trigonometry was done in the Math::Complex module. With 5.004, the Math::Trig module (part of the standard Perl distribution) implements the trigonometric functions. Internally it uses the Math::Complex module and some functions can break out from the real axis into the complex plane, for example the inverse sine of 2. Rounding in financial applications can have serious implications, and the rounding method used should be specified precisely. In these cases, it probably pays not to trust whichever system rounding is being used by Perl, but to instead implement the rounding function you need yourself. To see why, notice how you'll still have an issue on half-way-point alternation: for ($i = 0; $i 1.01; $i += 0.05) { printf %.1f ,$i} 0.0 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1.0 1.0 Don't blame Perl. It's the same as in C. IEEE says we have to do this. Perl numbers whose absolute values are integers under 2**31 (on 32 bit machines) will work pretty much like mathematical integers. Other numbers are not guaranteed. ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users