Soy relativamente nuevo en Perl y me he topado con este proyecto que estoy teniendo un momento difícil. El objeto del proyecto es comparar dos archivos csv, uno de los cuales contendría: $ nombre, $ modelo, $ versión y el otro que contendría: $ nombre2, $ disco, $ almacenamiento al final el El archivo RESULTADO contendrá las líneas coincidentes y reunirá la información como sigue: $ nombre, $ modelo, $ versión, $ disco, $ almacenamiento.omitiendo una línea en una matriz, Perl
He logrado hacer esto, pero mi problema es que cuando se rompe uno de los elementos que faltan al programa. Cuando encuentra una línea en el archivo que falta un elemento, se detiene en esa línea. ¿Como puedo solucionar este problema? ¿Alguna sugerencia o una forma de cómo puedo hacer que omita esa línea y continúe?
Aquí está mi código:
open(TESTING, '>testing.csv'); # Names will be printed to this during testing. only .net ending names should appear
open(MISSING, '>Missing.csv'); # Lines with missing name feilds will appear here.
#open (FILE,'C:\Users\hp-laptop\Desktop\file.txt');
#my (@array) =<FILE>;
my @hostname; #stores names
#close FILE;
#***** TESTING TO SEE IF ANY OF THE LISTED ITEMS BEGIN WITH A COMMA AND DO NOT HAVE A NAME.
#***** THESE OBJECTS ARE PLACED INTO THE MISSING ARRAY AND THEN PRINTED OUT IN A SEPERATE
#***** FILE.
#open (FILE,'C:\Users\hp-laptop\Desktop\file.txt');
#test
if (open(FILE, "file.txt")) {
}
else {
die " Cannot open file 1!\n:$!";
}
$count = 0;
$x = 0;
while (<FILE>) {
($name, $model, $version) = split(","); #parsing
#print $name;
chomp($name, $model, $version);
if (($name =~ /^\s*$/)
&& ($model =~ /^\s*$/)
&& ($version =~ /^\s*$/)) #if all of the fields are blank (just a blank space)
{
#do nothing at all
}
elsif ($name =~ /^\s*$/) { #if name is a blank
$name =~ s/^\s*/missing/g;
print MISSING "$name,$model,$version\n";
#$hostname[$count]=$name;
#$count++;
}
elsif ($model =~ /^\s*$/) { #if model is blank
$model =~ s/^\s*/missing/g;
print MISSING"$name,$model,$version\n";
}
elsif ($version =~ /^\s*$/) { #if version is blank
$version =~ s/^\s*/missing/g;
print MISSING "$name,$model,$version\n";
}
# Searches for .net to appear in field "$name" if match, it places it into hostname array.
if ($name =~ /.net/) {
$hostname[$count] = $name;
$count++;
}
#searches for a comma in the name feild, puts that into an array and prints the line into the missing file.
#probably won't have to use this, as I've found a better method to test all of the feilds ($name,$model,$version)
#and put those into the missing file. Hopefully it works.
#foreach $line (@array)
#{
#if($line =~ /^\,+/)
#{
#$line =~s/^\,*/missing,/g;
#$missing[$x]=$line;
#$x++;
#}
#}
}
close FILE;
for my $hostname (@hostname) {
print TESTING $hostname . "\n";
}
#for my $missing(@missing)
#{
# print MISSING $missing;
#}
if (open(FILE2, "file2.txt")) { #Run this if the open succeeds
#open outfile and print starting header
open(RESULT, '>resultfile.csv');
print RESULT ("name,Model,version,Disk, storage\n");
}
else {
die " Cannot open file 2!\n:$!";
}
$count = 0;
while ($hostname[$count] ne "") {
while (<FILE>) {
($name, $model, $version) = split(","); #parsing
#print $name,"\n";
if ($name eq $hostname[$count]) # I think this is the problem area.
{
print $name, "\n", $hostname[$count], "\n";
#print RESULT"$name,$model,$version,";
#open (FILE2,'C:\Users\hp-laptop\Desktop\file2.txt');
#test
if (open(FILE2, "file2.txt")) {
}
else {
die " Cannot open file 2!\n:$!";
}
while (<FILE2>) {
chomp;
($name2, $Dcount, $vname) = split(","); #parsing
if ($name eq $name2) {
chomp($version);
print RESULT"$name,$model,$version,$Dcount,$vname\n";
}
}
}
$count++;
}
#open (FILE,'C:\Users\hp-laptop\Desktop\file.txt');
#test
if (open(FILE, "file.txt")) {
}
else {
die " Cannot open file 1!\n:$!";
}
}
close FILE;
close RESULT;
close FILE2;
En la próxima vez, utilice estrictas en su código, lo protege de errores molestos. –
Por favor, 'use strict;', 'use warnings;', sandee su código correctamente, use la versión de argumento de open con manejadores de archivos léxicos y aprenda a usar las funciones de matriz ('push, map, grep'). – dgw
Sean cuales sean los materiales que utilice para enseñar a usted mismo Perl, le aconsejaría soltarlos: su código se basa en plantillas que van desde simplemente muy anticuadas (identificadores de archivos con nombre global, forma de 2 arg de abierto) hasta incorrectas. No tome esto personalmente, obviamente no es su culpa, pero estaría MUY BIEN atendido al aprender de libros, tutoriales y ejemplos de códigos mucho mejores y más modernos de lo que está usando claramente. – DVK