<[EMAIL PROTECTED]> wrote:
>
> I am searching through a large data file and pulling out the data for
> elements that match the IP address...
>
> this is at least half working well up to " #just for testing 1"
> but not after " #just for testing 2",  hash %segmentFields is
> not being populated.
>
> BTW this will be a sub

Hi.

I recently questioned whether a rewrite was an appropriate response
to a question on this group. Now I'm going to do it anyway!

I couldn't see into the code you posted at all so had to reformat it.
The following program does very nearly the same thing, and will let
others see through the forest to help you find bugs.

#!perl

use strict;
use warnings;

my $siteIPAdd = shift;

my $NH_HOME;
my (@segmentElement, $segmentFieldKey, $segmentFieldValue, %segmentFields, 
@foundElements, @nullVar, $nullVar, $test);

local $/ = "}\n";

open POLLER, 'poller.cfg' or die 'can not open : ', $!;

my $i = 0;
while(<POLLER>) {

  next unless /\s+segment/;
  next unless /$siteIPAdd/;

  foreach (split /\n/) {
    ($nullVar,$segmentFieldKey,$segmentFieldValue) = split ' ', $_, 3;
    print "$segmentFieldKey wee $segmentFieldValue\n" ;
    $segmentFields { $segmentFieldKey => $segmentFieldValue };
    $foundElements[$i] = qw($segmentFields{segment} $segmentFields{deviceSpeed2} 
$segmentFields{sysName} $segmentFields{aliasName}
$segmentFields{mibTranslationFile});
    $i++;
    print "$i $segmentFields{segment} $segmentFields{deviceSpeed2} 
$segmentFields{sysName} $segmentFields{aliasName}
$segmentFields{mibTranslationFile} \n"; #just for testing 2
  }
}

close POLLER;

print @foundElements;

open(DISCOVERLOG, ">found.test.csv");
print DISCOVERLOG "@foundElements\n";
close DISCOVERLOG;

__END__

First of all, the warnings are there to help you. Don't 'fix' warnings by commenting
out the 'use warnings' line. From this code, I get

  Useless use of a constant in void context at E:\Perl\source\xx.pl line 21.

Line 21 is:

  $foundElements[$i] = qw($segmentFields{segment} ... );

and you're getting this error because you're assigning a list of strings to a scalar
value. All but one of the values will be thrown away, so they are 'useless'. What you
probably mean is

  $foundElements[$i] = qq($segmentFields{segment} ... );

as in the print statement at the end of the foreach loop.

Correcting this, I get

  Useless use of hash element in void context at E:\Perl\source\xx.pl line 20.

Line 20 is:

  $segmentFields { $segmentFieldKey => $segmentFieldValue };

all you're doing here is mentioning a single hash value without doing
anything with it. My guess is that you mean:

  $segmentFields{$segmentFieldKey} = $segmentFieldValue;

After that there are no more syntax errors, but without a lot more information
I can't tell whether  it will work for you.

Please don't just adopt these changes as your own unless you understand properly
how they work.

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to