Trouble with array syntax

T

Ted Byers

I am setting up a new table for use in a PDF

The existing tables work fine, but their structure is static. I can
therefore use something like:
@row = ["Transaction type","Response text","Transaction Count"];
push @table2,@row;
@row = [$tt,$rt,$tc];
push @table2,@row;

When I pass @table2 to the PDF routine for maing tables, this works
flawlessly.

Now, however, I need to make a new table where the column names and
numbers are not know until run time. I tried the following, but all
that got displayed in the table were reference values:

$tmp= "Merchant";
push @row,$tmp;
foreach $action_type (sort @master_transaction_types) {
$tmp= "Percent $action_type transactions declined";
push @row,$tmp;
}
print "@row\n";
push @table4,\@row;
foreach $merchant (sort keys %declined_transaction_ratio) {
undef @row;
push @row,$merchant;
foreach $action_type (sort @master_transaction_types) {
push @row,$declined_transaction_ratio{$merchant}{$action_type};
}
print "@row\n";
push @table4,\@row;
}

NB: When I tried the above using "push @table4,@row;" instead of "push
@table4,\@row;", I got an error message complaining that "Merchant"
isn't a reference.

So, then, how do I create a table, like @table2 above, when the column
numbers and names are not known until run time?

I must have missed something simple, but what?

Thanks

Ted
 
X

xhoster

Ted Byers said:
I am setting up a new table for use in a PDF

The existing tables work fine, but their structure is static. I can
therefore use something like:
@row = ["Transaction type","Response text","Transaction Count"];
push @table2,@row;
@row = [$tt,$rt,$tc];
push @table2,@row;

You are only using the first slot of @row, $row[0]. So you might as well
use a scalar rather than an array if you are only going to use one of its
slots.

Also, you should use lexical variables, and "use strict".

When I pass @table2 to the PDF routine for maing tables, this works
flawlessly.

Now, however, I need to make a new table where the column names and
numbers are not know until run time. I tried the following, but all
that got displayed in the table were reference values:

$tmp= "Merchant";
push @row,$tmp;

Again, you should lexical variables and use strict. Where did @row come
from? What kind of variable is it?
foreach $action_type (sort @master_transaction_types) {
$tmp= "Percent $action_type transactions declined";
push @row,$tmp;
}
print "@row\n";
push @table4,\@row;

I'll assume @table4 started out as empty until it got to this point.

foreach $merchant (sort keys %declined_transaction_ratio) {
undef @row;

You just undefined the thing that $table4[0] is referring to.

One way around this is to create a new variable @row, that just happens
to share a source-code name, but nothing else, with the old variable @row.
You do that like:

my @row; # starts empty, no need to undef it.

(For that matter, you could give it a new name. I think it'd keep this one
named @row and change the first occurrence to @header, to better reflect
their natures. But inside the loop, you would still have to use my,
otherwise you have just moved the problem down one level. Your header
would be different from your rows, but all your rows would just be pointing
to the same thing.
push @row,$merchant;
foreach $action_type (sort @master_transaction_types) {
push @row,$declined_transaction_ratio{$merchant}{$action_type};
}
print "@row\n";
push @table4,\@row;

In the absence of the change I proposed, @table4 now holds two references
to the *same* array.
}

NB: When I tried the above using "push @table4,@row;" instead of "push
@table4,\@row;", I got an error message complaining that "Merchant"
isn't a reference.

Presumably you got that error at some later step than what you have shown
us. The push itself would not generate such an error.
So, then, how do I create a table, like @table2 above, when the column
numbers and names are not known until run time?

If you make your programs "use strict" compliant, it will hope (but not
perfectly) figure this stuff out.
I must have missed something simple, but what?

The nature of references, the differences between shallow copies and
deep copies.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
S

smallpond

I am setting up a new table for use in a PDF

The existing tables work fine, but their structure is static. I can
therefore use something like:
@row = ["Transaction type","Response text","Transaction Count"];
push @table2,@row;
@row = [$tt,$rt,$tc];
push @table2,@row;


Would this make more sense to you if you wrote it as:

$row = ["Transaction type","Response text","Transaction Count"];
push @table2,$row;
$row = [$tt,$rt,$tc];
push @table2,$row;

$row (or $row[0] as you call it) is a list reference which you
are pushing into @table2.
 
T

Tad J McClellan

smallpond said:
I am setting up a new table for use in a PDF

The existing tables work fine, but their structure is static. I can
therefore use something like:
@row = ["Transaction type","Response text","Transaction Count"];
push @table2,@row;
@row = [$tt,$rt,$tc];
push @table2,@row;


Would this make more sense to you if you wrote it as:

$row = ["Transaction type","Response text","Transaction Count"];
push @table2,$row;
$row = [$tt,$rt,$tc];
push @table2,$row;

$row (or $row[0] as you call it) is a list reference which you
are pushing into @table2.


And, of course, you do not need the intermediate temporary variable either.

You can build the same data structure as above with:

push @table2, ["Transaction type","Response text","Transaction Count"];
push @table2, [$tt,$rt,$tc];

or even with just:

push @table2, ["Transaction type","Response text","Transaction Count"], [$tt,$rt,$tc];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,212
Messages
2,571,101
Members
47,695
Latest member
KayleneBee

Latest Threads

Top