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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:25:04+00:00 2026-05-25T15:25:04+00:00

I’ve never really worked with Perl before (avoided it where I could) and so

  • 0

I’ve never really worked with Perl before (avoided it where I could) and so my knowledge is sparse on the topic.

I know the script I am looking at has an array of ProductID values in @::s_Ship_sShipProducts.

I was attempting to see if any of the ProductIDs in the array began with either B or S and execute a function if so, else do another function. But what I ended up with was for each ProductID do the statement. This is what I (admittedly salvaged) had.

my $i;
for $i (0 .. $#::s_Ship_sShipProducts) # for each product
    if  ($::s_Ship_sShipProducts[$i] =~ /^(B|S)/) # Determine if B or S product
        {
        if (defined $phashBandDefinition->{'FreeOver'} && CalculatePrice() > 250)
        {$nCost = 0;}
        }
    else    {
        if (defined $phashBandDefinition->{'FreeOver'} && CalculatePrice() > $phashBandDefinition->{'FreeOver'})
        {$nCost = 0;}
        }

How could I change this so that the array was inspected to see if any ProductID was true and return true, or false if nothing matched? Then execute the appropriate function based on true or false? I have done some reading, but am still in the dark. Thanks for your time.

  • 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-25T15:25:05+00:00Added an answer on May 25, 2026 at 3:25 pm

    If the array is not too big (or you don’t care THAT much about performance), you can check if there are any matching values via grep:

    if (my @matching_elements = grep { /^(B|S)/ } @::s_Ship_sShipProducts) {
       # Better yet use /^[BS]/ expression - more idiomatic/readable
       print "Found B/S\n";
    } else {
       print "NOT Found B/S\n";
    }
    

    When done, @matching_elements would contain a list of matching IDs.

    In the rare case when the array IS too big to scan through entirely and you only need to find the first occurance, you can use any of the array search optimization strategies discussed in binary search in an array in Perl


    By the way, your approach to search works perfectly fine, you merely need to quit the loop once you found – and to boot, is one of those optimized approaches shown in the list above:

    for my $i (0 .. $#::s_Ship_sShipProducts) # for each product
        if  ($::s_Ship_sShipProducts[$i] =~ /^[BS]/) {  # Determine if B or S product
            # Execute your logic here
            last; # Found, get out of the loop.
        } 
    }
    

    NOTE: In Perl 5.10 and above, you can – instead of grep – use a so-called “smart match” operator ~~:

    if (@::s_Ship_sShipProducts ~~ /^(B|S)/) {
        # Your logic
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.