Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6762525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:20:19+00:00 2026-05-26T14:20:19+00:00

Currently I am working on my exam project and I require some assistance. The

  • 0

Currently I am working on my exam project and I require some assistance.

The goal from this script is to select data from a MySQL database and put this in a file with a specific line length and even a specific column length. The required length for each field and more make-up is located in a different database. So far so good, so what I’ve done is I made two arrays: one for the make up (columns, DB1) and another one for the data (DB2). Now the problem is each line has to make 256 characters, there are those fillers which are located in the make up database but not in the data one, so at the point where the filler field comes up, everything messes up. I tried loads of things, but I can’t figure out what to do, and since its my exam project, my whole study kinda relies on it.

$expsel->bind_columns(\$tabelnaam,\$huidige,\$id);
while($expsel->fetch()) {

    $tbl        = substr($tabelnaam, 0,4);
    $tblnr      = substr($tabelnaam, 2,2);

    $i = 0;
    $exp_spec = $dbh->prepare("SELECT * FROM tblExportspecificatie WHERE tabelnaam = '".$tbl."' ORDER BY id");
    $exp_spec->execute();
    $exp_spec->bind_columns(\$id, \$tblnaam, \$vldnaam, \$vldlngte, \$ascii, \$telveld, \$tellen, \$keystring);
    while($exp_spec->fetch()){
        if($i == 0){
            @AoA = ([ $tbl, $vldnaam, $vldlngte, $ascii, $telveld, $tellen, $keystring ]);
        }else{
            push @{ $AoA[$i] }, $tbl, $vldnaam, $vldlngte, $ascii, $telveld, $tellen, $keystring;
        }
        $i++;
    }
    $exp_spec->finish();

    # Start regel wegschrijven aan het begin van de nieuwe tabel reeks
    # printf FILE "10".$tblnr.pad2str(252, 1, "").chr(10);

    $tbl_data = $dba->prepare("SELECT * FROM ".$tbl." LIMIT 5");
    $tbl_data->execute();

    $regels = $tbl_data->rows;

    my @array;
    while ( my @arr = $tbl_data->fetchrow_array() ) {
        push @array,\@arr;
    }

    for($s = 0; $s < $regels; $s++){
        for($x = 0; $x < $i; $x++){
            if($x == 0){
                if($AoA[0][1] eq "F0101" || $AoA[0][1] eq "F6115"){
                    printf FILE $tblnr.pad2str(4, 0, $gemeentecode).pad2str(8, $AoA[0][3], $array[$s][$x]);
                }else{
                    printf FILE $tblnr.pad2str($AoA[0][2], $AoA[0][3], $array[$s][$x]);
                }
            }else{
                printf FILE pad2str($AoA[$x][2], $AoA[$x][3], $array[$s][$x]);
            }

        }
        printf FILE "--".chr(10);
    }
    printf FILE chr(10);
    print Dumper @AoA;
    print Dumper @array;

    @$AoA = 0;      
}

And here are the dumps for the arrays

$VAR1 = [
          'st31',
          'F0120',
          '9',
          '0',
          '0',
          '0',
          ''
        ];
$VAR2 = [
          'st31',
          'F1110',
          '24',
          '1',
          '0',
          '0',
          ''
        ];
$VAR3 = [
          'st31',
          'F1120',
          '5',
          '0',
          '0',
          '0',
          ''
        ];
$VAR4 = [
          'st31',
          'F1130',
          '1',
          '1',
          '0',
          '0',
          ''
        ];
$VAR5 = [
          'st31',
          'F1140',
          '4',
          '1',
          '0',
          '0',
          ''
        ];
$VAR6 = [
          'st31',
          'F1150',
          '2',
          '1',
          '0',
          '0',
          ''
        ];
$VAR7 = [
          'st31',
          'F1160',
          '6',
          '1',
          '0',
          '0',
          ''
        ];
$VAR8 = [
          'st31',
          'F1020',
          '40',
          '1',
          '0',
          '0',
          ''
        ];
