http://qs321.pair.com?node_id=794307

dmsparts has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that parses XML data with details of vehicles into many CSV files(one for each vehicle). i Have posted here before when i was developing the script. it has been working faultlessly until the other day when there was only one vehicle in the XML file. I cannot work out why it wont work, can anyone help. Here is a copy of the script

#!/usr/bin/perl use XML::Simple; print "Content-type: text/html\n"; print "\n"; ######XML PARSE########## $ipath = "e:/cars/"; $str = ".xml"; opendir THEDIR, "$ipath"; @xmlfile = grep (/$str/, readdir THEDIR); closedir THEDIR; foreach $file (@xmlfile){ $xml = $ipath.$file; } if (lc($xml) =~ "don"){&doncaster} else { if (lc($xml) =~ "ayl"){$holding = "asmholding"}; if (lc($xml) =~ "cartrans"){$holding = "ctpholding"}; if (lc($xml) =~ "hills"){$holding = "hilholding"}; if (lc($xml) =~ "windley"){$holding = "wlsholding"}; if (lc($xml) =~ "ppm"){$holding = "ppmholding"}; print "Other Cars<br>"; $config = XMLin( $xml, SuppressEmpty => "" ); $exportid = "$config->{Header}->{ExportID}"; $numveh = "$config->{Summary}->{NumberOfVehicles}"; $memid = "$config->{Header}->{MemberID}"; $lineno = 10; $vehicleno = 1; foreach $vehicle ( @{ $config->{'Vehicle'} } ) { $auctionid = "$vehicle->{AuctionID}"; $make = "$vehicle->{Manufacturer}"; $model = "$vehicle->{Model}"; $reg = "$vehicle->{RegNo}"; $year = "$vehicle->{RegYear}"; $colour = "$vehicle->{Colour}"; $fueltype = "$vehicle->{Fuel}"; $damage = "$vehicle->{Damage}"; $doors = "$vehicle->{Doors}"; $cc = "$vehicle->{CC}"; $mileage = "$vehicle->{Speedo}"; $transpeed = "$vehicle->{TransSpeed}"; $trantype = "$vehicle->{TransType}"; $trim = "$vehicle->{TrimLevel}"; $abicat = "$vehicle->{Cat}"; $reserve = "$vehicle->{Reserveprice}"; $vat = "$vehicle->{HasVAT}"; if ($vat eq 1){$hasvat = "Yes"} else {$hasvat = "No"}; $xmlstarts = "$vehicle->{Starts}"; if ($xmlstarts eq 1){$starts = "Starts"} else {$starts = ""}; $xmldrives = "$vehicle->{Drives}"; if ($xmldrives eq 1){$drives = "Drives"} else {$drives = ""}; $xmlkeys = "$vehicle->{Keys}"; if ($xmlkeys eq 1){$keyssupplied = "yes"} else {$keyssupplied = "No"}; $image1 = "$vehicle->{Images}->{Image_1}"; $image2 = "$vehicle->{Images}->{Image_2}"; $image3 = "$vehicle->{Images}->{Image_3}"; $image4 = "$vehicle->{Images}->{Image_4}"; $image5 = "$vehicle->{Images}->{Image_5}"; $image6 = "$vehicle->{Images}->{Image_6}"; $image7 = "$vehicle->{Images}->{Image_7}"; $image8 = "$vehicle->{Images}->{Image_8}"; $image9 = "$vehicle->{Images}->{Image_9}"; $image10 = "$vehicle->{Images}->{Image_10}"; $image11 = "$vehicle->{Images}->{Image_11}"; $image12 = "$vehicle->{Images}->{Image_12}"; ###########Write CSV############ $opath = "e:/out/" ; $add='0'; $add2='xx.csv'; $add5 = $exportid.$lineno ; $add3 = $opath.$exportid.$lineno.$add2 ; $add6 = "xx"; $filename = $exportid.$lineno.$add2; $mileage =~ tr/,//d ; $damage =~ tr/,//d ; $trim =~ tr/,//d ; open (FILENAME,">$add3"); print FILENAME "Ref No,Make,Model,Trim,Vehicle Sub Class,Colour,Engine + Size,Fuel Type,Transmission,Year,Mileage,Registration No,Keys Suppli +ed,Damage Report,VAT Applicable,Condition,FSH,ABI Category,Reserve,Ch +assis No,Vehicle Source,Agent,Region,Vehicle Location,Source Name,Dat +e Approved\n"; print FILENAME "$add5,$make,$model,$trim,$doors,$colour,$cc,$fueltype, +$transpeed $trantype,$year,$mileage,$reg,$keyssupplied,$damage,$hasva +t,$starts $drives,No,$abicat,$reserve,na,$memid,$memid,$memid,$memid, +$auctionid,na,\n"; close FILENAME ; print "<table width=400>"; print "<tr><td>$vehicleno. "; print "$filename Created </td>"; #############Change IMG Name############## $pga ="a.jpg"; $pgb ="b.jpg"; $pgc ="c.jpg"; $pgd ="d.jpg"; $pge ="e.jpg"; $pgf ="f.jpg"; $pgg ="g.jpg"; $pgh ="h.jpg"; $pgi ="i.jpg"; $pgj ="j.jpg"; $pgk ="k.jpg"; $pgl ="l.jpg"; if ($image1 ne ""){$jpg1 ="$add5$add6$pga"; rename ($ipath.$image1, $ipath.$jpg1); print "<td colspan=2>Image 1 Renamed</td></tr>"; } if ($image2 ne ""){$jpg2 ="$add5$add6$pgb"; rename ($ipath.$image2, $ipath.$jpg2); print "<tr><td>&nbsp</td><td colspan=2>Image 2 Renamed</td></tr>"; } if ($image3 ne ""){$jpg3 ="$add5$add6$pgc"; rename ($ipath.$image3, $ipath.$jpg3); print "<tr><td>&nbsp</td><td colspan=2>Image 3 Renamed</td></tr>"; } if ($image4 ne ""){$jpg4 ="$add5$add6$pgd"; rename ($ipath.$image4, $ipath.$jpg4); print "<tr><td>&nbsp</td><td colspan=2>Image 4 Renamed</td></tr>"; } if ($image5 ne ""){$jpg5 ="$add5$add6$pge"; rename ($ipath.$image5, $ipath.$jpg5); print "<tr><td>&nbsp</td><td colspan=2>Image 5 Renamed</td></tr>"; } if ($image6 ne ""){$jpg6 ="$add5$add6$pgf"; rename ($ipath.$image6, $ipath.$jpg6); print "<tr><td>&nbsp</td><td colspan=2>Image 6 Renamed</td></tr>"; } if ($image7 ne ""){$jpg7 ="$add5$add6$pgg"; rename ($ipath.$image7, $ipath.$jpg7); print "<tr><td>&nbsp</td><td colspan=2>Image 7 Renamed</td></tr>"; } if ($image8 ne ""){$jpg8 ="$add5$add6$pgh"; rename ($ipath.$image8, $ipath.$jpg8); print "<tr><td>&nbsp</td><td colspan=2>Image 8 Renamed</td></tr>"; } if ($image9 ne ""){$jpg9 ="$add5$add6$pgi"; rename ($ipath.$image9, $ipath.$jpg9); print "<tr><td>&nbsp</td><td colspan=2>Image 9 Renamed</td></tr>"; } if ($image10 ne ""){$jpg10 ="$add5$add6$pgj"; rename ($ipath.$image10, $ipath.$jpg10); print "<tr><td>&nbsp</td><td colspan=2>Image 10 Renamed</td></tr>"; } if ($image11 ne ""){$jpg11 ="$add5$add6$pgk"; rename ($ipath.$image11, $ipath.$jpg11); print "<tr><td>&nbsp</td><td colspan=2>Image 11 Renamed</td></tr>"; } if ($image12 ne ""){$jpg12 ="$add5$add6$pgl"; rename ($ipath.$image12, $i2path.$jpg12); print "<tr><td>&nbsp</td><td colspan=2>Image 12 Renamed</td></tr>"; } print "<BR>"; print "</table>"; $vehicleno = $vehicleno +1; $lineno = $lineno+1 } $str = ".jpg"; opendir THEDIR, "$ipath"; @jpgfile = grep (/$str/, readdir THEDIR); closedir THEDIR; print "Creating Thumbnails........."; foreach $file (@jpgfile){ $nconv = "c:\\nconvert"; $jpgin = "$ipath/$file"; $jpgout = "$ipath/t$file"; $cmd = "$nconv -quiet -out jpeg -resize 100 75 -o $jpgout $jpgin"; system $cmd; } print "Thumbnails Created<BR>"; unlink $xml; };

Thanks Michael