john <mailto:[EMAIL PROTECTED]> wrote:

: Thanks, Charles, for your suggestions. I think I've incorporated
: most of them.

    You're welcome.


: Your regex is a lot more practical than what I was using.

    I wouldn't have used it a year ago. Perhaps I'm getting
better. :)


: Here is the improved script. I can live with the one warning
: "Newline in left-justified string for printf at ./describe_skus.pl
: line 29".

    That's strange, there doesn't seem to be a new line in that
format string. Try adding this to the top of the script and see if
you can track down the problem. You might get a better idea of how
to correct this problem. Did you leave it wrapped like it is in the
email perhaps? The format should all be on one line.

: #!/usr/bin/perl -w
: use strict;
: use DBI;

use diagnostics;

===============================================================

    I indented and wrapped this to make it easier to read.

: while ( my $columns = $sth2->fetchrow_arrayref() ) {
:     printf OUTFILE ("%-4d %-40s %-22s %d",
          "@$columns[0]",
          "@$columns[1]",
          "@$columns[2]",
          "@$columns[3]"
      );
:     print OUTFILE "\n";
: }

    I assume you chose fetchrow_arrayref() over fetchrow_array()
because it's more efficient. Good. Now we need to break
you of this double-quoting variables habit. :)

    The code above can be written without the double quotes. The
only warning is from the printf.

while ( my $columns = $sth2->fetchrow_arrayref() ) {
    printf OUTFILE (
        '%-4d %-40s %-22s %d',
        @$columns[0],
        @$columns[1],
        @$columns[2],
        @$columns[3],
    );
    print OUTFILE "\n";
}

    You can play along at home with this test code (no database
needed).

while ( my $columns = [0..3] ) {
    printf (
        '%-4d %-40s %-22s %d',
        @$columns[0],
        @$columns[1],
        @$columns[2],
        @$columns[3],
    );
    print "\n";
    last;
}

    When I run this, I get the following error. We can avoid this
by dropping the parenthesis. See code below.

printf (...) interpreted as function at a.pl line 10.


while ( my $columns = [0..3] ) {
    printf
        '%-4d %-40s %-22s %d',
        @$columns[0],
        @$columns[1],
        @$columns[2],
        @$columns[3];

    print "\n";
    last;
}


    The new line (\n) can be added into the format and allow us to
eliminate the second print statement. Special characters like this
and \t are the only reasons for putting formats in double quotes.
Only single quoted strings are normally needed.

while ( my $columns = [0..3] ) {
    printf
        "%-4d %-40s %-22s %d\n",
        @$columns[0],
        @$columns[1],
        @$columns[2],
        @$columns[3];

    last;
}

    While this code does not give any errors, it is more common to
use the -> operator.

while ( my $columns = [0..3] ) {
    printf
        "%-4d %-40s %-22s %d\n",
        $columns->[0],
        $columns->[1],
        $columns->[2],
        $columns->[3];

    last;
}

    There is another way to go. In this example we deference the
array reference with the @{} operator.

while ( my $columns = [0..3] ) {
    printf
        "%-4d %-40s %-22s %d\n",
        @{ $columns };

    last;
}

    As a shortcut we can use @$columns.

while ( my $columns = [0..3] ) {
    printf
        "%-4d %-40s %-22s %d\n",
        @$columns;

    last;
}

    Taking this back to the database example, we end up with this.

while ( my $columns = $sth2->fetchrow_arrayref() ) {
    printf "%-4d %-40s %-22s %d\n", @$columns;
}


    Or, if you are feeling lucky, this might do.

printf "%-4d %-40s %-22s %d\n", @$_ while $sth2->fetchrow_arrayref();


    If the "sku" column is unique, only one row is returned and
this will do the trick.

printf "%-4d %-40s %-22s %d\n", @{ $sth2->fetchrow_arrayref() };

    # or (maybe):

printf "%-4d %-40s %-22s %d\n", $sth2->fetchrow_array();


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to