RichT wrote:
Hi all,

its time again for me to hassle you, i have my script mostly working,
but iv have been bashing my head against a wall trying to fix the last
bits...

its a script for extracting data from a file and exporting it to another.

Problems:
1) i keep getting the warning "Use of uninitialized value in numeric
eq (==) at ./scanDCI.pl line 122, <DCIFILE> line 28." but it should
never execute "./scanDCI.pl line 122" for <DCIFILE> line 28. as thats
the DE line...

initialize C<$inElementSection>:

my $inElementSection = 0;

2) i get void as the 3rd value in the output
["shelf-1-slot-12-port-1",155520000,void,] but that should be the same
as the 2nd value in this case.

The variable C<%segmentFields> should be an array since it is being used as an array. This was fixed when I made that change.


Also, note that in all cases found in your code, you should be using C<my> instead of C<our>.

#!/bin/perl

use strict;
use warnings;
use Getopt::Long;

Getopt::Long::config qw(no_ignore_case);


my ( $inputFile, $outputAllFields, $searchKey, $outputFields ); my @foundSegments; {

    $outputFields = 'name,deviceSpeedIn,deviceSpeedOut';

    my $needHelp;
    GetOptions(         'dciInFile=s'           => \$inputFile,
                        'findField=s'           => \$searchKey,
                        'showFields=s'          => \$outputFields,
                        'listAllFields'         => \$outputAllFields,
                        'help|h|H|?|HELP'          => \$needHelp
              );
    die usage() if $needHelp;
}

############################################################
# Data collection
#  using an input file
#  else quit
############################################################

if ($inputFile) {
    findVars($inputFile);
} else {
    die usage();
}


############################################################ # Data output # if request for keys print all keys # else print results ############################################################

if ($outputAllFields) {
    foreach ( @foundSegments ) {
        foreach (keys %$_) {
            print "$_, ";
        }
        print "\n";
    }

} else {
    foreach my $found (@foundSegments) {
        foreach my $showKey (split /,/, $outputFields) {
            print "$found->{$showKey},";
        }
        print "\n";
    }
}


sub usage { return <<USAGETEXT; usage: $0 -dciInFile filename -dciInFile filename is the dci file you want to scan the following options are also availble [ -findField fieldName ] this is the search key to be used (by default all elements will be returned) [ -showFields field names ] feilds to output (default is name,deviceSpeedIn,deviceSpeedOut) [ -listAllFields ] list avalible fields [ -help] this help text USAGETEXT }

sub findVars {
    ############################################################
    # find infomation form Concord's dci files
    # usage:
    #  findVars("key to search in","value to search for")
    #
    # output:
    #  an aray of Hashes containing all matched segments and keys
    ############################################################


my ($dciFile, %segment, @segmentFields); my $inElementSection = 0;

    # read in dci filename from parent

$dciFile=$_[0] || die qq( FindVars function: Missing dciInfile value $!);

    chomp $dciFile;

open(DCIFILE, "<$dciFile") || die "FindVars function: can not open : $!";


foreach my $line (<DCIFILE>) { chomp $line;

        if ( $line =~ /^FN,Elements,/ ) {
            $line =~ s/FN\,Elements\,//g;
            @segmentFields = split(/\,/,$line);
            next;
        }

        if ( $line =~ /^DS,,Elements,/ ) {
            $inElementSection=1; next;
        }                        #marks start of elements
        if ( $line =~ /^DE/ ) {
            $inElementSection=0; next;
        }                        #marks end of elements
        if ($inElementSection==1) {
            my @segmentValues = split(/\,/,$line);
            for ( my $i = 0; $i < $#segmentValues; $i++ ) {
                $segment{$segmentFields[$i]} = $segmentValues[$i];
            }
            push @foundSegments, \%segment;
            next;
        }
    }
    close DCIFILE;

    return (@foundSegments); # return to main ruteen

}


-- 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