B
Ben Bullock
Can anyone explain what I'm doing wrong in the following? The output is
something like this:
First try:
1 X 1: A
1 X 2: B
1 X 3: C
2 X 1: D
2 X 2: E
2 X 3: F
3 X 1: G
3 X 2: H
3 X 3: I
Try again:
1 X 1: A
1 X 2: B
1 X 3: C
2 X 1: D
2 X 2: E
2 X 3: F
3 X 1: G
3 X 2: H
3 X 3: I
Now what happens:
Total size 3 X 3
1 X 1: Not defined
1 X 2: Not defined
1 X 3: Not defined
2 X 1: Not defined
2 X 2: Not defined
2 X 3: Not defined
3 X 1: Not defined
3 X 2: Not defined
3 X 3: Not defined
<<<<<<<<<<<<<<<<<<<<<<<<<
I don't know why I get the "Not defined" in the last part. I think it's an
error in calling the subroutine but I'm not sure what I should do to get
the correct syntax.
The following is the shortest I could make my sample program. This is a
complete running example.
============================
#!/usr/bin/perl -w
use strict;
sub read_table
{
my $input_text = $_[0]; # assumes the table text is in the first argument
$input_text =~ m/\{\|(.*?)\|\}/s;
my @rows;
my $table_text = $1;
my @row_text = split (/\|\-/s, $table_text);
my $row_number = 0;
my $n_columns = 0;
my $column_number;
my %cell;
foreach my $rt (@row_text) {
# print "rt='$rt'\n";
$column_number = 0;
$row_number++;
chomp $rt;
while ($rt =~ s/\|(.*)//) {
$column_number++;
$cell{"text"} = $1;
# print "text = '$1' rt = '$rt'";
$rows[$row_number][$column_number] = { %cell };
}
if ($column_number > $n_columns) {
$n_columns = $column_number;
}
}
$rows[0][0] = [$row_number, $n_columns];
for (my $x = 1; $x <= $row_number; $x++) {
for (my $y = 1; $y <= $n_columns; $y++) {
print "$x X $y: ";
my $cell_ref = $rows[$x][$y];
if ($cell_ref) {
my %cell = %$cell_ref;
print $cell{"text"};
} else {
print "Not defined";
}
print "\n";
}
}
return @rows;
}
sub print_table
{
my @rows = $_[0];
my $dim = $rows[0][0];
if (! $dim) {
print "No dimensions in table.\n";
return;
}
my @dimensions = @$dim;
my $row_number = $dimensions[0];
my $n_columns = $dimensions[1];
print "Total size $row_number X $ n_columns\n";
for (my $x = 1; $x <= $row_number; $x++) {
for (my $y = 1; $y <= $n_columns; $y++) {
print "$x X $y: ";
my $cell_ref = $rows[$x][$y];
if ($cell_ref) {
my %cell = %$cell_ref;
print $cell{"text"};
} else {
print "Not defined";
}
print "\n";
}
}
}
my $text = <<ENDTEXT;
{|
|A
|B
|C
|-
|D
|E
|F
|-
|G
|H
|I
|}
ENDTEXT
print "First try:\n";
my @table = read_table ($text);
print "Try again:\n";
for (my $x = 1; $x <= 3; $x++) {
for (my $y = 1; $y <= 3; $y++) {
print "$x X $y: ";
my $cell_ref = $table[$x][$y];
if ($cell_ref) {
my %cell = %$cell_ref;
print $cell{"text"};
} else {
print "Not defined";
}
print "\n";
}
}
print "Now what happens:\n";
print_table (@table);
something like this:
First try:
1 X 1: A
1 X 2: B
1 X 3: C
2 X 1: D
2 X 2: E
2 X 3: F
3 X 1: G
3 X 2: H
3 X 3: I
Try again:
1 X 1: A
1 X 2: B
1 X 3: C
2 X 1: D
2 X 2: E
2 X 3: F
3 X 1: G
3 X 2: H
3 X 3: I
Now what happens:
Total size 3 X 3
1 X 1: Not defined
1 X 2: Not defined
1 X 3: Not defined
2 X 1: Not defined
2 X 2: Not defined
2 X 3: Not defined
3 X 1: Not defined
3 X 2: Not defined
3 X 3: Not defined
<<<<<<<<<<<<<<<<<<<<<<<<<
I don't know why I get the "Not defined" in the last part. I think it's an
error in calling the subroutine but I'm not sure what I should do to get
the correct syntax.
The following is the shortest I could make my sample program. This is a
complete running example.
============================
#!/usr/bin/perl -w
use strict;
sub read_table
{
my $input_text = $_[0]; # assumes the table text is in the first argument
$input_text =~ m/\{\|(.*?)\|\}/s;
my @rows;
my $table_text = $1;
my @row_text = split (/\|\-/s, $table_text);
my $row_number = 0;
my $n_columns = 0;
my $column_number;
my %cell;
foreach my $rt (@row_text) {
# print "rt='$rt'\n";
$column_number = 0;
$row_number++;
chomp $rt;
while ($rt =~ s/\|(.*)//) {
$column_number++;
$cell{"text"} = $1;
# print "text = '$1' rt = '$rt'";
$rows[$row_number][$column_number] = { %cell };
}
if ($column_number > $n_columns) {
$n_columns = $column_number;
}
}
$rows[0][0] = [$row_number, $n_columns];
for (my $x = 1; $x <= $row_number; $x++) {
for (my $y = 1; $y <= $n_columns; $y++) {
print "$x X $y: ";
my $cell_ref = $rows[$x][$y];
if ($cell_ref) {
my %cell = %$cell_ref;
print $cell{"text"};
} else {
print "Not defined";
}
print "\n";
}
}
return @rows;
}
sub print_table
{
my @rows = $_[0];
my $dim = $rows[0][0];
if (! $dim) {
print "No dimensions in table.\n";
return;
}
my @dimensions = @$dim;
my $row_number = $dimensions[0];
my $n_columns = $dimensions[1];
print "Total size $row_number X $ n_columns\n";
for (my $x = 1; $x <= $row_number; $x++) {
for (my $y = 1; $y <= $n_columns; $y++) {
print "$x X $y: ";
my $cell_ref = $rows[$x][$y];
if ($cell_ref) {
my %cell = %$cell_ref;
print $cell{"text"};
} else {
print "Not defined";
}
print "\n";
}
}
}
my $text = <<ENDTEXT;
{|
|A
|B
|C
|-
|D
|E
|F
|-
|G
|H
|I
|}
ENDTEXT
print "First try:\n";
my @table = read_table ($text);
print "Try again:\n";
for (my $x = 1; $x <= 3; $x++) {
for (my $y = 1; $y <= 3; $y++) {
print "$x X $y: ";
my $cell_ref = $table[$x][$y];
if ($cell_ref) {
my %cell = %$cell_ref;
print $cell{"text"};
} else {
print "Not defined";
}
print "\n";
}
}
print "Now what happens:\n";
print_table (@table);