Acklay
10
genetics
2051
Bith
12
anthropology
2053
Blistmok
14
metaphysics
2051
Chiilak
17
2051
Lylek
2053
Massiff
13
botany
2053
####
#!/usr/local/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $file = "C:/Documents and Settings/franciscor/Desktop/Kindergarten.txt";
open my $fh, '<', $file or die("Error opening file $!");
my %students_51;
my %students_53;
until ( eof($fh) ) {
chomp( my $name = <$fh> );
chomp( my $age = <$fh> );
chomp( my $topic = <$fh> );
chomp( my $class = <$fh> );
if ( $class == 2051 ) {
$students_51{$name}->{'age'} = $age;
$students_51{$name}->{'topic'} = $topic;
$students_51{$name}->{'class'} = $class;
}
elsif ( $class == 2053 ) {
$students_53{$name}->{'age'} = $age;
$students_53{$name}->{'topic'} = $topic;
$students_53{$name}->{'class'} = $class;
}
}
close $fh;
print Dumper(\%students_51,\%students_53);
##
##
#OUTPUT:
$VAR1 = {
'Chiilak' => {
'topic' => '',
'class' => 2051,
'age' => '17'
},
'Blistmok' => {
'topic' => 'metaphysics',
'class' => 2051,
'age' => '14'
},
'Acklay' => {
'topic' => 'genetics',
'class' => 2051,
'age' => '10'
}
};
$VAR2 = {
'Lylek' => {
'topic' => '',
'class' => 2053,
'age' => ''
},
'Massiff' => {
'topic' => 'botany',
'class' => 2053,
'age' => '13'
},
'Bith' => {
'topic' => 'anthropology',
'class' => 2053,
'age' => '12'
}
};
##
##
use Text::Table;
my $table_51 = Text::Table->new("NAME","AGE","TOPIC","CLASS");
##
##
foreach my $key(keys %students_51){
#Direct interpolation to the add function of Text::Table
$table_51->add(
$key,
"$students_51{$key}->{'age'}",
"$students_51{$key}->{topic}",
"$students_51{$key}->{class}"
); # a record of data
}
$table_51->add(' '); #ADD AN EMPTY Record
print $table_51;
##
##
#OUTPUT:
NAME AGE TOPIC CLASS
Chiilak 17 2051
Blistmok 14 metaphysics 2051
Acklay 10 genetics 2051
##
##
#Zuxu record
my @Zuxu_data = ("Zuxo"," "," ","2053"); #blanks serve as placeholders here
##
##
my $table_53 = Text::Table->new( "NAME\n-----", "AGE\n---", "TOPIC\n-----", "CLASS\n-----" );
my @Zuxu_data = ( "Zuxo", " ", " ", "2053" );
$table_53->load(
[
"Bith", "$students_53{Bith}->{'age'}",
"$students_53{Bith}->{'topic'}", "$students_53{Bith}->{'class'}"
],
[
"Lylek", "$students_53{Lylek}->{'age'}",
"$students_53{Lylek}->{'topic'}", "$students_53{Lylek}->{'class'}"
],
[
"Massiff",
"$students_53{Massiff}->{'age'}",
"$students_53{Massiff}->{'topic'}",
"$students_53{Massiff}->{'class'}"
],
\@Zuxu_data
);
print $table_53;
##
##
#OUTPUT:
NAME AGE TOPIC CLASS
----- --- ----- -----
Bith 12 anthropology 2053
Lylek 2053
Massiff 13 botany 2053
Zuxo 2053
##
##
#print the header for $table_53 and Massiff's record.
foreach(0,1,4){
print $table_53->table($_);
}
##
##
#OUTPUT:
NAME AGE TOPIC CLASS
----- --- ----- -----
Massiff 13 botany 2053
##
##
#show me 3 records starting from line 2
print $table_53->table(2,3);
##
##
#OUTPUT:
Bith 12 anthropology 2053
Lylek 2053
Massiff 13 botany 2053
##
##
my @headed_table = $table_53->table();
my @headless_table = $table_53->body();
print @headless_table;
print "+" x 50, "\n";
print @headed_table;
##
##
#OUTPUT:
Bith 12 anthropology 2053
Lylek 2053
Massiff 13 botany 2053
Zuxo 2053
++++++++++++++++++++++++++++++++++++++++++++++++++
NAME AGE TOPIC CLASS
----- --- ----- -----
Bith 12 anthropology 2053
Lylek 2053
Massiff 13 botany 2053
Zuxo 2053
##
##
print $table_53->body();
print "\n\n";
my $third_record = $table_53->body(2);
print $third_record;
##
##
#OUTPUT:
Bith 12 anthropology 2053 <---The entire table body
Lylek 2053
Massiff 13 botany 2053
Zuxo 2053
Massiff 13 botany 2053 <----Third Record only
##
##
#!/usr/local/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $file = "C:/Documents and Settings/franciscor/Desktop/Kindergarten.txt";
open my $fh, '<', $file or die("Error opening file $!");
my %students_51;
my %students_53;
until ( eof($fh) ) {
chomp( my $name = <$fh> );
chomp( my $age = <$fh> );
chomp( my $topic = <$fh> );
chomp( my $class = <$fh> );
if ( $class == 2051 ) {
$students_51{$name}->{'age'} = $age;
$students_51{$name}->{'topic'} = $topic;
$students_51{$name}->{'class'} = $class;
}
elsif ( $class == 2053 ) {
$students_53{$name}->{'age'} = $age;
$students_53{$name}->{'topic'} = $topic;
$students_53{$name}->{'class'} = $class;
}
}
close $fh;
print Dumper(\%students_51,\%students_53);
use Text::Table;
my $table_51 =
Text::Table->new( "NAME\n-----", "AGE\n---", "TOPIC\n-----", "CLASS\n-----" );
foreach my $key ( keys %students_51 ) {
#Direct interpolation to the add function of Text::Table
$table_51->add( $key, "$students_51{$key}->{'age'}",
"$students_51{$key}->{topic}", "$students_51{$key}->{class}" );
}
$table_51->add(' '); #ADD AN EMPTY LINE
print $table_51;
#since we are using a bulk loader we don't need to loop through %students_53
#Of course this is inefficient if your table has many rows and columns
my $table_53 =
Text::Table->new( "NAME\n-----", "AGE\n---", "TOPIC\n-----", "CLASS\n-----" );
my @Zuxu_data = ( "Zuxo", " ", " ", "2053" );
$table_53->load(
[
"Bith", "$students_53{Bith}->{'age'}",
"$students_53{Bith}->{'topic'}", "$students_53{Bith}->{'class'}"
],
[
"Lylek", "$students_53{Lylek}->{'age'}",
"$students_53{Lylek}->{'topic'}", "$students_53{Lylek}->{'class'}"
],
[
"Massiff",
"$students_53{Massiff}->{'age'}",
"$students_53{Massiff}->{'topic'}",
"$students_53{Massiff}->{'class'}"
],
\@Zuxu_data
);
print $table_53;
##
##
my @headless_51 = $table_51->body();
my @headless_53 = $table_53->body();
print "@headless_51 @headless_53";
##
##
#OUTPUT:
Chiilak 17 2051
Blistmok 14 metaphysics 2051
Acklay 10 genetics 2051
Bith 12 anthropology 2053
Lylek 2053
Massiff 13 botany 2053
Zuxo 2053
##
##
my $merged_tables = Text::Table->new("\tTABLE 51 ","\tTABLE 53");
$merged_tables->add($table_51->select(0,1,2,3),$table_53->select(3,2,1,0));
print $merged_tables;
#OUTPUT:
TABLE 51 TABLE 53
NAME AGE TOPIC CLASS CLASS TOPIC AGE NAME
----- --- ----- ----- ----- ----- --- -----
Chiilak 17 2051 2053 anthropology 12 Bith
Blistmok 14 metaphysics 2051 2053 Lylek
Acklay 10 genetics 2051 2053 botany 13 Massiff
2053 Zuxo
##
##
my $merged_name_age=Text::Table->new("\tAGES OF CLASSES","");
$merged_name_age->add($table_51->select(0,1),$table_53->select(0,1));
print $merged_name_age;
#OUTPUT:
AGES OF CLASSES
NAME AGE NAME AGE
----- --- ----- ---
Chiilak 17 Bith 12
Blistmok 14 Lylek
Acklay 10 Massiff 13
Zuxo
##
##
my $class_topic=Text::Table->new("Subjects Of Classes","");
$class_topic->add($table_51->select(2,3),$table_53->select(2,3));
print $class_topic;
#OUTPUT:
Subjects Of Classes
TOPIC CLASS TOPIC CLASS
----- ----- ----- -----
2051 anthropology 2053
metaphysics 2051 2053
genetics 2051 botany 2053
2053
##
##
# @Dlines = $table1->body();
# @DPlines = $table2->body();
my $alignedTable = Text::Table->new();
for(my $i = 0 .. $#Dlines){
my($D1) = split(/\s{2,}/,$Dlines[$i]) if ($Dlines[$i] !~/^\s*+$/);
my($D2) = split(/\s{2,}/,$DPlines[$i]) if ($DPlines[$i]!~/^\s*+$/);;
$alignedTable->load([$D1, $D2]);
}
print $alignedTable;