rohan_532 has asked for the wisdom of the Perl Monks concerning the following question:

can i compare individual string with complete array i am comparing excel row with array using the following way, description variable contains string to match with @steps_name array

my @steps_name=("1-2 Steps", "5-7 Steps", "8-10 Steps", "11-15 Steps") +; foreach $sheet (@{$workbook->{Worksheet}}) { foreach $col ($sheet->{MinCol} .. $sheet->{MaxCol}) { if ($sheet->{Cells}[0][$col]->{Val} eq "DESCRIPTION") { $description = $col; } } foreach $row ($sheet->{MinRow}+1 .. 50) { my $db_description = $sheet->{Cells}[$row][$description]->{ +Val}; my $needle_regex = quotemeta $db_description; if (grep { /(?i)\Q$db_description\E/ } @steps_name) + { print "<br><h1>Element '$db_description' found $st +eps_name[$row+1] --- $seq_code[$row+1] !</h1></br>" ; } else { print "<br>$db_description not found </br>" } push(@excel_array,$db_description); }## end of for each }

Replies are listed 'Best First'.
Re: compare individual string with complete array
by thanos1983 (Parson) on May 31, 2017 at 09:46 UTC

    Hello rohan_532,

    First of all welcome to the monastery.

    I was reading through your question and it is not so clear to me, maybe another monk can understand but what you are trying to achieve?

    Since you are already comparing your array with the string:

    if (grep { /(?i)\Q$db_description\E/ } @steps_name)

    What is that you want to get. Can you provide us a pseudo code of the input lines and the expected output. Provide us a sample of input data so we can replicate your problem. In general please update your question based on How do I post a question effectively?. Do not take this the wrong way help us more to help you.

    Update: rohan_532 Do Not Re Post Your Questions on top of other questions that you have posted Re^2: regex for case insensitive. This will produce chaos on your questions and answers and also people that might refer to these questions on future.

    Looking forward to your update.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      hello in my program the $db_description is a variable which contains values such as $db_description="MOVE 1-2 STEPS" i want to check if this $db_description is present or matches in the above mentioned @steps_name array. the code i use only matches exact string ,I need any case insensitive even if 1-2 is present in string it should match with array

        Hello again rohan_532,

        You are mixing many questions and issues here.

        Having a case sensitive string is something different that comparing with a regex with numbers. So take a step backwards and analyze it.

        If you have an upper case string and you want to convert it to a lower case you can simply use lc or the opposite assume that you have a lower case string and you want to convert to upper case uc. Or even igf you manage to split the string into pieces and you want to make only the first letter of the string upper case ucfirst.

        Regarding the regex part of your question:

        #!usr/bin/perl use strict; use warnings; my @steps_name = ("1-2 Steps", "5-7 Steps", "8-10 Steps", "11-15 Steps +"); foreach my $element (@steps_name) { print "Matched with ".$element."\n" if ($element =~ /[0-9]-[0- +9]/); } __END__ $ perl test.pl Matched with 1-2 Steps Matched with 5-7 Steps Matched with 8-10 Steps Matched with 11-15 Steps

        As you can see this will match with all elements, so you need to be a bit more specific on what you want to compare against. For example 1-2 Steps only, or multiple choices?

        Again I am looking forward to update your question with more details.

        Update: Have you read the answers that marinersk and AnomalousMonk have already provided you on your previous question regex for case insensitive? I looks like they have already addressed your problem.

        Hope this is more clear now, programming gives you infinite possibilities, you have to tell us exactly what is the desired output.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: compare individual string with complete array
by AnomalousMonk (Archbishop) on May 31, 2017 at 20:10 UTC
Re: compare individual string with complete array
by locked_user sundialsvc4 (Abbot) on May 31, 2017 at 16:48 UTC

    As an aside, when you are searching an array for a particular value, using a loop, you probably want to break out of the loop once you find a match.   This will ensure that you find the first hit, and it also avoids wasting time.   The statement for breaking out of a loop in Perl is called last.

    undef $description; foreach $col ($sheet->{MinCol} .. $sheet->{MaxCol}) { if ($sheet->{Cells}[0][$col]->{Val} eq "DESCRIPTION") { $description = $col; last; } } if (defined($description)) { ... we found a match ... }

    In this example, we start by explicitly giving $description the “undefined” value.   (So that, no matter what happens, we are certain as to all possibilities of what the final value might be.)   Then, we begin a search.   If a hit is found, the $description variable is populated, and then we immediately terminate the foreach loop.   After the loop is over (one way or the other), there are now two possibilities.   If $description now has a non-undefined value, then it represents the first matching entry.   If it is still undefined, then no match was found.

    If you choose not to use the last statement, then you will have found the last matching entry ... the hard way.