Tin-Shan Chau wrote:
>
> I have run into the following problem: If I concatenate
> the packed values of a list to a string, when I use the
> unpack operation to put the values back into an array,
> the last element is missing. I can only get around the
> problem by specifying the exact number of elements to unpack.
> And if I don't have that available, I would have to do
> two unpack operations, the first one to find out how many
> elements are in the array of packed values, the second to
> get the last value. This seems rather a waste to me. Is
> there any way to solve this problem. Your help would be
> greatly appreciated. Thanks in advance.
>
> The following program illustrates the problem I am having:
>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
use warnings;
use strict;
> $TBLXCOL_FORMAT = 'I/a*';
> $lines[0] = "CLASSCOUNT PSOPRDEFN 8 2 SMALLINT N";
> $lines[1] = "OPERPSWD PSOPRDEFN 9 32 CHAR N";
> $lines[2] = "ENCRYPTED PSOPRDEFN 10 2 SMALLINT N";
> $lines[3] = "SYMBOLICID PSOPRDEFN 11 8 CHAR N";
> for ($i=0;$i<4;$i++) {
> ($column, $table) = split / +/, $lines[$i];
> if (!exists($tblxcol{uc($table)})) {
> $tblxcol{uc($table)} = pack ($TBLXCOL_FORMAT, $column);
> }
> else {
> $tblxcol{uc($table)} .= pack ($TBLXCOL_FORMAT, $column);
> }
> }
Ick! This looks more like a C or BASIC program.
my %tblxcol;
my $TBLXCOL_FORMAT = 'I/a* ';
my @lines = (
'CLASSCOUNT PSOPRDEFN 8 2 SMALLINT N',
'OPERPSWD PSOPRDEFN 9 32 CHAR N',
'ENCRYPTED PSOPRDEFN 10 2 SMALLINT N',
'SYMBOLICID PSOPRDEFN 11 8 CHAR N',
);
for ( @lines ) {
my ( $column, $table ) = split;
$tblxcol{ uc $table } .= pack $TBLXCOL_FORMAT, $column;
}
> print "1st example\n";
> @array = unpack $TBLXCOL_FORMAT x 500, $tblxcol{PSOPRDEFN};
^^^^^
It looks like this won't work unless you use the _exact amount_ as the
number of fields. Here is one way to do it if you don't know the number
of fields beforehand:
my @array;
while ( length $tblxcol{'PSOPRDEFN'} ) {
my $size = unpack 'I', substr $tblxcol{'PSOPRDEFN'}, 0, 4, '';
push @array, substr $tblxcol{'PSOPRDEFN'}, 0, $size, '';
}
print 'Array size: ' . @array . "\n";
for my $i ( 1 .. @array ) {
print "$i $array[$i - 1]\n";
}
> exit 0;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]