TheBigAmbulance has asked for the wisdom of the Perl Monks concerning the following question:
I have a script that loops through a file a couple of times looking for values stored in @search
#!/usr/bin/perl -w use warnings; use strict; my @search = ("/<ID>(.*?)<\/ID>/", # ID "/<TimeStamp>(.*?)<\/TimeStamp>/", # Time Stamp "/<IP_Address>(.*?)<\/IP_Address>/", # IP Address "/<Title>(.*?)<\/Title>/", # Title "/<Complainant><Entity>(.*?)<\/Entity>/", # Reporting Ent +ity "/<Contact>(.*?)<\/Contact>/", # Reporting Entity Con +tact "/<Address>(.*?)<\/Address>/", # Reporting Entity Add +ress "/<\/Phone><Email>(.*?)<\/Email>/"); # Reporting Entity E +mail Address my $search_count = @search; my @xmlfiles2 = <*xml>; foreach my $file (@xmlfiles2) { for (my $i = 0; $i <= $search_count; $i++) { open FILE, $file or die "Could not read from $file, program haltin +g."; while (<FILE>) { my $line = "$_"; if ( $line =~ $search[$i]) { my $Var = $1; print "$Var\n"; } } close FILE; } }
When I run this, when it matches data, it is generating errors.
dpich@m6400-vb:~/Documents$ perl 2time.pl Use of uninitialized value within @search in regexp compilation at 2ti +me.pl line 21, <FILE> line 1. Use of uninitialized value $Var in concatenation (.) or string at 2tim +e.pl line 23, <FILE> line 1. Use of uninitialized value within @search in regexp compilation at 2ti +me.pl line 21, <FILE> line 2. Use of uninitialized value $Var in concatenation (.) or string at 2tim +e.pl line 23, <FILE> line 2. Use of uninitialized value within @search in regexp compilation at 2ti +me.pl line 21, <FILE> line 3. Use of uninitialized value $Var in concatenation (.) or string at 2tim +e.pl line 23, <FILE> line 3. Use of uninitialized value within @search in regexp compilation at 2ti +me.pl line 21, <FILE> line 4. Use of uninitialized value $Var in concatenation (.) or string at 2tim +e.pl line 23, <FILE> line 4. dpich@m6400-vb:~/Documents$
With my variables declared, I am at a loss to see why this is generating errors. Can anyone point me in the proper direction?
|
|---|