Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have a program (see below) that parses 2 CSV files and outputs text files containing the relevant data. However, when I run my program, I get repeating 'Use of uninitialized value' errors for $listenerName, $port, $channel and $name. Please help me with this people I am going crazy trying to figure out what's wrong!

#!/usr/bin/perl use Class::CSV; use strict; use warnings; # Enable the above statement 'use warnings' to allow error checking. my @AllFiles; my @SplitLine; my %QMS; my $QMName; my $Line; my $buffer = ""; $AllFiles[0] = 'C:\Strawberry\perl_tests\SKYCHAINALL.csv'; $AllFiles[1] = 'C:\Strawberry\perl_tests\CRISALL.csv'; for (my $i=0; $i < @AllFiles; $i++) { open (my $fh, '<', @AllFiles[$i]); if (@AllFiles[$i] ne "\n") { print "\n" . "Filename = '" . @AllFiles[$i] . "'\n"; }; while(read($fh, $buffer, 100)) { @SplitLine = split(',', $buffer); #print $buffer . "\n"; $QMName = $SplitLine[5]; my $currentQM; if (length[$QMName] != 0) { if ($::QMS{"key"} .= $QMName) { $currentQM = $::QMS{"key"} .= $QMName; } else { $currentQM = New Class::QueueManager; $currentQM::Name = $QMName; $::QMS{"key"} .= $currentQM; }; }; &QueueManager::AddListenerName($SplitLine[7]); &QueueManager::AddPort($SplitLine[8]); &QueueManager::AddChannel($SplitLine[9]); &QueueManager::AddQueue($SplitLine[10], $SplitLine[11], $S +plitLine[12], $SplitLine[22]); }; close $fh; }; package Queue; our $Name; our $Depth; our $Persistence; our $Description; sub New { $Name = $_[0]; $Depth = $_[1]; $Persistence = $_[2]; $Description = $_[3]; } # overrides function sub ToString() { return $Name . " " . $Depth . " " . $Persistence . " " . $Desc +ription; }; sub getMQSCcommand { my $cmd; $cmd = "DEFINE QUEUE(" . $Name . ") "; if (length[$Depth] != 0) { $cmd = $cmd . "MAXDEPTH(" . $Depth . ") "; }; if (length[$Persistence] != 0) { $cmd = $cmd . "DEFPSIST(" . $Persistence . ") "; }; if (length[$Description] != 0) { $cmd = $cmd . "DESCRIPTION(" . $Description . ") "; }; $cmd = $cmd . " REPLACE"; return $cmd; }; package QueueManager; our $Name; our %ListenerNames; our %Ports; our %Channels; our %Queues; sub AddListenerName { my $listenerName = $_[0]; if (length($listenerName) == 0) { return ""; } if (exists($ListenerNames{$listenerName})) { $QueueManager::ListenerNames{"key"} .= $listenerName; }; }; sub AddPort { my $port = $_[0]; if (length($port) == 0) { return ""; } if (exists($Ports{$port})) { $QueueManager::Ports{"key"} .= $port; }; }; sub AddChannel { my $channel = $_[0]; if (length($channel) == 0) { return ""; } if (exists($Channels{$channel})) { $QueueManager::Channels{"key"} .= $channel; }; }; sub AddQueue { my $currentQ; my $name = $_[0]; my $depth = $_[1]; my $persistence = $_[2]; my $description = $_[3]; if (exists($Queues{$name})) { $currentQ = New Class::Queue; $QueueManager::Queues{"key"} .= $currentQ; }; }; # overrides function ToString sub ToString() { my $value; $value = $Name; foreach $::listenerName (@{%ListenerNames}) { $value = $value . " " . $::listenerName; }; foreach $::port (@{%Ports}) { $value = $value . " " . $::port; }; foreach $::channel (@{%Channels}) { $value = $value . " " . $::channel; }; foreach $::queue (@{%Queues}) { $value = $value . " " . $::queue; }; }; sub WriteQMFile { open(my $fh, '>', 'C:\Strawberry\perl_tests\'' . $::currentQM +. '.txt'); foreach $::listenerName (@{%ListenerNames->$::currentQM}) { print $fh 'DEFINE LISTENER(' . $::listenerName . ')' . "\n +"; }; foreach $::port (@{%Ports->$::currentQM}) { print $fh ' TRPTYPE(TCP) CONTROL(QMGR) PORT(' . $::port . +') REPLACE' . "\n"; }; foreach $::channel (@{%Channels->$::currentQM}) { print $fh 'DEFINE CHANNEL(' . $::channel . ') TRPTYPE(TCP) + CHLTYPE(SVRCONN) REPLACE' . "\n"; }; foreach $::queue (@{%Queues->$::currentQM}) { print $fh &getMQSCcommand; }; close $fh; print "Done\n"; };

Replies are listed 'Best First'.
Re: WHY DOESN'T THIS WORK
by choroba (Cardinal) on Mar 07, 2016 at 12:03 UTC
    You didn't give us the full information needed. Where in the script does the problem happen? Moreover, "Uninitialized value" is not an error, it's just a warning.

    Without seeing a sample of the input data, we can't really tell what's wrong. See also Short, Self Contained, Correct Example on how to ask a good question with a high chance of getting a satisfying answer.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      The input data is just standard CSV files full of data.

      I get this chunk of 'warnings' repeatedly;

      Use of uninitialized value $listenerName in numeric eq (==) at C:\Strawberry\perl_tests\CSV_Parser.pl line 99.

      Use of uninitialized value $port in numeric eq (==) at C:\Strawberry\perl_tests\CSV_Parser.pl line 112.

      Use of uninitialized value $channel in numeric eq (==) at C:\Strawberry\perl_tests\CSV_Parser.pl line 125.

      Use of uninitialized value $name in exists at C:\Strawberry\perl_tests\CSV_Parser.pl line 142.

        length[$QMName] should be length($QMName)
        same for
        length[$Depth] length[$Persistence] length[$Description]
        poj
Re: WHY DOESN'T THIS WORK
by GotToBTru (Prior) on Mar 07, 2016 at 12:57 UTC

    It seems odd to read a csv file in this way. In fact, unless your lines are exactly 100 bytes long, including line endings, you will quickly get confused about what column of what line you're reading. Garbage in, garbage out at that point.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)