$VAR9 = [
          'st31',
          'F1310',
          '40',
          '1',
          '0',
          '0',
          ''
        ];
$VAR10 = [
           'st31',
           'F8110',
           '1',
           '1',
           '0',
           '0',
           ''
         ];
$VAR11 = [
           'st31',
           'F8120',
           '8',
           '2',
           '0',
           '0',
           ''
         ];
$VAR12 = [
           'st31',
           'F8130',
           '8',
           '2',
           '0',
           '0',
           ''
         ];
$VAR13 = [
           'st31',
           'F1170',
           '40',
           '1',
           '0',
           '0',
           ''
         ];
$VAR14 = [
           'st31',
           'F0121',
           '10',
           '0',
           '0',
           '0',
           ''
         ];
$VAR15 = [
           'st31',
           'F0130',
           '8',
           '0',
           '0',
           '0',
           ''
         ];
$VAR16 = [
           'st31',
           'FILLER',
           '4',
           '1',
           '0',
           '0',
           ''
         ];
$VAR17 = [
           'st31',
           'F0140',
           '10',
           '0',
           '0',
           '0',
           ''
         ];
$VAR18 = [
           'st31',
           'F0220',
           '2',
           '1',
           '0',
           '0',
           ''
         ];
$VAR19 = [
           'st31',
           'FILLER',
           '1',
           '1',
           '0',
           '0',
           ''
         ];
$VAR20 = [
           'st31',
           'F0410',
           '1',
           '1',
           '0',
           '0',
           ''
         ];
$VAR21 = [
           'st31',
           'F0310',
           '8',
           '2',
           '0',
           '0',
           ''
         ];
$VAR22 = [
           'st31',
           'F0810',
           '8',
           '2',
           '0',
           '0',
           ''
         ];
$VAR23 = [
           'st31',
           'F0811',
           '1',
           '1',
           '0',
           '0',
           ''
         ];
$VAR24 = [
           'st31',
           'FILLER',
           '5',
           '1',
           '0',
           '0',
           ''
         ];
$VAR25 = [
           'st31',
           'F1010',
           '1',
           '1',
           '0',
           '0',
           ''
         ];
$VAR26 = [
           'st31',
           'FILLER',
           '7',
           '1',
           '0',
           '0',
           ''
         ];
$VAR1 = [
          '170805955',
          'Waterlelie              ',
          '16',
          undef,
          undef,
          undef,
          '3434VK',
          'Nieuwegein                              ',
          undef,
          'I',
          '2010-01-01',
          '2011-01-01',
          undef,
          '356000000',
          '0',
          '2147483647',
          undef,
          'V',
          '1946-10-24',
          '0000-00-00',
          'A',
          'W'
        ];

So I think the problem is because the column array has more data then the data one, so at the point of the filler, the counter messes up… here’s some more debug data:

