Thread: perl question
View Single Post
Old 05-26-2011, 08:42 PM  
Barry-xlovecam
It's 42
 
Industry Role:
Join Date: Jun 2010
Location: Global
Posts: 18,083
split=/regex/ works

This looks sort of nasty but works.
I used a Firefox bookmarks file.
Some adjustments to the splits might be necessary for their regexes ...

You don't need to use some complex module to manipulate a line of text in Perl.
Perl has a very complex and efficient regex engine in its core distribution.
I am sure there are more elegant ways to regex this but this works

Code:
#!/usr/bin/perl
####################################
#bookmarkfile.cgi
#
#
#
#
####################################
use CGI::Carp qw/fatalsToBrowser/;
use CGI qw/:standard/;
use strict;
use warnings;

print "Content-type: text/html\n\n";

my $mystuff = "$ENV{'QUERY_STRING'}";
	if ($mystuff =~ s/[^a-zA-Z0-9\_]//g) {print qq~HUH???~;       exit;}

my $bookmarkfile="barry-bookmarks-6-2010.html";

open BOOKMARKFILE ,"<",$bookmarkfile or die "<bookmarkfile";

#######SAMPLE LINE
#        <DT><A HREF="http://trends.google.com/websites?q=xlovecam.com&geo=all&date=all&sort=0
#" ADD_DATE="1274745854" LAST_MODIFIED="1274745854">Google Trends for Websites: xlovecam.com</A>
#######

my @bookmarks=(<BOOKMARKFILE>);

	my @urls = grep /(http:)/, @bookmarks;

			foreach my $urls (@urls){
				my @a= split /HREF=\"/, $urls;
				my @b= split /" ADD_DATE/,$a[1];
				my @anchor1 = split />/, $a[1];
				my @anchor = split /</, $anchor1[1];

				print "$b[0]|$anchor[0]<br/>\n";
			     }
outputs:
Code:
http://trends.google.com/websites?q=xlovecam.com&geo=all&date=all&sort=0|Google Trends for Websites: xlovecam.com

Last edited by Barry-xlovecam; 05-26-2011 at 08:48 PM..
Barry-xlovecam is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote