Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Create a expression to cmpare a formal against a string

by learnP (Initiate)
on Nov 12, 2013 at 00:38 UTC ( #1062087=perlquestion: print w/replies, xml ) Need Help??

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

I have a input file which I am reading into a hash table, The input file is as below

_DATA_ MANAGEMENT=IDL NORTH=IDL,NORTHERN||VIDL,NORTH||IDL,NORTH SOUTH=IDL,SOUTHERN||CIDL,SOUTH

Now for each key I have to check if another string say instance (This is also a string) contains the value. for example I check 15000 such instances against IDL which is the first value and then against (IDL AND NORTHERN )OR(VIDL OR NORTH) AND ( IDL OR NORTH )

Condition= TRUE if $instance contains (IDL AND NORTHERN) OR (VIDL AND NORTH) OR (IDL AND NORTH)

Please note that Value can many such OR conditions I have such 15000 instances against which I have to compare each value so need a fast method. I was thinking of passing the value to a function which would return a regex against which I can check but haven't been able to think of any such (I am new bee in perl)

What I am trying to achieve ?

i have a config file which has data like above (sample data, the number of OR can vary so can the number of commas. commas are nothing but 'AND' conditions) I have another array (instance) which can have number of strings ; now I want to check each one of these instances against each of the values in the hash; if the string in the array CONTAINS (not a direct match) the first value I move it to a new hash table; further check all the fields in the array against the value and do the same for all the present values from the config file

<b>Expected value of $instance</b> 1. IDL_MANAGEMENT_101001 2. IDL_NORTH_TX 3. IDL_SOUTH 4. VIDL_NORTH 5. ABX_PERL _DATA_ MANAGEMENT=IDL||VIDL NORTH=IDL,NORTHERN||VIDL,NORTH||IDL,NORTH SOUTH=IDL,SOUTHERN||CIDL,SOUTH
WorkFlow it will match IDL (the first value) against each instant and create a hash with $instance as key and MANAGEMENT as value ; in next iteration when the key is NORTH check is done $instance contains (IDL AND NORTHERN) OR (VIDL AND NORTH) OR (IDL AND NORTH) so IDL_NORTH_TX will have a new value NORTH in the resultant hash...and hence forth
Resultant Hash IDL_MANAGEMENT_101001 ==> MANAGEMENT IDL_NORTH_TX ==> NORTH IDL_SOUTH ==> SOUTH VIDL_NORTH ==> NORTH

Replies are listed 'Best First'.
Re: Create a expression to cmpare a formal against a string
by GrandFather (Saint) on Nov 12, 2013 at 00:52 UTC

    So show us the code you have and the result you want to achieve. Better still, show us a small (but complete) test program that just attempts the thing you are having trouble with.

    You description doesn't actually make your problem very clear. The intent of getting you to provide some sample code is so you can show us how the pieces of your problem hang together.

    True laziness is hard work
      Doing that , not getting lot of ideas ; hence the ask

        Even telling us why you are doing it would help somewhat. Context can help a lot to make sense of an otherwise poor explanation. Bear in mind that we don't have the background to the problem that you do.

        True laziness is hard work
Re: Create a expression to cmpare a formal against a string
by Kenosis (Priest) on Nov 12, 2013 at 01:24 UTC

      Did try other forums as well, Unable to find a convinicng idea; Yes I am new in perl

Re: Create a expression to cmpare a formal against a string
by hdb (Monsignor) on Nov 12, 2013 at 07:12 UTC

    Trying to understand your problem: if your $instance string would contain IDL only, then it would fit the condition MANAGEMENT. If the string contains CIDL and SOUTH it would fit the condition SOUTH. Is this correct?

    How do you want to deal with the string abc VIDL abc? It does contain IDL and thus would fit the condition MANAGEMENT?

    It would really be useful to post a number of examples for your $instance strings and the desired outcomes, including some strings that do NOT match anything.

Re: Create a expression to cmpare a formal against a string
by hdb (Monsignor) on Nov 12, 2013 at 09:44 UTC

    Your example output is not quite correct and I think your problem is still not fully specified. However, have a look at this and see whether you can adapt it for your purposes.

    use strict; use warnings; sub check_condition { my( $inst, $cond, $ors ) = @_; OR: for ( @$ors ) { $inst !~ /$_/ and next OR for @$_; return 1; } return 0; } sub check { my( $inst, $list ) = @_; return grep { check_condition $inst, $_, $list->{$_} } keys %$ +list; } my %conditions = map { /^(.*?)=(.*)$/; $1 => [ map { [ split /,/ ] } split /\|\|/, $2 + ]; } <DATA>; my @instances = qw( IDL_MANAGEMENT_101001 IDL_NORTH_TX IDL_SOUTH VIDL_ +NORTH ABX_PERL ); print "$_:", join(",",check( $_, \%conditions )), "\n" for @instances; __DATA__ MANAGEMENT=IDL NORTH=IDL,NORTHERN||VIDL,NORTH||IDL,NORTH SOUTH=IDL,SOUTHERN||CIDL,SOUTH

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1062087]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-01-28 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (74 votes). Check out past polls.

    Notices?