<<DEBUG>> L-> 9 AS-> 0, DATA-> 170805955 
<<DEBUG>> L-> 24 AS-> 1, DATA-> Waterlelie               
<<DEBUG>> L-> 5 AS-> 0, DATA-> 16 
<<DEBUG>> L-> 1 AS-> 1, DATA->  
<<DEBUG>> L-> 4 AS-> 1, DATA->  
<<DEBUG>> L-> 2 AS-> 1, DATA->  
<<DEBUG>> L-> 6 AS-> 1, DATA-> 3434VK 
<<DEBUG>> L-> 40 AS-> 1, DATA-> Nieuwegein                               
<<DEBUG>> L-> 40 AS-> 1, DATA->  
<<DEBUG>> L-> 1 AS-> 1, DATA-> I 
<<DEBUG>> L-> 8 AS-> 2, DATA-> 2010-01-01 
<<DEBUG>> L-> 8 AS-> 2, DATA-> 2011-01-01 
<<DEBUG>> L-> 40 AS-> 1, DATA->  
<<DEBUG>> L-> 10 AS-> 0, DATA-> 356000000 
<<DEBUG>> L-> 8 AS-> 0, DATA-> 0 
<FILLER> <-- things are getting messy!
<<DEBUG>> L-> 4 AS-> 1, DATA-> 2147483647 
<<DEBUG>> L-> 10 AS-> 0, DATA->  
<<DEBUG>> L-> 2 AS-> 1, DATA-> V 
<<DEBUG>> L-> 1 AS-> 1, DATA-> 1946-10-24 
<<DEBUG>> L-> 1 AS-> 1, DATA-> 0000-00-00 
<<DEBUG>> L-> 8 AS-> 2, DATA-> A 
<<DEBUG>> L-> 8 AS-> 2, DATA-> W 
<<DEBUG>> L-> 1 AS-> 1, DATA->  
<<DEBUG>> L-> 5 AS-> 1, DATA->  
<<DEBUG>> L-> 1 AS-> 1, DATA->  
<<DEBUG>> L-> 7 AS-> 1, DATA-> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:20:20+00:00Added an answer on May 26, 2026 at 2:20 pm

    A significant part of your formatting code is hidden in pad2str. The problem seems to be how you handle 22 columns with 26 field definitions. My guess is that it is not handling the four non-existent columns at the end.

    In the particular case you gave, your fields add up to a length of 254. However, you seem to be adding both a 2-character $tblnr value at the beginning and '--' at the end! So do you need 256 or 256 + 2 (‘–‘) for record length?

    Also in a given length of the large while loop, @AoA[0][1] is constant, why are you evaluating it 26x5 times? That’s bad code in PHP or Perl.

    • Fix pad2str (which you probably don’t even need.)
    • Decide whether you want $tblnr at the beginning or ‘–‘ at the end–or decide what your record length should be.
    • And regard the code transformation below:

      use List::MoreUtils qw<pairwise>;
      
      # push behavior into statement handles... 
      {   package DBI::st;
          sub get_rows {
              my $sth = shift;
              my @results;
              $sth->execute( @_ );
              while ( my @row = $sth->fetchrow_array ) { 
                  push @results, \@row;
              }
              $sth->close;
              return @results;
          }
      }
      
      my %needs_adjustment = qw<F0101 1 F6115 1>;
      $expsel->bind_columns( \$tabelnaam, \$huidige, \$id);
      while( $expsel->fetch()) {
      
          $tbl   = substr( $tabelnaam, 0, 4 );
          $tblnr = substr( $tabelnaam, 2, 2 );
      
          my ( $first_col ) 
              = @AoA 
              = $dbh->prepare( qq/ 
                SELECT * 
                  FROM tblExportspecificatie 
                 WHERE tabelnaam = '$tbl' 
                ORDER BY id
              / )->get_rows
              ;
      
          my $adjust_first = $needs_adjustment{ $first_col->[1] };
          $first_col->[2]  = 8 if $adjust_first;
          ( $sth = $dba->prepare("SELECT * FROM $tbl LIMIT 5"))->execute;
      
          while ( my $row = $sth->fetchrow_arrayref ) { 
              print $fh 
                    $tblnr
                  , ( $adjust_first ? pad2str( 4, 0, $gemeentecode ) : '' )
                  , ( pairwise { pad2str( @$a[2,3], $b ) } @AoA, @$row )
                  , chr(10)
                  ;
          }
          print $fh chr(10);
      }
      
    • You don’t want printf, unless you’re going to supply a format specifier–which you do not seem to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently working on a c# project that uses another .net library. This library
Currently working on database part of android project. The main aim of the project
Were currently working on a script that reads the current users information from out
Currently working on a project to upgrade our Goole Maps API from v2 to
I am currently working on a project and my goal is to locate text
I'm currently working on the GUI part of our group's first semester exam project,
Currently working with the following package structure: /package __init__.py final.py /write __init__.py write.py /data
Currently working on a VBScript to automate some of the dirty PST ingestion work
Currently working on a flex AIR project based on PureMVC framework. There was a
Currently working through a Teach Yourself WPF tutorial. Usually I can mentally convert from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.