Solved

Problem "unpacking"

Posted on 1998-12-07
5
249 Views
Last Modified: 2010-03-05
I got some problem trying to "unpack" some data.
the variables "p_carrier_cod" and "p_flight_num"
are not assigned values.
Pls advise.

===============================================
#!/usr/local/bin/perl

# auto flush output
$|=1;

# define seperator. Informix uses a pipe character.
$seperator = '|';

# define input file
$fsafile = '/export/home/slok/SIMSQ.TXT';

# define output file
$dumpfile = '> /export/home/slok/sia_segment.txt';
$dumpfile1 = '>> /export/home/slok/sia_segment.txt';

# define format of record type 3 in input file
$format3 = 'x2 a3 a4 a2 a2 x1 a7 a7 a7 x1 a3 a4 a4 a5 a2 a3 a4 a4 a5 a2 a3 a20 x54  a10';

# define format of record type 4 in input file
$format4 = 'x28 a1 a1 a3 x6 a154';

# define format of various data in record type 4 based on DEI
$format4_010 = 'a3 a4 x147';
$format4_050 = 'a3 a4';
#$format4_101 = '';
#$format4_106 = '';
#$format4_109 = '';
#$format4_111 = '';
$format4_503 = 'x8 a1';

# open file for reading ($fsafile) and outputing ($dumpfile)
open (INFILE, $fsafile) or die "open file: $!";
open (OUTFILE, $dumpfile) or die "open file: $!";


      # local variable declarations for Record Type 3
      local (
            $carrier_code,
            $flight_num,
            $IVI,
            $leg_num,
            $period_from,
            $period_to,
            $DOP,
            $dept_station,
            $dept_passanger_STD,
            $dept_aircraft_STD,
            $dept_time_var,
            $dept_terminal,
            $arrv_station,
            $arrv_aircraft_STA,
            $arrv_passanger_STA,
            $arrv_time_var,
            $arrv_terminal,
            $aircraft_type,
            $aircraft_config
      );

      # local variable declarations for Record Type 4
      local (
            $BPI,      # Board Point Indicator
            $OPI,      # Off Point Indicator
            $DEI,      # Data Element Indicator
            $data,      # Data

            $restrict_code,      # traffic restriction
            $cargo_ind,      # cargo indicator
            $code_share_ind,      # code share indicator
            $p_carrier_code,      # partner carrier code
            $p_flight_num      # partner flight number
      );

# init loop counter
$i=0;

while( <INFILE> ) {

      last if $i > 120;

      #
      # in loop initialization!
      #

      # init cargo_ind to 'B'
      $cargo_ind = 'B';

      # init code_share_ind to '0'
      #$code_share_ind = 0;

      #chomp;

      #
      # Record Type 3 handler
      #
      if (/^3/) {

            # increment counter
            $i++;

            # if this is not first Record Type 3
            if ( $i > 1 ) {


                  if ($code_share_ind eq "") { $code_share_ind = "0"; }

                  # append data from Record Type 4
                  $record3 .=
                        ( $BPI . $seperator .
                         $OPI . $seperator .
                         $restrict_code . $seperator .
                         $cargo_ind . $seperator .
                         $code_share_ind . $seperator .
                         $p_carrier_code . $seperator .
                         $p_flight_num );

                  # add a CR
                  $record3 .= "\n";
            }

            ($carrier_code,
                  $flight_num,
                  $IVI,
                  $leg_num,
                  $period_from,
                  $period_to,
                  $DOP,
                  $dept_station,
                  $dept_passanger_STD,
                  $dept_aircraft_STD,
                  $dept_time_var,
                  $dept_terminal,
                  $arrv_station,
                  $arrv_aircraft_STA,
                  $arrv_passanger_STA,
                  $arrv_time_var,
                  $arrv_terminal,
                  $aircraft_type,
                  $aircraft_config
                  )
                  = unpack($format3, $_);

            # replace 00XXX00 with null string if found
            $period_to =~ s/00XXX00/       /;

            $record3 .= ($carrier_code . $seperator .
                        $flight_num . $seperator .
                        $IVI . $seperator .
                        $leg_num . $seperator .
                        $period_from . $seperator .
                        $period_to . $seperator .
                        $DOP . $seperator .
                        $dept_station . $seperator .
                        $dept_passanger_STD . $seperator .
                        $dept_aircraft_STD . $seperator .
                        $dept_time_var . $seperator .
                        $dept_terminal . $seperator .
                        $arrv_station . $seperator .
                        $arrv_aircraft_STA . $seperator .
                        $arrv_passanger_STA . $seperator .
                        $arrv_time_var . $seperator .
                        $arrv_terminal . $seperator .
                        $aircraft_type . $seperator .
                        $aircraft_config . $seperator );


      }

      #
      # Record Type 4 handler
      #
      if (/^4/) {

            ($BPI, $OPI, $DEI, $data) = unpack ($format4, $_);

            if ( $DEI eq "010" ) {
                  ($p_carrier_code,
                        $p_flight_num,
                        $dummy)
                        = unpack ($format_010, $data);
                  $code_share_ind = '1';
                  print "I am in 010\n";
                  print "data is [$data].\n";
                  print $p_carrier_code."\n";
                  print "p_flight_num [$p_flight_num].\n";
                  print "dummy [$dummy]\n";
                  print "\n";
            }
            elsif ( $DEI eq "050" ) {
                  ($p_carrier_code,
                        $p_flight_num)
                        = unpack ($format_050, $data);
                  $code_share_ind = '2';
            }
            elsif ( $DEI eq "503" ) {
                  ($restrict_code)
                        = unpack ($format_503, $data);
            }
            else {
                  #print "Information: DEI $DEI not handled.\n";
            }

      }

}

# add another CR
$record3 .= "\n";

print OUTFILE $record3;

close(INFILE);
close(OUTFILE);
============================================
0
Comment
Question by:slok
  • 3
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1206790
perl -wc 10106242.perl
Name "main::format4_503" used only once: possible typo at 10106242.perl line 29.
Name "main::format_010" used only once: possible typo at 10106242.perl line 179.
Name "main::format_050" used only once: possible typo at 10106242.perl line 191.
Name "main::dumpfile1" used only once: possible typo at 10106242.perl line 14.
Name "main::format_503" used only once: possible typo at 10106242.perl line 196.
Name "main::format4_010" used only once: possible typo at 10106242.perl line 23.
Name "main::format4_050" used only once: possible typo at 10106242.perl line 24.
0
 
LVL 3

Author Comment

by:slok
ID: 1206791
Hi ozo,

thanks for the help.
Will wait for you to submit it as answer and I will award you the points.

0
 
LVL 84

Expert Comment

by:ozo
ID: 1206792
With what grade?
0
 
LVL 3

Author Comment

by:slok
ID: 1206793
it will be an 'A'.
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 1206794
as general advice: use the -w flag
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now