Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

splitting data

by amma (Novice)
on Apr 23, 2013 at 08:51 UTC ( [id://1030078]=perlquestion: print w/replies, xml ) Need Help??

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

I want to split the following data automatically

tlp_p2 <= (((((((NOT(tlp_p0(0) XOR tlp_add_p0(0)) AND NOT(tlp_p0(1) XOR tlp_add_p0(1))) AND NOT(tlp_p0(2) XOR t +lp_add_p0(2))) etc

like $var1 = (NOT(tlp_p0(0) XOR tlp_add_p0(0))

$var2 = (NOT(tlp_p0(1) XOR tlp_add_p0(1))

Can somebody help with initial code: limitations: 1. I do not know how many variables i should split in first place. I mean the logic may contain 0,1....12 bits

Replies are listed 'Best First'.
Re: splitting data
by kcott (Archbishop) on Apr 23, 2013 at 10:00 UTC

    G'day amma,

    Welcome to the monastery.

    I had a few issues with your example.

    • $var2 begins with a '(' (left parenthesis) but that leading character is not present in the original string at that position.
    • Should tlp_add_p0(0)) have an additional trailing ')' (right parenthesis) to match the others?
    • Using etc is a bit vague so I'm guessing here. A better way to show this sort of thing is with an ellipsis, e.g. "A, B, C, ..., Z".
    • Is the pattern 'NOT(tlp_p0(N) XOR tlp_add_p0(N))' common throughout or are there other formats?

    If the repeated, internal pattern is the same throughout, you can do something like this:

    $ perl -Mstrict -Mwarnings -E ' my $x = qq{tlp_p2 <= (((((((NOT(tlp_p0(0) XOR tlp_add_p0(0)) AND\n +}; $x .= q{ NOT(tlp_p0(1) XOR tlp_add_p0(1))) AND }; $x .= q{NOT(tlp_p0(2) XOR tlp_add_p0(2))) etc}; say for $x =~ /(NOT\(tlp_p0\(\d+\) XOR tlp_add_p0\(\d+\))/gm; ' NOT(tlp_p0(0) XOR tlp_add_p0(0) NOT(tlp_p0(1) XOR tlp_add_p0(1) NOT(tlp_p0(2) XOR tlp_add_p0(2)

    If you want to use split, I'd chop off any leading and trailing dross then split something like this:

    $ perl -Mstrict -Mwarnings -E ' my $x = qq{tlp_p2 <= (((((((NOT(tlp_p0(0) XOR tlp_add_p0(0)) AND\n +}; $x .= q{ NOT(tlp_p0(1) XOR tlp_add_p0(1))) AND }; $x .= q{NOT(tlp_p0(2) XOR tlp_add_p0(2))) etc}; $x =~ s/\A\w+ <= \(+(.*?)\) etc\z/$1/ms; say for split /\)\s*AND\s+/ => $x; ' NOT(tlp_p0(0) XOR tlp_add_p0(0) NOT(tlp_p0(1) XOR tlp_add_p0(1)) NOT(tlp_p0(2) XOR tlp_add_p0(2))

    -- Ken

Re: splitting data
by Anonymous Monk on Apr 23, 2013 at 08:57 UTC
Re: splitting data
by MidLifeXis (Monsignor) on Apr 23, 2013 at 12:41 UTC

      I took this problem as inspiration to write a little script to visualize matching parantheses. I probably got carried away and it is not helping on the original problem.

      use strict; use warnings; my $tlp_p2 = "(((((((NOT(tlp_p0(0) XOR tlp_add_p0(0)) AND NOT(tlp_p0(1 +) XOR tlp_add_p0(1))) AND NOT(tlp_p0(2) XOR tlp_add_p0(2)))"; my $level = 0; my $tab = "| "; my %action = ( '(' => sub { print "\n", $tab x ++$level, shift }, ')' => sub { print "\n", $tab x $level--, shift }, 'default' => sub { print shift }, ); ( $action{$_} // $action{'default'} )->($_) for $tlp_p2 =~ /./g;

      The last line reads each character of the string $tlp_p2 individually and calls the corresponding sub from hash %action.

Re: splitting data
by hdb (Monsignor) on Apr 23, 2013 at 09:07 UTC

    If you do not care about balanced parantheses it is simple:

    use strict; use warnings; my $tlp_p2 = "(((((((NOT(tlp_p0(0) XOR tlp_add_p0(0)) AND NOT(tlp_p0(1) XOR tlp_add_p0( +1))) AND NOT(tlp_p0(2) XOR tlp_add_p0(2)))"; my @var = split /\s*AND\s*/, $tlp_p2; $,="\n"; print @var;

    BUT: already your proposed split for $var1 and $var2 does not have balanced parantheses. In fact, the closing ) at the end of $var2 corresponds to the opening ( at the beginning of $var1 if I have counted correctly. How do you want to deal with that situation?

    Remark: If the terms in your equation are always of this type, you can create them bottom-up easily instead of extracting them from the equation...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found