Hi folks,
I've been dead-ending on this issue, and I'm dusting off my brain to re-engage with Perl, so pls bear with me.
Here's an example of what I'm trying to accomplish, have tried several variants to resolve; but am getting error mssgs to the effect of: Global symbol "@site" requires explicit package name at csvconvert-BETA.pl line 16.
Overview is I'm uploading a .CSV file to a tmp directory on a solaris box, and then running the script to read the data and convert to an XML format. I've had success with that portion, so now I'm trying to find a way to read line 4, column 4 (for example) and apply that to name the XML file. (previously I had statically assigned the directory and name and that worked fine, but want the script to be able to dynamically base it on a value in the .CSV file as there will need to be many uniquely named XML files in the same directory)
[CODE=perl]
use strict;
# Open the exchange-script.csv file for input
open(CSV_FILE, "/tmp/exchange-script.csv") ||
die "Can't open file: $!";
#set variable
my $count=0;
while(<CSV_FILE >) {
#only want to take data from line 4
if ($count==4) {
# Split each field, on the comma delimiter, into an array
my @fields = split(/,/, $_);
}
# Open a new xml file for output and name according to column 4 on line 4
open(XML_FILE, ">/exdir/dir2/ems/$fields[3].xml") ||
die "Can't open file: $!";
}
# Print the initial XML header and the root element
print XML_FILE "<?xml version=\"1.0\" ?>\n";
print XML_FILE "<CEV>\n";
print XML_FILE " <Management_Ite m_File>/somewhere.xml</Management_Item _File>\n";
print XML_FILE " <Icon_Groups>\n ";
print XML_FILE " <Icon_Group>\n" ;
print XML_FILE " <Name>Digital_I P_1_27</Name>\n";
print XML_FILE " <Symbol_Type>Ca rds:Card</Symbol_Type>\n" ;
print XML_FILE "\n";
print XML_FILE " <Background_Ima ge>/something.gif</Background_Imag e>\n";
print XML_FILE "\n";
# The while loop to traverse through each line in exchange-script.csv
while(<CSV_FILE >) {
chomp; # Delete the new line char for each line
# Split each field, on the comma delimiter, into an array
my @fields = split(/,/, $_);
print XML_FILE<<"EOF" ;
<icon>
<Alarm_Status_I D>$fields[8]</Alarm_Status_ID >
<Label>$field s[2]</Label>
<X_Coordinate>$ fields[7]</X_Coordinate>
<Y_Coordinate>$ fields[0]</Y_Coordinate>
<Width>$field s[6]</Width>
<Height>$fiel ds[7]</Height>
<Symbol_Type>En vMSIcon:Generic </Symbol_Type>
<Optional>$fiel ds[9]</Optional>
</icon>
EOF
}
# Close the root element
print XML_FILE " </Icon_Group>\n";
print XML_FILE " </Icon_Groups>\n" ;
print XML_FILE "</CEV>";
# Close all open files
close CSV_FILE;
close XML_FILE
[/CODE]
Thanks for any assist!
Cheers, Hutch
I've been dead-ending on this issue, and I'm dusting off my brain to re-engage with Perl, so pls bear with me.
Here's an example of what I'm trying to accomplish, have tried several variants to resolve; but am getting error mssgs to the effect of: Global symbol "@site" requires explicit package name at csvconvert-BETA.pl line 16.
Overview is I'm uploading a .CSV file to a tmp directory on a solaris box, and then running the script to read the data and convert to an XML format. I've had success with that portion, so now I'm trying to find a way to read line 4, column 4 (for example) and apply that to name the XML file. (previously I had statically assigned the directory and name and that worked fine, but want the script to be able to dynamically base it on a value in the .CSV file as there will need to be many uniquely named XML files in the same directory)
[CODE=perl]
use strict;
# Open the exchange-script.csv file for input
open(CSV_FILE, "/tmp/exchange-script.csv") ||
die "Can't open file: $!";
#set variable
my $count=0;
while(<CSV_FILE >) {
#only want to take data from line 4
if ($count==4) {
# Split each field, on the comma delimiter, into an array
my @fields = split(/,/, $_);
}
# Open a new xml file for output and name according to column 4 on line 4
open(XML_FILE, ">/exdir/dir2/ems/$fields[3].xml") ||
die "Can't open file: $!";
}
# Print the initial XML header and the root element
print XML_FILE "<?xml version=\"1.0\" ?>\n";
print XML_FILE "<CEV>\n";
print XML_FILE " <Management_Ite m_File>/somewhere.xml</Management_Item _File>\n";
print XML_FILE " <Icon_Groups>\n ";
print XML_FILE " <Icon_Group>\n" ;
print XML_FILE " <Name>Digital_I P_1_27</Name>\n";
print XML_FILE " <Symbol_Type>Ca rds:Card</Symbol_Type>\n" ;
print XML_FILE "\n";
print XML_FILE " <Background_Ima ge>/something.gif</Background_Imag e>\n";
print XML_FILE "\n";
# The while loop to traverse through each line in exchange-script.csv
while(<CSV_FILE >) {
chomp; # Delete the new line char for each line
# Split each field, on the comma delimiter, into an array
my @fields = split(/,/, $_);
print XML_FILE<<"EOF" ;
<icon>
<Alarm_Status_I D>$fields[8]</Alarm_Status_ID >
<Label>$field s[2]</Label>
<X_Coordinate>$ fields[7]</X_Coordinate>
<Y_Coordinate>$ fields[0]</Y_Coordinate>
<Width>$field s[6]</Width>
<Height>$fiel ds[7]</Height>
<Symbol_Type>En vMSIcon:Generic </Symbol_Type>
<Optional>$fiel ds[9]</Optional>
</icon>
EOF
}
# Close the root element
print XML_FILE " </Icon_Group>\n";
print XML_FILE " </Icon_Groups>\n" ;
print XML_FILE "</CEV>";
# Close all open files
close CSV_FILE;
close XML_FILE
[/CODE]
Thanks for any assist!
Cheers, Hutch
Comment