Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Perl Format the Output in table

by ppnair (Initiate)
on Oct 11, 2012 at 14:05 UTC ( #998455=perlquestion: print w/replies, xml ) Need Help??
ppnair has asked for the wisdom of the Perl Monks concerning the following question:

I have written a perl code for processing file 'Output.txt' which has below Content. 28 28 28 29 29 29 29 28 29 30 31 31

And file 'domain.txt' has list of domain Names which i need to match against File 'Output.txt'

I could manage to write PERL code like this

#!/usr/bin/perl use strict; open(LOGFILE,"Output.txt") or die("Could not open log file."); my $domain_name = 'domain.txt' ; open(DOM, $domain_name); my @r_contents = <LOGFILE>; close(LOGFILE); while(<DOM>) { chomp; my $line = $_; my @lowercase = map { lc } @r_contents; my @grepNames = grep /^$line/, @lowercase; foreach (@grepNames) { if ( grep /^$line/, @lowercase ) { $domains{lc($_)}++ ; } } } close(DOM) ; foreach my $domain (sort keys %domains) { my %seen ; ($Dname, $WeekNum) = split(/\s+/, $domain); my @array1 = grep { ! $seen{ $_ }++ } $WeekNum; push @array2, @array1; my @array4 = "$domains{$domain} $domain" ; push @matrix,@array4 ; } printf "%-10s %-25s %-25s\n", 'DoaminName', "Week $array2[0]" , +"Week $array2[1]","Week $array2[2]"; print " @matrix \n";

current Output looks like this.

DoaminName Week 28 week29 week30 week 31

2 35

1 28

4 29

1 28

1 29

1 30

1 31

But i trying re-write the perl code to print the output In table with unique details .Please help me to correct the code.

Domain/WeekNumber Week28 Week29 Week30 Week31 2 No No No 1 4 NO NO 1 1 1 1

Replies are listed 'Best First'.
Re: Perl Format the Output in table
by protist (Monk) on Oct 11, 2012 at 15:30 UTC
    #!/usr/bin/perl use warnings; use strict; open my $fh, "<", "output.txt" or die "could not open output.txt\n"; my %data; while(<$fh>){ chomp; $_=~ m/(.*?)\s++(\d++)/; my $site=$1; my $week=$2; $data{$site}{$week}++; } print "Domain/WeekNumber\tWeek28\tWeek29\tWeek30\tWeek31\n"; for my $site(sort keys %data){ print"$site\t\t"; for my $week (qw(28 29 30 31)){ unless(defined $data{$site}{$week} ){ print"NO\t"; }else{ print $data{$site}{$week} ."\t"; } } print"\n"; } close $fh;

      Nice work on processing the Output.txt file. However, if I'm understanding the OP correctly, the contents of domain.txt are used to filter the contents of Output.txt, such that will not appear in the final output when using the OP's shown data sets. Given this, perhaps something like the following additions are needed:

      ... open my $fh2, '<', 'domains.txt' or die "could not open domains.txt: $ +!\n"; my %domains = map { chomp; $_ => 1 } <$fh2>; close $fh2; open my $fh, "<", "output.txt" or die "could not open output.txt: $!\n +"; my %data; while (<$fh>) { chomp; $_ =~ m/(.*?)\s++(\d++)/; next unless $domains{$1}; ...

      Thank you very much for the help.

      But 'Output.txt'have got dynamic data.The week Number Will keep on change when ever i pull data from Maillog for current month.

      print "Domain/WeekNumber\tWeek28\tWeek29\tWeek30\tWeek31\n"; and for my $week (qw(28 29 30 31)) will not work for me since the week number chnages every month.

Re: Perl Format the Output in table
by Anonymous Monk on Oct 11, 2012 at 14:31 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998455]
Approved by Corion
[stonecolddevin]: ,arto o/
[stonecolddevin]: dammit
[stonecolddevin]: marto o/
LanX beta matrix
[marto]: The child still awake just made me aware of 'Lego Sonic', the sonic game rebuilt in a Lego game engine...
[marto]: What a world
[oiskuu]: Ahem. Speaking of toxic injections.. Is there a simple way to inject into package a sub{} so that it captures a $variable that is declared package my?
[Corion]: oiskuu: No, which is why I hate package-scope lexicals
[marto]: yikes
[Corion]: Variables declared in a package should always be globals so that you can get at them from the outside

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2017-12-15 19:36 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (442 votes). Check out past polls.