Solarum – Information For Everyone

Archive for the ‘PERL’ Category

Nov
6

Update to the PERL round function

Mr. Thierry H. was kind enough to post a comment with a nice little mod to the PERL round function I posted a while back. With his addition, you can now specify how many digits to show on the right of the decimal. Pretty neat, check it out.

Sep
20

PERL Tip For Data In Arrays

Recently I was working on a script for log reporting.  You know, one of those handy little guys that send you some info every day helping to make sure you keep up with whatever it is that you don’t want to forget about.  Well, some of the data was in a plain old text file, and there is nothing wrong with that.  It’s easy to simply cat the file and pipe it through mailx or mutt, no fuss, no muss.

Read the rest of this entry »

May
29

Generate Random Unique Number

I have found many times when writing stuff in PHP that I have come across a need to generate a random number. That’s not too bad in and of itself, but what about when you need a unique ID key or something like that. For example, say you are writing the next best helpdesk application and need unique case numbers and don’t want to simply use a counter. For whatever reason, I put this little function together and it has served me well. Check it out …

Read the rest of this entry »

May
23

PERL Trim Functions

PERL is a wonderful scripting language, it is extremely powerful, flexible and portable. It also lacks a couple basic functions that other languages have built in. Fear not my friend, just like the PERL round function, I have functions for other things as well!

One thing I miss is a trim function. They have chop and chomp, but that doesn’t always fit what I want. Below I have included a few suns that will get the job done nicely, check it out. Read the rest of this entry »

Apr
27

Great stuff at PortableApps

In this post, I’d like to do two things.  First off, I want to plug a really cool site called PortableApps.com that has some really cool software in the form of … well, portable apps.  What these are, are common widely used applications that have been transformed in such a way that they can run right off of your thumb drive, no install necessary, hence the term portable.  They have lots of cool stuff that you can download, absolutely free, and use right off your thumb drive, or hard drive, or anywhere really.  It’s nice being able to have firefox and open office (and much more) with you, no matter where you go, even with all of your own settings and customizations.  That’s hard to beat!  Go check it out, you won’t be disappointed I am sure. Read the rest of this entry »

Mar
16

PERL Round Function

Hey all your PERL junkies like me, I have a present for you. Anyone that has done any coding at all other PERL will know (and miss) the round function that most other languages have built in. For those that may not know, the round function lets you do just that, round a number to the specified digit. So, instead of having to use 3.14159265 as an answer for a particular equation, you could round it to 3.14. Nice, huh?

Well, here is a neato little round function that you can drop into your PERL scripts and call to actually round numbers instead of cutting them off with ceil or cut. Check it out:

sub round {
  my($number) = shift;
  return int($number + .5 * ($number <=> 0));
}

There you go!

**Update**
Thanks to Thierry H. for adding a little mod to the round function allowing you to specify the number of decimals to print. Here is the modified function:

sub round {
  my $number = shift || 0;
  my $dec = 10 ** (shift || 0);
  return int( $dec * $number + .5 * ($number <=> 0)) / $dec;
}

You would call it by giving not only the number to round, but how many numbers to show on the right side of the decimal. It will look like so:

$result = round(123.4567,3);

This should return 123.457 (the 6 in the third slot gets rounded to 7). There ya go, thanks for the mod Thierry! You can check out Thierry’s site here.

Pages

Articles