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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:30:42+00:00 2026-05-16T01:30:42+00:00

I am looking to substitute hash value with perl regular expression /e option. The

  • 0

I am looking to substitute hash value with perl regular expression /e option. The following code is not working for me.

 #!/usr/bin/perl

%hash=( 1 => "aa", 2 => "bb", 3 => "cc" );

$_="name,3";
s/^(name,(\d+).*)/$2,$hash{$1}/e;
print "$_\n";

exit 0;

I am expecting output like this:

name,3,cc

How can I make this work?

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-05-16T01:30:43+00:00Added an answer on May 16, 2026 at 1:30 am
    s/^(name,(\d+).*)/$1,$hash{$2}/;
    

    First, the parens are numbered in the order of the left paren, not the right paren. So $1 is the complete line, and $2 is the number, not the other way around.

    Second, you don’t want to use /e here. That causes the replacement to be evaluated as Perl code instead of just a string, which means that the , is treated as the comma operator and not just text. In scalar context, the comma operator evaluates both operands and returns the second one, and that isn’t what you want here. If you had said

    use warnings;
    

    Perl would have told you “Useless use of a variable in void context”, because the first expression is being ignored.

    You should always start your programs with:

    use strict;
    use warnings;
    

    Those two lines will help catch a lot of the common mistakes you might make. Sometimes you’ll need to turn them off for part of your code, but you should understand the rules before you can decide when it’s ok to break them.

    If you do that in this program, you’ll need to add a my before %hash.

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

Sidebar

Related Questions

Looking at some assembly code for x86_64 on my Mac, I see the following
#!/usr/bin/python from string import Template s = Template('$x, go home $x') s.substitute(x='lee') print s
I'm looking for method to inject value into a <c:if test> . The code
Looking for a perl one-liner what will find all words with the next pattern:
Looking at the Ehcahce implementation of net.sf.cache.JS107, I am trying to achieve the following
Looking for a control that allows to select one text value at a time
Looking at some of the code System.Linq I've come across some examples of Buffer<TSource>
I am looking for a substitute for the HTML <pre> tag. Using the <pre>
One can use a command such as the following to substitute the word before
I was looking for a substitute of gotoxy() for gcc compiler and found this

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.