Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Friday, September 06, 2013

Interesting fact about finding perl array size

Recently, I came across a problem with Perl array. I was assuming that these three methods generate same output for finding array size, but I am WRONG :

my @newArray = (2);
print scalar @newArray;   #1
print $#newArray; #2
print @newArray; #3


First and third give me proper values, but second gave me 0 size. So be careful, while using it. I would recommend using "scalar @newArray".

Here is some explanation on it : http://stackoverflow.com/questions/7406807/find-size-of-array-in-perl

Cheers.



Tuesday, August 27, 2013

Cannot insert perl array in database

I recently came across a problem where my perl array was generating some random value in MySql query. I finally figured out that, you have to convert perl array to string first and then can insert in the database. Here is how you can do it:

my @arrayData = ("aaa","bbb","ccc");
my $ArrayString = join " ", @arrayData;

use above variable in MySql query.

Cheers. 

Thursday, May 23, 2013

Perl display time and date

To display time and date in perl use:

use POSIX qw(strftime);
print strftime("%a, %d %b %Y %H:%M:%S %z", localtime(time())) . "\n";

Note: you might need to install date time module.  sudo apt-get install libdatetime-perl