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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:36:01+00:00 2026-06-11T12:36:01+00:00

I am fairly new to Perl so hopefully this has a quick solution. I

  • 0

I am fairly new to Perl so hopefully this has a quick solution.

I have been trying to combine two files based on a key. The problem is there are multiple values instead of the one it is returning. Is there a way to loop through the hash to get the 1-10 more values it could be getting?

Example:

File Input 1:

12345|AA|BB|CC
23456|DD|EE|FF

File Input2:

12345|A|B|C
12345|D|E|F
12345|G|H|I
23456|J|K|L
23456|M|N|O
32342|P|Q|R

The reason I put those last one in is because the second file has a lot of values I don’t want but file 1 I want all values. The result I want is something like this:

WANTED OUTPUT:

12345|AA|BB|CC|A|B|C
12345|AA|BB|CC|D|E|F
12345|AA|BB|CC|G|H|I
23456|DD|EE|FF|J|K|L
23456|DD|EE|FF|M|N|O

Attached is the code I am currently using. It gives an output like so:

OUTPUT I AM GETTING:

12345|AA|BB|CC|A|B|C
23456|DD|EE|FF|J|K|L

My code so far:

#use strict; 
#use warnings; 

open file1, "<FILE1.txt"; 
open file2, "<FILE2.txt"; 

while(<file2>){

    my($line) = $_; 
    chomp $line; 
    my($key, $value1, $value2, $value3) = $line =~ /(.+)\|(.+)\|(.+)\|(.+)/; 
    $value4 = "$value1|$value2|$value3"; 
    $file2Hash{$key} = $value4; 
} 

while(<file1>){ 

    my ($line) = $_; 
    chomp $line; 
    my($key, $value1, $value2, $value3) = $line =~/(.+)\|(.+)\|(.+)\|(.+)/; 

    if (exists $file2Hash{$key}) {

        print $line."|".$file2Hash{$key}."\n";
    } 
    else {
        print $line."\n";
    } 
} 

Thank you for any help you may provide,

  • 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-11T12:36:02+00:00Added an answer on June 11, 2026 at 12:36 pm

    Your overall idea is sound. However in file2, if you encounter a key you have already defined, you overwrite it with a new value. To work around that, we store an array(-ref) inside our hash.

    So in your first loop, we do:

    push @{$file2Hash{$key}}, $value4; 
    

    The @{...} is just array dereferencing syntax.

    In your second loop, we do:

    if (exists $file2Hash{$key}){
      foreach my $second_value (@{$file2Hash{$key}}) {
        print "$line|$second_value\n";
      }
    } else {
      print $line."\n";
    }
    

    Beyond that, you might want to declare %file2Hash with my so you can reactivate strict.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to Perl and have been searching the interwebs for documentation for
I very new to Python, and fairly new to regex. (I have no Perl
I am fairly new to Perl programming, but I have a fair amount of
Fairly new to Jquery here.... but one thing I have been told and being
Im fairly new to c sorry if this is a stupid question! i have
I'm fairly new to Perl, so forgive me if this seems like a simple
I am fairly new to scripting with perl. I am trying to ssh into
I'm fairly new to perl so sorry if this is a newbie question. As
Fairly new to Spring, so I'm having some trouble with this. I'm trying to
Fairly new to wpf, using the galasoft mvvm templates. I have two relaycommands that

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.