How to create hash of arrays in perl from excel files ?

The format of input file is ( two columns in excel):

A1;A2
A1:B3
A1:C3
A2:C3
A2:D1

I'd like to get (make) something like that:

A1=>[A2,B3,C3];
A2=>[C3,D1];

No Idea ho to do it ;(
Michał PijusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
Depending on the Excel file format, you need to use either Spreadsheet::ParseExcel or Spreadsheet::XLSX (or Spreadsheet::ParseXLSX).

This should do what you want assuming that the data is in the first two columns of the first worksheet.
use strict;
use warnings;
use Spreadsheet::ParseExcel;
my $file = shift or die "Usage: $0 <input.xls>\n";
my $parser = Spreadsheet::ParseExcel->new();
my $wb = $parser->parse($file) or die $parser->error();
my $ws = ($wb->worksheets())[0];
my ($row_min, $row_max) = $ws->row_range();
my %hash;
for my $row ($row_min .. $row_max) {
    my $c1 = $ws->get_cell($row, 0)->value();
    my $c2 = $ws->get_cell($row, 1)->value();
    if (exists $hash{$c1}) {
        push @{$hash{$c1}}, $c2;
    } else {
        $hash{$c1} = [$c2];
    }
}
# do whatever you want with %hash

Open in new window

2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tel2Commented:
Beautiful work, wilcoxon!  I tested it on Windows and it worked for me.  Take a pay rise, and have the rest of the week off.

Michał, what kind of Excel files are you wanting to process?  XLS or XLSX?
0
wilcoxonCommented:
If you want to process XLSX then, in theory, you should be able to just change "use Spreadsheet::ParseExcel" to "use Spreadsheet::ParseXLSX" in the code I provided.  Note though that I haven't actually used ParseXLSX (the syntax for Spreadsheet::XLSX code is a little different (it is compatible with an older version of Spreadsheet::ParseExcel)).
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

tel2Commented:
Thanks wilcoxon.
I changed the 2 occurrences of "Spreadsheet::ParseExcel" in your script to "Spreadsheet::ParseXLSX" and it worked for an XLSX file, just like a bought one.

Michał, what kind of Excel files are you wanting to process?  XLS or XLSX or both?  If both, wilcoxon's code could be easily modified to handle that, but you might be able to do that yourself.
0
tel2Commented:
Hi Michał,

I understand that this is your first question with Experts Exchange, so are you aware that to accept someone's answer, you don't just click the thumbs-up button.  You have to click the button under their post, near the right, to accept their answer, then follow the prompts.  Otherwise, the expert doesn't earn any points for their hard work.
0
Michał PijusAuthor Commented:
Thank you wilcoxon. It works !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.