Create a Perl script to store the information in Excel

Hello,
--------------------------------------
id name address
1 cristine New York
2 murray Redmond
3 kumar Sunnyvale
--------------------------------------

This is 'samples.xlsx' file.
'id' string in A1, 'name' in B1, 'address' in C1 column in excel file.

Create a Perl script to store the information about employee using the header as in raw 1. The Perl script should be able to provide data of any employee using the id or name or address. For example: If I need details that who is the person lives in Sunnyvale, the Perl script should give me back the result as Kumar.

*What data structure you have used and why?
*Try out usage of GetOptions in Perl script...?
*How can I manage duplicate data if any?

Please help me and answer me..

Questions by Cristine Thine

Showing Answers 1 - 3 of 3 Answers

elavv

  • Aug 10th, 2011
 

You can construct one hash where the the emp id as key to the hash and rest of the elements as values in the structure of array ref.

Code
  1. #!<SHEBANG LINE HERE>

  2. use strict;

  3. use Spreadsheet::ParseExcel::Simple;

  4.  

  5. my %output_Hash;

  6.  

  7.  

  8. my $xls = Spreadsheet::ParseExcel::Simple->read('INPUT_FILE.xls');

  9.  

  10. foreach my $sheet ($xls->sheets) {

  11. #my $sheet = ($xls->sheets)[2]; # Sheet 1

  12. #my $data = ($sheet->next_row)[-1]; # last col of row 1

  13. my $sheet_name = $sheet->{sheet}->{Name};

  14.  

  15. open (OUT ,">",$sheet_name.'.xml') or die $!;

  16.  

  17. my($emp_id,$emp_name,$emp_adress);

  18.  while ($sheet->has_data) {  

  19.          my @data = $sheet->next_row;

  20.          $emp_id = $data[0];

  21.      $emp_name = $data[1] ;

  22.          $emp_adress = $data[2];

  23.          

  24. push( @{$output_Hash{$emp_id}}, [$emp_name,$emp_adress]);

  25.  

  26. foreach my $emp_id (sort keys %output_Hash)

  27.   {

  28.   foreach my $array_ref(@{$output_Hash{$emp_id}})

  29. {

  30. my($emp_name,$emp_adress)=@{$array_ref};

  31. }

  32.  }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions