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

  • Home
  • SEARCH
  • 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 8179883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:05:48+00:00 2026-06-07T00:05:48+00:00

I am using Text::CSV::Slurp to create a CSV file from an array of hashes.

  • 0

I am using Text::CSV::Slurp to create a CSV file from an array of hashes. It works great except some headers are missing.

hash1    
    header1 header2
    1          2

hash2
    header1 header2 header3
    11        22       33

I want the final output to be a CSV file:

header1 header2 header3
1          2
11         22      33

not the slurp output

header1 header2
1          2
11         22

Any suggestions?

code:

sub entry_capture_csv {
my ($stores_folder, $cmdstr, $header_field, $entrycmdstr, $entryfilename) = @_;

print "\t\tOSSI-" . $cmdstr . " for " . $entryfilename . " entry detail\n";
$node->pbx_command($cmdstr);
if ( $node->last_command_succeeded() ) {
    my @ossi_output = $node->get_ossi_objects();
    my $i = 0;
    my @ext_array;
    foreach my $hash_ref(@ossi_output) {
        $i++;
        #print "output result $i\n";
        for my $field ( sort keys %$hash_ref ) {
            my $value = $hash_ref->{$field};
            #print "\t$field => $value\n";
        }
        my $entryNumber = trim($hash_ref->{$header_field});
        unless( defined $entryNumber ) { $entryNumber = '' };
        if ($entryNumber eq "") {
            #empty string
        } else {
            push(@ext_array, $entryNumber);
        }
    }

    # Issue = failed sometimes there is extra header
    #
    my @result_array;

    foreach (@ext_array) {
        my $entrycmd =  $entrycmdstr . " " . $_;
        $node->pbx_command($entrycmd);

        if ( $node->last_command_succeeded() ) {
            print "\t\t\t" . $entrycmd . "\n";

            my @ossi_output = $node->get_ossi_objects();
            push(@result_array, @ossi_output);

            #my $csv = Text::CSV::Slurp->create( input => \@ossi_output );
            #open (OUTFILE, ">$stores_folder/$store-" . $entryfilename . "-" . "$_" . ".csv") || die "Can't open output file.\n";
            #print OUTFILE $csv;
            #close(OUTFILE);

        } else {
            print "Failed\t\t\t" . $entrycmd . "\n";
        }
    }

    my $csv = Text::CSV::Slurp->create( input => \@result_array );
    open (OUTFILE, ">$stores_folder/$store-" . $entryfilename . ".csv") || die "Can't open output file.\n";
    print OUTFILE $csv;
    close(OUTFILE);
}

}

  • 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-07T00:05:49+00:00Added an answer on June 7, 2026 at 12:05 am

    The Text::CSV::Slurp->create constructor method takes a field_order option which defines the keys of the hash elements to be used and their order in the CSV record.

    Take a look at this code, which I think is self-explanatory.

    use strict;
    use warnings;
    
    use Text::CSV::Slurp;
    
    my @data = (
      {
        header1 => 1,
        header2 => 2,
      },
      {
        header1 => 11,
        header2 => 22,
        header3 => 33,
      },
    );
    
    my $csv = Text::CSV::Slurp->create(
        input => \@data,
        field_order => [qw/ header1 header2 header3 /]);
    
    print $csv;
    

    output

    header1,header2,header3
    1,2,
    11,22,33
    

    Update

    To automate getting a list of headers from the data, you could write something like this. The output is identical to that from the previous program.

    Note that there is no way of knowing how best to sort the list of headers so I have used a simple lexical sort. It produces the desired effect with this trivial data.

    use strict;
    use warnings;
    
    use Text::CSV::Slurp;
    
    my @data = (
      {
        header1 => 1,
        header2 => 2,
      },
      {
        header1 => 11,
        header2 => 22,
        header3 => 33,
      },
    );
    
    my @headers = do {
      my %headers;
      $headers{$_}++ for map { keys %$_ } @data;
      sort keys %headers;
    };
    
    my $csv = Text::CSV::Slurp->create(
        input => \@data,
        field_order => \@headers);
    
    print $csv;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read data from a CSV file using microsoft text driver. Can
I am using C# to read a ~120 MB plain-text CSV file. Initially I
I am using this text editor for my windows forms application This works great
My goal here is to have the browser download a csv file using headers
I am using Text::CSV to parse a csv file. Not all lines can be
I'm using a technique from another Stack Overflow question to write a CSV file
I follow the tutorial here to query content from CSV file: http://rudesyle.wordpress.com/2008/01/28/using-linq-to-query-a-csv-file/ However, the
I am using PHP to pass some information in a text file back to
I'm trying to use IO::Handle to create a CSV file with Text::CSV . When
I am trying to download a CSV file from a URL using PHP Curl,

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.