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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:33:27+00:00 2026-06-08T10:33:27+00:00

I have two files, one with text and another with key / hash values.

  • 0

I have two files, one with text and another with key / hash values. I want to replace occurrences of the key with the hash values. The following code does this, what I want to know is if there is a better way than the foreach loop I am using.

Thanks all

Edit: I know it is a bit strange using

s/\n//;
s/\r//;

instead of chomp, but this works on files with mixed end of line characters (edited both on windows and linux) and chomp (I think) does not.

File with key / hash values (hash.tsv):

strict  $tr|ct
warnings    w@rn|ng5
here    h3r3

File with text (doc.txt):

Do you like use warnings and strict?
I do not like use warnings and strict.
Do you like them here or there?
I do not like them here or there?
I do not like them anywhere.
I do not like use warnings and strict.
I will not obey your good coding practice edict. 

The perl script:

#!/usr/bin/perl

use strict;
use warnings;
open (fh_hash, "<", "hash.tsv") or die "could not open file $!";
my %hash =();
while (<fh_hash>)
{
    s/\n//;
    s/\r//;
    my @tmp_hash = split(/\t/);
    $hash{ @tmp_hash[0] } = @tmp_hash[1];
}
close (fh_hash);
open (fh_in, "<", "doc.txt") or die "could not open file $!";
open (fh_out, ">", "doc.out") or die "could not open file $!";
while (<fh_in>)
{
    foreach my $key ( keys %hash )
    {
        s/$key/$hash{$key}/g;
    }
    print fh_out;
}
close (fh_in);
close (fh_out);
  • 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-08T10:33:29+00:00Added an answer on June 8, 2026 at 10:33 am

    One problem with

    for my $key (keys %hash) {
        s/$key/$hash{$key}/g;
    }
    

    is it doesn’t correctly handle

    foo => bar
    bar => foo
    

    Instead of swapping, you end up with all “foo” or all “bar”, and you can’t even control which.

    # Do once, not once per line
    my $pat = join '|', map quotemeta, keys %hash;
    
    s/($pat)/$hash{$1}/g;
    

    You might also want to handle

    foo  => bar
    food => baz
    

    by taking the longest rather than possibly ending with “bard”.

    # Do once, not once per line
    my $pat =
       join '|',
        map quotemeta,
         sort { length($b) <=> length($a) }
          keys %hash;
    
    s/($pat)/$hash{$1}/g;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the two following text files: First one: chr10 1000 1001 DEL 2.4807
I want to append two text files together. I have one file with a
I have two variables, one with text, and another with patterns. And I want
So I have two files, one that contains my text, and another which I
I have two folders, one full of images and another full of text files.
I have two large identical-sized files. One is ASCII plain text, and the other
I have two layouts(xml files) and I want to flip from one page to
I would like to have two separated Javascript files. One for IE and another
I have two files, one is in the webroot, and another is a bootstrap
I have two files one with senators who are retiring and one of complete

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.