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 3612648
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:01:38+00:00 2026-05-18T22:01:38+00:00

I had to make a really small and simple script that would fill a

  • 0

I had to make a really small and simple script that would fill a table with string values according to these criteria:

  • 2 characters long
  • 1st character is always numeric (0-9)
  • 2nd character is (0-9) but also includes “X”
  • Values need to be inserted into a table on a database

The program would execute:

insert into table (code) values ('01');
insert into table (code) values ('02');
insert into table (code) values ('03');
insert into table (code) values ('04');
insert into table (code) values ('05');
insert into table (code) values ('06');
insert into table (code) values ('07');
insert into table (code) values ('08');
insert into table (code) values ('09');
insert into table (code) values ('0X');

And so on, until the total 110 values were inserted.

My code (just to accomplish it, not to minimize and make efficient) was:

use strict;
use DBI;
my ($db1,$sql,$sth,%dbattr);
%dbattr=(ChopBlanks => 1,RaiseError => 0);
$db1=DBI->connect('DBI:mysql:','','',\%dbattr);
my @code;
for(0..9)
{
    $code[0]=$_;
    for(0..9)
    {
        $code[1]=$_;
        insert(@code);
    }
    insert($code[0],"X");
}
sub insert
{
    my $skip=0;
    foreach(@_)
    {
        if($skip==0)
        {
            $sql="insert into table (code) values ('".$_[0].$_[1]."');"; 
            $sth=$db1->prepare($sql); 
            $sth->execute();
            $skip++;
        }
        else
        {
            $skip--;
        }
    }
}
exit;

I’m just interested to see a really succinct & precise version of this logic.

  • 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-18T22:01:38+00:00Added an answer on May 18, 2026 at 10:01 pm

    133 characters – non-strict

    use DBI;$d=DBI->connect('DBI:mysql','','',{RaiseError=>1});for$a(0..9){for$b(0..9,'X'){$d->do("insert into table values('$a$b')");}}
    

    152 characters – strict

    use strict;use DBI;my$d=DBI->connect('DBI:mysql','','',{RaiseError=>1});for my$a(0..9){for my$b(0..9,'X'){$d->do("insert into table values('$a$b')");}}
    

    Legible version of 152 character string:

    use strict;use DBI;
    my $d=DBI->connect('DBI:Informix:stores','','',{RaiseError=>1});
    foreach my $a (0..9)
    {
        foreach my $b (0..9, 'X')
        {
            $d->do("insert into table values('$a$b')");
        }
    }
    

    Thought process

    Given:

    create table table(code char(2) not null);
    

    And the Perl:

    use strict;
    use DBI;
    my $d=DBI->connect('DBI:mysql','','',{RaiseError=>1});
    my $h=$d->prepare("insert into table(code)values(?)");
    foreach my $a (0..9)
    {
        foreach my $b (0..9, 'X')
        {
            $h->execute("$a$b");
        }
    }
    

    I tested with Informix, so the connect string I actually used was “DBI:Informix:stores“.

    This solution is still readable – and because of the RaiseError, error-proofed (unless you want to add a transaction too).

    Code Golfing it, it becomes (182 characters):

    use strict;use DBI;my$d=DBI->connect('DBI:mysql','','',{RaiseError=>1});my$h=$d->prepare("insert into table(code)values(?)");for my$a(0..9){for my$b(0..9,'X'){$h->execute("$a$b");}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using visual studio to program this small TcpServer. It's really specific. The server
I'm working on an online PHP application that has a need for delayed PHP
I'm a hobbyist, and started learning PHP last September solely to build a hobby
I am hoping someone can help me with a question i have relating to
I've been reading up on Decision Trees and Cross Validation, and I understand both
I have a pre-existing c++ object model which represents the business layer tier of
I have something I need a 2D array for, but for better cache performance,
I am in desperate need of some advice. I'm almost done with my junior
First a little about myself. I am not an experienced software engineer, architect or
I have an existing project written in Ruby on Rails. It is sort of

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.