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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:32:20+00:00 2026-06-04T05:32:20+00:00

Is it possible to have a foreach loop with a condition in Perl? I’m

  • 0

Is it possible to have a foreach loop with a condition in Perl?

I’m having to do a lot of character by character processing – where a foreach loop is very convenient. Note that I cannot use some libraries since this is for school.

I could write a for loop using substr with a condition if necessary, but I’d like to avoid that!

  • 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-04T05:32:24+00:00Added an answer on June 4, 2026 at 5:32 am

    You should show us some code, including the sort of thing you would like to do.

    In general, character-by-character processing of a string would be done in Perl by writing

    for my $ch (split //, $string) { ... }
    

    or, if it is more convenient

    my @chars = split //, $string;
    for (@chars) { ... }
    

    or

    my $i = 0;
    while ($i < @chars) { my $char = $chars[$i++]; ... }
    

    and the latter form can support multiple expressions in the while condition. Perl is very rich with different ways to do the similar things, and without knowing more about your problem it is impossible to say which is best for you.


    Edit

    It is important to note that none of these methods allow the original string to be modified. If that is the intention then you must use s///, tr/// or substr.

    Note that substr has a fourth parameter that will replace the specified part of the original string. Note also that it can act as an lvalue and so take an assignment. In other words

    substr $string, 0, 1, 'X';
    

    can be written equivalently as

    substr($string, 0, 1) = 'X';
    

    If split is used to convert a string into a list of characters (actually one-character strings) then it can be modified in this state and recombined into a string using join. For instance

    my @chars = split //, $string;
    $chars[0] = ‘X’;
    $string = join ”, @chars;

    does a similar thing to the above code using substr.

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

Sidebar

Related Questions

Possible Duplicate: Javascript Array.forEach HowTo break? Given that I have a forEach loop, how
Possible Duplicate: Breaking out of a nested loop I have this code foreach (___)
I have a foreach loop that I am parallelizing and I noticed something odd.
I have a foreach loop that has try catch and finally. it goes through
I have a simple foreach loop that goes through the productID's I have stored
Possible Duplicate: How would I stop this foreach loop after 3 iterations? I have
I have a foreach loop that looks like this: foreach (var line in theCP4UnknownList.Distinct())
I have a foreach loop in a view that display a list of files
Is it possible to have a foreach loop in PHP with multiple index variables,
I have here a foreach loop that displays images from a database onto a

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.