SPUG: mkfavhtm.pl -- Converting IE favorites to HTM (unix/mozilla users just ignore me)

John W. Krahn krahnj at telus.net
Tue Jul 11 16:21:07 PDT 2006


Aaron West wrote:
> Hardly spug-relevant, and half+ of you guys probably are mozilla users, but
> here goes...

Another couple of points that may help your code.  :-)

[snip]

> 	my $url = undef;
> 	while ( defined( my $line = <FH> ) ) {
> 	    if ( $line =~ /^URL=([^\r\n]*)/ ) {
> 		$url = $1;
> 	    }
> 	}

If your file contains more then one line matching the expression
/^URL=([^\r\n]*)/ then $url will receive the contents of the last match.
If, as I suspect, there is only one line that matches you can exit the loop
early (perldoc -f last) and avoid reading the whole file.  This of course is
moot if the file only contains a few lines or the 'URL=' line is the last line
in the file.


> 	close FH;
> 	$filename =~ s/\.url$//;
> 	$url{ $mtime, $url, $filename }++ 
> 	    if defined $url;
>     }
> }
> 
> for ( sort keys %url ) {

Suppose that you have two files:

Name     Mtime
file1    10000234
file2    9987654

sort() will sort 'file1' before 'file2'.  If you want the output in correct
mtime order you will either have to sort using a numerical comparison or
convert the mtime field so that it can be sorted correctly as text.


John
-- 
use Perl;
program
fulfillment


More information about the spug-list mailing list