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 7032635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:57:39+00:00 2026-05-28T00:57:39+00:00

Here is the code, I know it is not perfect perl. If you have

  • 0

Here is the code, I know it is not perfect perl. If you have insight on how I an do better let me know. My main question is how would I print out the arrays without using Data::Dumper?

#!/usr/bin/perl
use Data::Dumper qw(Dumper);

use strict;
use warnings;

open(MYFILE, "<", "move_headers.txt") or die "ERROR: $!";

#First split the list of files and the headers apart
my @files;
my @headers;
my @file_list = <MYFILE>;
foreach my $source_parts (@file_list) {
  chomp($source_parts);
  my @parts = split(/:/, $source_parts);
  unshift(@files, $parts[0]);
  unshift(@headers, $parts[1]);
}

# Next get a list of unique headers
my @unique_files;
foreach my $item (@files) {
  my $found = 0;
  foreach my $i (@unique_files) {
      if ($i eq $item) {
        $found = 1;
        last;
      }
  }
  if (!$found) {
    unshift @unique_files, $item;
  }
}
@unique_files = sort(@unique_files);

# Now collect the headers is a list per file
my %hash_table;
for (my $i = 0; $i < @files; $i++) {
  unshift @{ $hash_table{"$files[$i]"} }, "$headers[$i]";
}

# Process the list with regex
while ((my $key, my $value) = each %hash_table) {
  if (ref($value) eq "ARRAY") {
    print "$value", "\n";
  }
}
  • 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-05-28T00:57:39+00:00Added an answer on May 28, 2026 at 12:57 am

    You’re doing a couple things the hard way. First, a hash will already uniqify its keys, so you don’t need the loop that does that. It appears that you’re building a hash of files, with the values meant to be the headers found in those files. The input data is “filename:header”, one per line. (You could use a hash of hashes, since the headers may need uniquifying, but let’s let that go for now.)

    use strict;
    use warnings;
    open my $files_and_headers, "<", "move_headers.txt" or die "Can't open move_headers: $!\n";
    
    my %headers_for_file;
    while (defined(my $line = <$files_and_headers> )) {
            chomp $line;
            my($file, $header) = split /:/, $line, 2;
            push @{ $headers_for_file{$file} }, $header;
    }
    
    # Print the arrays for each file:
    foreach my $file (keys %headers_for_file) {
        print "$file: @{ $headers_for_file{$file}}\n";
    }
    

    We’re letting Perl do a chunk of the work here:

    1. If we add keys to a hash, they’re always unique.
    2. If we interpolate an array into a print statement, Perl adds spaces between them.
    3. If we push onto an empty hash element, Perl automatically puts an empty anonymous array in the element and then pushes onto that.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code I dont know whats wrong with it. <?php //Logout code
The code here is X++. I know very little about it, though I am
I'm trying to solve the problem here but I don't know why my code
enter code here I have a table on SQL server 2005 with bigint primary
enter code here Hi All, I have a simple windows service application that connects
See here: http://code.google.com/p/ie7-js/ Does anyone have any experience or remarks about this javascript? Is
Am interested to know how correct is my query the question is posted here
I know its a not a new concept that my site is perfect on
I think everything is perfect in the code but I'm not able to receive
This is driving me mad , I have a code that works perfect with

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.