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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:41:29+00:00 2026-05-15T16:41:29+00:00

I am writing a script in Perl and have a question about Perl’s foreach

  • 0

I am writing a script in Perl and have a question about Perl’s foreach construct.

It appears that if you change one of the loop variables it changes in the actual array. Is this in fact the case, or have I done something completely wrong?

I want to change a string like abc.abc#a to abc_abc_a (underscores for non alpha-numeric characters), but I need to preserve the original value in the array for later use.

I have code that looks something like this:

@strings = ('abc.abc#a', 'def.g.h#i');
foreach my $str (@strings){
    $str =~ s/[^0-9A-Za-z]/_/g;
    print $str, "\n"; #Actually I use the string to manipulate files.
}

I could solve the problem by doing the following:

@strings = ('abc.abc#a', 'def.g.h#i');
foreach my $str (@strings){
    my $temp = $str; #copy to a temporary value
    $temp =~ s/[^0-9A-Za-z]/_/g;
    print $temp, "\n"; #$str remains untouched...
}

but is there a more efficient way to accomplish this?

Thank you very much!

  • 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-15T16:41:30+00:00Added an answer on May 15, 2026 at 4:41 pm

    You’re not crazy; this is normal behaviour. See perldoc perlsyn under Foreach loops:

    If any element of LIST is an lvalue, you can modify it by modifying VAR inside the
    loop. Conversely, if any element of LIST is NOT an lvalue, any attempt to modify that
    element will fail. In other words, the “foreach” loop index variable is an implicit
    alias for each item in the list that you’re looping over.

    Other loop iterators such as map have similar behaviour:

    map BLOCK LIST  
    map EXPR,LIST
    

    …
    Note that $_ is an alias to the list value, so it can be used to modify the
    elements of the LIST. While this is useful and supported, it can
    cause bizarre results if the elements of LIST are not
    variables. Using a regular “foreach” loop for this purpose would be
    clearer in most cases.
    See also “grep” for an array composed of those items
    of the original list for which the BLOCK or EXPR evaluates to true.

    You could rewrite your code this way, which would at least save you from adding an extra line:

    my @strings = ('abc.abc#a', 'def.g.h#i');
    foreach my $str (@strings){
        (my $copy = $str) =~ s/[^0-9A-Za-z]/_/g;
        print $copy, "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.