Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:05:13+00:00 2026-06-15T01:05:13+00:00

I am writing a Perl script which parses an Excel file. The aim of

  • 0

I am writing a Perl script which parses an Excel file. The aim of this script is to count for each cell value in column 1, the number of values it has in column 2.

Per example an Excel file that looks like this :

12    abc
12    abc
12    efg
12    efg
13    hij
13    hij
13    klm

My script would return:

For cell value 12 I have :

2 values "abc", 2 values "efg" and for cell value 13 i have : 2 values "hij" and 1 value "klm". 

My script would look something like this (I took this example from the perl doc) :

 use Spreadsheet::XLSX;

 my $excel = Spreadsheet::XLSX -> new ('Book1.xlsx');

 foreach my $sheet (@{$excel -> {Worksheet}}) {

    printf("Sheet: %s\n", $sheet->{Name});

    $sheet -> {MaxRow} ||= $sheet -> {MinRow}; 

     foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {

            $sheet -> {MaxCol} ||= $sheet -> {MinCol};

            foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {

                    my $cell = $sheet -> {Cells} [$row] [$col];

                    if ($cell) {
                        #here I should count the cell values 
                    }
                print $cell;
            }

    }


 }

I have no idea how to do this since I’ve never used perl before in my life, and I can’t find examples online that match what I want exactly. Any help would be much much appreciated.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:05:14+00:00Added an answer on June 15, 2026 at 1:05 am

    Perhaps the following commented script will help:

    use strict;
    use warnings;
    use Spreadsheet::XLSX;
    use Data::Dumper;
    
    # No need to iterate through columns, so set val for col 1
    my $col1 = 0;
    my %hash;
    
    my $excel = Spreadsheet::XLSX->new('Book1.xlsx');
    
    # Just get the first sheet
    my $sheet = ${ $excel->{Worksheet} }[0];
    
    # Calculate the range of rows
    $sheet->{MaxRow} ||= $sheet->{MinRow};
    
    # Iterate through each row
    foreach my $row ( $sheet->{MinRow} .. $sheet->{MaxRow} ) {
    
        # The cell in column 1
        my $cell = $sheet->{Cells}[$row][$col1];
    
        if ($cell) {
    
            # The adjacent cell in column 2
            my $adjacentCell = $sheet->{Cells}[$row][ $col1 + 1 ];
    
            # Use a hash of hashes
            $hash{ $cell->{Val} }{ $adjacentCell->{Val} }++;
        }
    }
    
    # Numerically sort the keys; the value is a hash reference
    for my $key1 ( sort { $a <=> $b } keys %hash ) {
        print "For cell value $key1: ";
    
        # Dereference the hash reference and get the keys/values
        while ( my ( $key2, $val2 ) = each %{ $hash{$key1} } ) {
            print qq{$val2 value(s) "$key2" };
        }
        print "\n";
    }
    
    # Show the hash structure
    print "\n", Dumper \%hash;
    

    Output:

    For cell value 12: 2 value(s) "abc" 2 value(s) "efg" 
    For cell value 13: 1 value(s) "klm" 2 value(s) "hij" 
    
    $VAR1 = {
              '13' => {
                        'klm' => 1,
                        'hij' => 2
                      },
              '12' => {
                        'abc' => 2,
                        'efg' => 2
                      }
            };
    

    You can do the following to show the values associated with key ’13’:

    # Show only the value(s) for key '13'
    print "For cell value 13: ";
    
    # Dereference the hash reference for key '13' and get the keys/values
    while ( my ( $key2, $val2 ) = each %{ $hash{13} } ) {
        print qq{$val2 value(s) "$key2" };
    }
    

    Output:

    For cell value 13: 1 value(s) "klm" 2 value(s) "hij"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Perl/Tk script which displays Excel worksheets using the ss2tk example
I am writing a Perl script which does this : Reads the values of
I'm writing a Perl script in which I need to loop over each character
I'm writing a script using Perl and Net::FTP, which is trying to upload a
I am writing a Perl script (in Windows) that is using File::Find to index
I am writing a Perl script which needs to perform some validations. I have
Can anyone please help me with writing a Perl script which can take as
I am writing a script in Perl which searches for a motif(substring) in protein
I am writing a Perl script that is iterating over file names in a
I'm writing a Bash script in which I am running a perl script which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.