 |
|
Home:
Perl Programming Help:
Beginner:
Re: [manchester] Extracting Data from a File and Tabulating It :
Edit Log
|
|

Kenosis
User
Feb 25, 2013, 9:12 PM
Views: 252
|
|
Re: [manchester] Extracting Data from a File and Tabulating It
|
|
|
I had also used the same hash structure as Chris Charley, but the following's run from the command line and uses Text::Table:
use strict; use warnings; use Text::Table; my ( @files, %names, @row, @rows ) = @ARGV; while (<>) { $names{$1}{$ARGV} = $2 if /(\w+)\s+(\w+)/; } for my $name ( sort keys %names ) { push @row, $name; push @row, $names{$name}{$_} // 0 for @files; push @rows, [ splice @row, 0, @row ]; } my $tb = Text::Table->new( 'Name', @files ); $tb->load(@rows); print $tb; Usage: perl script.pl ONE TWO THREE FOUR [>outFile] Output:
Name ONE TWO THREE FOUR alpha 3 6 0 0 bravo 2 0 2 0 charlie 1 9 0 4 delta 0 2 4 0 echo 0 0 0 1 The files' names are in @ARGV, and are saved in @files for later use. The while (<>) notation reads all the files consecutively, and the current file's name is in $ARGV. A regex is used to capture the files' data. Text::Table takes an array of arrays (AoA) for the table's rows it builds, so the for loop builds this. The the // "defined or" operator is used to either return the number associated with the name/file or 0. The square brackets [ ] denote an anonymous array, and the splice inside them moves all the elements out of @row for use as the anonymous array's elements. Finally, a Text::Table object is created and initialized with heading information. The row data is then loaded, and the table is printed.
(This post was edited by Kenosis on Feb 25, 2013, 9:56 PM)
|
|
|
Edit Log:
|
|
Post edited by Kenosis
(User) on Feb 25, 2013, 9:26 PM
|
|
Post edited by Kenosis
(User) on Feb 25, 2013, 9:44 PM
|
|
Post edited by Kenosis
(User) on Feb 25, 2013, 9:56 PM
|
|
|  |