|
||||||
| Perl Discussion for the PERL language - Practical Extraction and Reporting Language, is a programming language often used for creating CGI programs. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Basic XML parser. I noted an "EMPTY???" comment beside the area which doesn't print anything. It's strange because the variables do get filled properly...anyone have any ideas?
Code:
#!/usr/bin/perl -w
my $title = ""; #title
my $link = ""; #link
my $description = ""; #description
my $item_found = 0; # <item> element within the xml file
print "<html>\n";
print "\n\t<body>\n";
#read xml file from standard input
while( $line = <stdin> )
{
#remove white space
chomp( $line );
if( $item_found == 0 )
{
$start = index( $line, "item" ); #find an item tag
if( $start != -1 )
{
$item_found = 1; #beginning of item
}
}
if( $item_found == 1 )
{
#parse data that we want
$title = &process_element( $line, "title" );
$link = &process_element( $line, "link" );
$description = &process_element( $line, "description" );
#look for the end of item tag
$end = index( $line, "/item" );
if( end != -1 )
{
#print the gathered information to the html file
if( length( $title ) != 0 || length( $link ) != 0 || length( $description ) != 0 )
{
print "\n\t\t<p> \n\t \t\t<a href= \"$link\"> $title </a> \n\t\t\t $description \n\t\t</p>\n\n" ; #EMPTY????
}
#clear stored information
$title = "";
$link = "";
$description = "";
$chann_found = 0; #end of channel
$item_found = 0; #end of item
}
}
}
#$_[0] = line, $_[1] = tag
sub process_element
{
#my ( $start, $end, $length ); #private local variables
#my $information = "";
#Grab the string between these tags
$start = index( $_[0], "$_[1]" ); #assume this is how a beginning tag will always look
$end = rindex( $_[0], "</" ); #this is how an ending tag will always look
if( $start != -1 )
{
$start = index( $_[0], ">" ) + 1; #advance start to the end of the tag
$end = rindex( $_[0], "<" ); #advance start to the beginning of the closing tag
$length = length( $_[0] );
#end tag wasn't on the same line
if( $end == -1 )
{
$end = $length;
}
$length = $end - $start; #the length of the substring
$information = substr( $_[0], $start, $length );
#replace encoded characters if any exist
$information =~ s/</</;
$information =~ s/>/>/;
$information =~ s/&/&/;
return $information
}
}
print "\n\t</body>\n";
print "\n</html>\n\n";
|
| Sponsored Links |
|
|
|
|||||
|
Can you explain what was wrong? Other users might have the same problem as you had.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
|
|||
|
no problem. In subroutines a return value is not necessary, but you can include one. It doesn't follow the same structure as high level language though, notice how mine is inside an if statement, so it could in fact never execute, leaving my subroutine without a return value (not allowed in many languages). That was the issue, on certain cases the variables assigned to the subroutine value returned ended up either equaling nothing or whatever may be equivalent to void. So my variables would equal the correct value, but following another execution of the subroutine given a case where the return statement wasn't executed, they were overridden. I hope that makes sense....and is understandable. I just threw in a "error check" if statement to correct the problem.
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| DNS script - required! | Walls | Request Services (Paid) | 0 | 02-11-2008 02:43 AM |
| Hacking Perl Script | falco85 | Perl | 5 | 11-12-2007 04:51 PM |
| JavaScript:Tutorial, Using an External Script | TcM | Javascript | 7 | 09-11-2007 07:39 AM |
| (Script) Copy content to clipboard, how? | annannienann | Visual Basic Programming | 0 | 06-19-2007 05:20 PM |
| John | ........ | 223.00000 |
| dargueta | ........ | 168.00000 |
| Xav | ........ | 164.00000 |
| LogicKills | ........ | 20.00000 |
| gaylo565 | ........ | 18.00000 |
| WingedPanther | ........ | 15.00000 |
| |pH| | ........ | 15.00000 |
| Johnnyboy | ........ | 3.00000 |
| navghost | ........ | 1.00000 |
Goal: 100,000 Posts
Complete: 67%