Loading ...
Sorry, an error occurred while loading the content.

Beginner's problem

Expand Messages
  • jayspry2001
    Please understand that I m new to PERL and I have taken on an effort with very little experience with PERL. The script I have posted is the core problem and I
    Message 1 of 4 , May 7, 2002
    • 0 Attachment
      Please understand that I'm new to PERL and I have taken on an effort
      with very little experience with PERL. The script I have posted is
      the core problem and I had help with the particulars of HASHes (I'm
      not understanding them quite yet). Anyway, I have two files one with
      data in it and the other is a cvs file. The data file has two types
      of lines, one TRACK and the other POINT. The TRACK line defines the
      name and information about a TRACK and the POINT records are
      associated with a particular TRACK line and contain the location
      data. A TRACK line may have any number of POINT records below it and
      then there will be a new TRACK line and below that associated POINT
      records. Now depending of matching entries in the data file and the
      cvs file, I make modifications to the TRACK line. If there are
      changes to the TRACK line, then I need to make some modifications to
      all of the POINT records below that TRACK line and output the changes
      to a new file. If there is no matches, then the TRACK line and POINT
      lines are output just as they originally were to the new file.

      I hope all that was clear; may be not.

      I'm not sure if this script will work or not yet. I get an
      error "Bad name after TRACK' at c:/p/score2w.PL line 40". I'm not
      sure what the error is, at least I'm not seeing it with my limited
      PERL experience. Any assistance would be greatly appreciated.

      Thank you all in advance for your efforts,

      Jay Spry

      SCRIPT:
      #!/usr/bin/perl -w

      use strict;

      # assign files

      my $cvsfile = "c:\\p\\D\\cvs.txt"; # CVS file input
      my $wamfile = "c:\\p\\D\\SCORE.txt"; # Original SCORE file
      my $newwamfile = "c:\\p\\D\\wam.txt"; # Name of new combined file

      # Some variables

      my %cvs; # Hash for info to check for Source from the
      cvs file
      my %cvs2; # Hash for info to check for Pod # from cvs
      file
      my $flag = 0; # Flag used for switch - for POINT records

      # Reads cvs file into Array line

      open (SOURCE, $cvsfile) or die "Could not read from '$cvsfile',$!";
      while (my $line = <SOURCE>) {
      chomp $line;
      my @line = split (':',$line);
      $cvs{$line[0]} = \@line; # place data into Hash
      $cvs2{$line[0]} = \@line; # place data into Hash
      }
      close SOURCE;

      # Reads Original SCORE file one line at a time

      open (IN,$wamfile) or die "Could not read from '$wamfile',$!";
      open (OUT,">$newwamfile") or die "Could not write
      to '$newwamfile',$!";

      $flag=0; # Set switch to off - no
      modifications for POINT records

      # Begins checks and concatinations

      while (my $line = <IN>) {
      chomp $line;
      my @line = split ('\',$line); # data into Array line
      if ($line[0] eq 'TRACK') { # Check to see if
      line begins with TRACK

      # Next if, checks to see if Source from cvs file is in Original SCORE
      file
      # if so, then check to see that there is a POD # in same cvs record

      if(exist ($cvs{$line[0]})) {

      # This if checks for POD # in cvs record

      if (exist ($cvs2{$line[1]})) {

      # If both Source and Pod # are found, begin modifications and
      concatentations
      # for the TRACK line

      # New Source is set to TRU
      $line[1] = "TRU";

      # UNIT_DES is concat of cvs fields 2 & 3
      $line[2] = $cvs{$line[0]}[2] ." ".
      $cvs{$line[0]}[3];

      # Check to see if cvs field 5 is blank. If
      blank then
      # output original field data, else set
      UNIT_NAME = to
      # cvs field 5
      if ($cvs{$line[0]}[5] eq "") {
      $line[4] = $line[4]
      } else {
      $line[4] = $cvs{$line[0]}[5];
      }

      # UNIT_NO is concat of cvs fields 0 & 1
      $line[5] = $cvs{$line[0]{[0].$cvs
      {$line[0]}[1];

      # UNIT_TYPE = cvs field 2
      $line[6] = $cvs{$line[0]}[2];

      # EXPEND_DES = cvs field 6
      $line[7] = $cvs{$line[0]}[6];

      $flag = 1; # set switch to on -
      modify POINT records
      print OUT join ('\',@l...),"\n"; #
      output to new file

      } else { # No matches, output
      original data
      $flag = 0;
      print OUT $line, "\n";
      }
      }
      }
      if ($line[0] eq 'POINT') { # check if line
      begins with POINT
      if ($flag eq 1) { # If switch is on,
      need to make mods to
      # POINT records

      # CAT = cvs field 7
      $line[18] = $cvs{$line[0]}[7];

      # ID = cvs field 8
      $line[20] = $cvs{$line[0]}[8];
      print OUT join ('\', @line),"\n";
      } else {
      print OUT $line, "\n";
      $flag = 0; # Set switch to off
      }
      }
      }
      close IN;
      close OUT;
    • webmaster_bmw
      ... =============================================================== As with any programming language, most errors can be traced to simple mistakes which are
      Message 2 of 4 , May 9, 2002
      • 0 Attachment
        --- In programming3@y..., "jayspry2001" <jayspry2001@y...> wrote:
        > I'm not sure if this script will work or not yet. I get an
        > error "Bad name after TRACK' at c:/p/score2w.PL line 40". I'm not
        > sure what the error is, at least I'm not seeing it with my limited
        > PERL experience. Any assistance would be greatly appreciated.
        ===============================================================
        As with any programming language, most errors can be traced to simple
        mistakes which are easy to overlook. It's not always a syntax or
        logic problem.

        Copy & Paste errors! (Variables already defined earlier)

        > # Begins checks and concatinations
        >
        > while (my $line = <IN>) {
        > chomp $line;
        > my @line = split ('\',$line); # data into Array line


        Typos - Typos - Typos !!!
        (Get your braces the right way round!)
        > # UNIT_NO is concat of cvs fields 0 & 1
        > $line[5] = $cvs{$line[0]{[0].$cvs

        ==================================================================
        Greg
      • jayspry2001
        Thank you very much for replying. I have made several modifiations to my script. However, I m not seeing some error around the ); at the end of the splice
        Message 3 of 4 , May 9, 2002
        • 0 Attachment
          Thank you very much for replying. I have made several modifiations
          to my script. However, I'm not seeing some error around the ); at
          the end of the splice (I've gotten several suggestions of changing
          the code). It's is some syntax error. Thanks for your help.

          Jay Spry

          #!/usr/bin/perl -w

          use strict;

          # assign files

          my $cvsfile = "c:/p/D/cvs.txt"; # CVS file input
          my $wamfile = "c:/p/D/SCORE.txt"; # Original SCORE file
          my $newwamfile = "c:/p/D/wam.txt"; # Name of new combined file

          # Some variables

          my %cvs; # Hash key for info to check for Source from
          the cvs file
          my $flag = 0; # Flag used for switch - for POINT records

          # Reads cvs file into Array line

          open (SOURCE, $cvsfile) or die "Could not read from '$cvsfile',$!";
          while (my $line = <SOURCE>) {
          chomp $line;
          my @line = split (/:/, $line);
          $cvs{$line[0]} = \@line; # place data into Hash
          }
          close SOURCE;

          # Reads Original SCORE file one line at a time

          open (IN,$wamfile) or die "Could not read from '$wamfile',$!";
          open (OUT,">$newwamfile") or die "Could not write
          to '$newwamfile',$!";

          $flag=0; # Set switch to off - no
          modifications for POINT records

          # Begins checks and concatinations

          while (my $line = <IN>) {
          chomp $line;
          my @line = split (/\\/, $line); # data into Array line
          if ($line[0] eq "TRACK") { # Check to see if
          line begins with TRACK

          # Next if, checks to see if Source from cvs file is in Original SCORE
          file
          # if so, then check to see that there is a POD # in same cvs record

          if(exists $cvs{$line[0]}) {

          # This if checks for POD # in cvs record

          if (exists $cvs{$line[1]}) {

          # If both Source and Pod # are found, begin modifications and
          concatentations
          # for the TRACK line

          # New Source is set to TRU
          # UNIT_DES is concat of cvs fields 2 & 3
          # Check to see if cvs field 5 is blank. If
          blank then
          # output original field data, else set
          UNIT_NAME = to
          # cvs field 5
          # UNIT_NO is concat of cvs fields 0 & 1
          # UNIT_TYPE = cvs field 2
          # EXPEND_DES = cvs field 6

          # Use splice to enter TRACK line data

          splice (@line, 1, 7,
          'TRU',
          $cvs{$line[0]}[2] . " " . $cvs{$line
          [0]}[3],
          $line[3],
          (($cvs{$line[0]}[5]) ? $cvs{$line[0]}
          [5] : $line[4],
          $cvs{$line[0]}[0].$cvs{$line[0]}[1],
          $cvs{$line[0]}[2],
          $cvs{$line[0]}[6],
          );

          $flag = 1; # set switch to on -
          modify POINT records
          print OUT join ('\\', @line),"\n";
          # output to new file

          } else { # No matches, output
          original data
          $flag = 0;
          print OUT $line, "\n";
          }
          }
          }
          if ($line[0] eq "POINT") { # check if line
          begins with POINT
          if ($flag eq 1) { # If switch is on,
          need to make mods to
          # POINT
          records

          # CAT = cvs field 7
          $line[18] = $cvs{$line[0]}[7];

          # ID = cvs field 8
          $line[20] = $cvs{$line[0]}[8];
          print OUT join ('\\', @line),"\n";
          } else {
          print OUT $line, "\n";
          $flag = 0; # Set switch to off
          }
          }
          }
          close IN;
          close OUT;


          --- In programming3@y..., webmaster_bmw <no_reply@y...> wrote:
          > --- In programming3@y..., "jayspry2001" <jayspry2001@y...> wrote:
          > > I'm not sure if this script will work or not yet. I get an
          > > error "Bad name after TRACK' at c:/p/score2w.PL line 40". I'm not
          > > sure what the error is, at least I'm not seeing it with my
          limited
          > > PERL experience. Any assistance would be greatly appreciated.
          > ===============================================================
          > As with any programming language, most errors can be traced to
          simple
          > mistakes which are easy to overlook. It's not always a syntax or
          > logic problem.
          >
          > Copy & Paste errors! (Variables already defined earlier)
          >
          > > # Begins checks and concatinations
          > >
          > > while (my $line = <IN>) {
          > > chomp $line;
          > > my @line = split ('\',$line); # data into Array line
          >
          >
          > Typos - Typos - Typos !!!
          > (Get your braces the right way round!)
          > > # UNIT_NO is concat of cvs fields 0 & 1
          > > $line[5] = $cvs{$line[0]{[0].$cvs
          >
          > ==================================================================
          > Greg
        • jayspry2001
          Please egnore the last email, I found the problem at last. Thanks, Jay Spry ... SCORE ... not
          Message 4 of 4 , May 9, 2002
          • 0 Attachment
            Please egnore the last email, I found the problem at last.

            Thanks,

            Jay Spry

            --- In programming3@y..., "jayspry2001" <jayspry2001@y...> wrote:
            > Thank you very much for replying. I have made several modifiations
            > to my script. However, I'm not seeing some error around the ); at
            > the end of the splice (I've gotten several suggestions of changing
            > the code). It's is some syntax error. Thanks for your help.
            >
            > Jay Spry
            >
            > #!/usr/bin/perl -w
            >
            > use strict;
            >
            > # assign files
            >
            > my $cvsfile = "c:/p/D/cvs.txt"; # CVS file input
            > my $wamfile = "c:/p/D/SCORE.txt"; # Original SCORE file
            > my $newwamfile = "c:/p/D/wam.txt"; # Name of new combined file
            >
            > # Some variables
            >
            > my %cvs; # Hash key for info to check for Source from
            > the cvs file
            > my $flag = 0; # Flag used for switch - for POINT records
            >
            > # Reads cvs file into Array line
            >
            > open (SOURCE, $cvsfile) or die "Could not read from '$cvsfile',$!";
            > while (my $line = <SOURCE>) {
            > chomp $line;
            > my @line = split (/:/, $line);
            > $cvs{$line[0]} = \@line; # place data into Hash
            > }
            > close SOURCE;
            >
            > # Reads Original SCORE file one line at a time
            >
            > open (IN,$wamfile) or die "Could not read from '$wamfile',$!";
            > open (OUT,">$newwamfile") or die "Could not write
            > to '$newwamfile',$!";
            >
            > $flag=0; # Set switch to off - no
            > modifications for POINT records
            >
            > # Begins checks and concatinations
            >
            > while (my $line = <IN>) {
            > chomp $line;
            > my @line = split (/\\/, $line); # data into Array line
            > if ($line[0] eq "TRACK") { # Check to see if
            > line begins with TRACK
            >
            > # Next if, checks to see if Source from cvs file is in Original
            SCORE
            > file
            > # if so, then check to see that there is a POD # in same cvs record
            >
            > if(exists $cvs{$line[0]}) {
            >
            > # This if checks for POD # in cvs record
            >
            > if (exists $cvs{$line[1]}) {
            >
            > # If both Source and Pod # are found, begin modifications and
            > concatentations
            > # for the TRACK line
            >
            > # New Source is set to TRU
            > # UNIT_DES is concat of cvs fields 2 & 3
            > # Check to see if cvs field 5 is blank. If
            > blank then
            > # output original field data, else set
            > UNIT_NAME = to
            > # cvs field 5
            > # UNIT_NO is concat of cvs fields 0 & 1
            > # UNIT_TYPE = cvs field 2
            > # EXPEND_DES = cvs field 6
            >
            > # Use splice to enter TRACK line data
            >
            > splice (@line, 1, 7,
            > 'TRU',
            > $cvs{$line[0]}[2] . " " . $cvs{$line
            > [0]}[3],
            > $line[3],
            > (($cvs{$line[0]}[5]) ? $cvs{$line[0]}
            > [5] : $line[4],
            > $cvs{$line[0]}[0].$cvs{$line[0]}[1],
            > $cvs{$line[0]}[2],
            > $cvs{$line[0]}[6],
            > );
            >
            > $flag = 1; # set switch to on -
            > modify POINT records
            > print OUT join ('\\', @line),"\n";
            > # output to new file
            >
            > } else { # No matches, output
            > original data
            > $flag = 0;
            > print OUT $line, "\n";
            > }
            > }
            > }
            > if ($line[0] eq "POINT") { # check if line
            > begins with POINT
            > if ($flag eq 1) { # If switch is on,
            > need to make mods to
            > # POINT
            > records
            >
            > # CAT = cvs field 7
            > $line[18] = $cvs{$line[0]}[7];
            >
            > # ID = cvs field 8
            > $line[20] = $cvs{$line[0]}[8];
            > print OUT join ('\\', @line),"\n";
            > } else {
            > print OUT $line, "\n";
            > $flag = 0; # Set switch to off
            > }
            > }
            > }
            > close IN;
            > close OUT;
            >
            >
            > --- In programming3@y..., webmaster_bmw <no_reply@y...> wrote:
            > > --- In programming3@y..., "jayspry2001" <jayspry2001@y...> wrote:
            > > > I'm not sure if this script will work or not yet. I get an
            > > > error "Bad name after TRACK' at c:/p/score2w.PL line 40". I'm
            not
            > > > sure what the error is, at least I'm not seeing it with my
            > limited
            > > > PERL experience. Any assistance would be greatly appreciated.
            > > ===============================================================
            > > As with any programming language, most errors can be traced to
            > simple
            > > mistakes which are easy to overlook. It's not always a syntax or
            > > logic problem.
            > >
            > > Copy & Paste errors! (Variables already defined earlier)
            > >
            > > > # Begins checks and concatinations
            > > >
            > > > while (my $line = <IN>) {
            > > > chomp $line;
            > > > my @line = split ('\',$line); # data into Array line
            > >
            > >
            > > Typos - Typos - Typos !!!
            > > (Get your braces the right way round!)
            > > > # UNIT_NO is concat of cvs fields 0 & 1
            > > > $line[5] = $cvs{$line[0]{[0].$cvs
            > >
            > > ==================================================================
            > > Greg
          Your message has been successfully submitted and would be delivered to recipients shortly.