CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
understanding program

 



vamsi.valiveti
New User

Nov 8, 2014, 9:08 AM

Post #1 of 2 (2047 views)
understanding program Can't Post

 can anybody explain what the program will do inside the while and foreach loop especially split and count part in below program?


#!/usr/bin/perl
$count = 0;
$inputfilename=$ARGV[0];
$outputfilename=$ARGV[0]."_temp";
open ($INFILE, '<', $inputfilename) || die "Cannot open file for input $inputfilename: $!" ;
open ($OUTFILE, '>', $outputfilename) || die "Cannot open file for output $outputfilename: $!";
while (my $curline = <$INFILE>)
{
my @lines = split /\<\/Item\>/, $curline;
foreach my $line (@lines)
{
if (index($line, "<Item ") != -1)
{
$count = $count + 1;
$line =~ s/\<Item /\<Item itemKey\=\"$count\" /;
print $OUTFILE $line."</Item>";
}
else
{
print $OUTFILE $line;
}
}
print $OUTFILE "\n";
}
close ($FILE);


BillKSmith
Veteran

Nov 8, 2014, 5:44 PM

Post #2 of 2 (2040 views)
Re: [vamsi.valiveti] understanding program [In reply to] Can't Post

This program assumes that the input consists of a special subset of HTML. It splits each line into item tags. In each tag, it adds (after the attribute 'Item') the attribute 'ItemKey=count" (where count is the number of replacements made so far.

In general this is a very poor way to edit HTML. It probably works ok as long as the input truly conforms to the expected format.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives