Closed Thread
Results 1 to 6 of 6

Thread: Need some help with my script

  1. #1
    John_L is offline Newbie
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Need some help with my script

    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/&lt;/</;
    		$information =~ s/&gt;/>/;
    		$information =~ s/&amp;/&/;
    					
    		return $information
    		
    	}
    }
    
    print "\n\t</body>\n";
    print "\n</html>\n\n";

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    John_L is offline Newbie
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Re: Need some help with my script

    never mind...i know what was wrong.

  4. #3
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42

    Re: Need some help with my script

    Can you explain what was wrong? Other users might have the same problem as you had.

  5. #4
    John_L is offline Newbie
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Re: Need some help with my script

    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.

  6. #5
    jmcallister is offline Newbie
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Re: Need some help with my script

    Thank you for sharing the issue with us this actually helped me with a build perl script that i have been having problems with.

  7. #6
    Nipa is offline Newbie
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Re: Need some help with my script

    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,

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [HELP] convert Perl script to Bash shell script
    By Egypte in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 04-24-2011, 05:37 PM
  2. In need of a script
    By lewje007 in forum PHP Development
    Replies: 5
    Last Post: 11-26-2009, 01:25 PM
  3. PHP script writing a PHP script
    By ezcat in forum PHP Development
    Replies: 6
    Last Post: 03-08-2009, 04:47 AM
  4. What is the difference between VB script and Java script?
    By newage123 in forum JavaScript and CSS
    Replies: 8
    Last Post: 11-14-2008, 10:49 AM
  5. Need Help With a Script
    By tfusion in forum PHP Development
    Replies: 5
    Last Post: 04-14-2007, 05:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts