I have the following Perl snippet:
The value of $url is not important at this stage, any webpage will do. This code downloads a webpage and the filename is the current date and time (see link removed). I am concerned about the addleadingzero function, because it is called often and I would prefer to use map(), but that is impossible because of year ($dt[5]). How can I make this code more efficient/terse?
Thanks in advance
Last edited by radoulov; 07-27-2008 at 04:18 PM..
Reason: please don't post links to illegal sites
1) Write addleadingzero() to handle an array and pass all the values (except year) to it. Return the array, then use it, eg
my (@myarray) = ($dt[4], $dt[3], ... etc);
my $outfilename = addleadingzero(@myarray); #returns @result, elements all properly padded....
$outfilename = $year . $result[4] . $result[3] . $result[2] .... etc.
you call addleadingzero() only once for each web-page...
2) You could try setting the year separate from the rest (different variable) then use map on the rest of them, concatenate at the end... More steps, but you cutout addleadingzero() altogether. You'd have to benchmark both and see which is faster....
3) Instead of checking numeric value of $gmtime[...] test it's length() as string and pad with "0", etc. Same as you are doing, but I suspect this one would be slower unless perl's string parsing is more efficient than its numeric... Ya just never know!!!
4) You could use map on all of them... I think you can do stuff like...
(@dt) = ($gmtime[5], $gmtime[4], ... etc, the ones you want);
(@result) = map { if (length($_) < 3 {
#process all but year
} else {
# process year
} } @dt;
Last edited by quine; 07-27-2008 at 03:59 PM..
Reason: forgot assignment to $outfilename
The function should do all the date formatting and return the string ready to use. And don't use concatenation when simple string constructors will suffice.
Note that the link in your post is to a well known pirate website. Please do not post links to that website anymore. Thank you.
Thank you for your suggestions. Ran a few experiments and came up with the following, which at least meets the terseness standard:
It requires use POSIX qw(strftime);
Note that this solution does not require the use of the subroutine addleadingzero.
Last edited by figaro; 07-27-2008 at 04:39 PM..
Reason: Added reasoning
Hello, there.
I'm a new beginner to Linux kernel and curious about its memory management.
When multiple applications apply for memory space at the same time, how Linux kernel solve the resource contending problem for high performance?
I have known that there is a buddy system for allocating and... (4 Replies)
I've got this program set up so that it creates files whose unique names specify the jobs their contents describe. In order to retrieve the information inside those files, I have to do a "grep" and awk or sed to extract it. I've just assumed that making a directory with that unique name that... (1 Reply)
Hello Everyone!
I am a newbie. I'd like to get key lines from a big txt file by Reg Exp, The file is nearly 22MB.
GREP or SED?which may be the best choice,more efficient way?
or any other best practise?
Thank you in advance.
Ever:) (5 Replies)
Hi !! I've finished an awk exercise. Here it is:
#!/bin/bash
function calcula
{
# Imprimimos el mayor tamaño de fichero
ls -l $1 | awk '
BEGIN {
max = $5; # Inicializamos la variable que nos guardará el máximo con el tamaño del primer archivo
}
{
if ($5 > max){ #... (8 Replies)
how efficient is it, and how practical is it to call outside programs in a shell script (bash) for small tasks?
for example, say i have a script that might preform many tasks, one of those tasks may require root access; rather than implementing inside the script a method to use su or sudo to... (11 Replies)