LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2014, 01:20 PM   #1
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Rep: Reputation: 15
Writing Hashes with Text::CVS (Perl Code)


Code:
use Text::CSV;
open my $file, ">", "StaffPrinting.csv" or die "Couldn't open location file: $!";
my @names = ("Username", "Black and White", "Color", "Ricoh", "Total Page Count");
my @lines = ("========", "===============", "=====", "=====", "================");

my %data;
my $csv = Text::CSV->new () or die;
$csv->eol ("\n");

$csv->print ($file, \@names);
$csv->print ($file, \@lines);

while(<>)  {
  @a = Parse_Log($_);
  ($uname, $queue, $pages) = ((lc($a[5])), (uc($a[4])), $a[10]);
  $prn = (split(/-/, $queue))[3];
  chop($prn);     

  $s = "\$" . "$prn" . "_amt{'$uname'} += $pages"; 
  eval($s);  
  $tot_page_count{$uname} = $BW_amt{$uname} + $CL_amt{$uname} + $RC_amt{$uname};

  # If an entry does not exist yet, create a blank one
  if(not exists $data{$uname}) {
    $data{$uname} = {};
  }

  $data{$uname}{BW_amt} = $BW_amt{$uname};
  if ($data{$uname}{BW_amt} eq "" ) { $data{$uname}{BW_amt} = 0; }
  
  $data{$uname}{CL_amt} = $CL_amt{$uname};
  if ($data{$uname}{CL_amt} eq "" ) { $data{$uname}{CL_amt} = 0; }
  
  $data{$uname}{RC_amt} = $RC_amt{$uname};
  if ($data{$uname}{RC_amt} eq "" ) { $data{$uname}{RC_amt} = 0; }

  $data{$uname}{tot_page_count} = $tot_page_count{$uname};
}
foreach (sort by_total_then_username keys %data) {
  $row = "$_,$data{$_}{BW_amt},$data{$_}{CL_amt},$data{$_}{RC_amt},$data{$_}{tot_page_count}\n";
  $csv->print ($file, $row);
}

sub by_total_then_username {
  return $data{$a}{tot_page_count} <=> $data{$b}{tot_page_count} ||
         $a cmp $b;
}
close $file or die "$!";
Obviously there is some not so pretty ways of accomplishing steps here, but I am editing someone else code and finishing the steps needed to complete before I go back and "pretty up" the code.

The issue is in the "$csv->print ($file, $row); ". How would I get $csv->print to print hashes to the csv file? And How would I get the columns to fit the data?
 
Old 03-16-2014, 04:48 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well the CPAN module page http://search.cpan.org/~makamaka/Tex...t/CSV.pm#print says it expects an io object and an array ref, pretty much as you have done.
Perhaps you could expand on your requirement ?
If your actual src will be a hash, you'll have to convert it for output.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with Perl hashes!!! kedarp Programming 8 03-04-2013 09:17 AM
Perl: Searching through hashes Poetics Programming 4 02-27-2009 02:05 PM
Perl hash of hashes props666999 Programming 2 09-07-2006 04:43 AM
Perl hashes ShaqDiesel Programming 6 08-09-2006 02:54 AM
Nesting Hashes in Perl. faref Programming 2 06-07-2006 05:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration