Wassup Monks, heres an interesting (to some) problem i need help with...

I have a some code that splits a diagram into 2 arrays @leftCon & @rightCon. In their respective arrays, left and right connectors are contained.

e.g.
@leftCon = (left123, left456); @rightCon = (right123, right456, right789);

I also have a file that shows the ping connectivity between them, which looks like this:

left123:right123
left123:right456
right123:right456
right789:left456
right789:left123

Note that the left & right connectors can be on either side of the ':', i.e. left connectors are not always on the left and right connectors are not always on the right, which i have tried to show (they are from and to).

What i want to do is recursively get each element of the @leftCon and find it in the file, replace/output it with the with the element number (+1) and "L", then do the same with @rightCon, except <elementno.+1>R.

So in the example i will take the first element of @leftCon, which is left123, find left123 in the file and replace it with 1L. I will then get the next element of @leftCon, which is left456, find left456 in the file and replace that with 2L. I will then do the same with @rightCon, so the final output will look like:

1L:1R 1L:2R 1R:2R 3R:2L 3R:1L

There is one thing i want to try an avoid, which is why i am having so much trouble, i dont want to use any perl extensions. The reason being is that i am using perl2exe to create an exe out of all my scripts, and it doesnt seem to like file extensions. I have set the path to point to where the modules are kept and used the 'lib' command to set the path in the script. The exe is created but then says it cant find the modules?! Whether its a problem with perl2exe or I have set it up wrongly (which is probably & usually the case) i would still like it to be done without using a perl extension if it can be.

This is the code that gets the connector IDs...it reads in a text file containing a diagram.

my @leftCon; my @rightCon; while (<>) { /^\s*\[([^]]+)\]/ and push @leftCon, $1; /\[([^]]+)\]\s*$/ and push @rightCon, $1; } my $leftnoel = @leftCon; my $rightnoel = @rightCon; for (my $x = 0; $x <= $leftnoel-1; $x++) { # remove leading tab $leftCon[$x] =~ s/\t//g; $leftCon[$x] =~ s/\s+//g; } for (my $y = 0; $y <= $rightnoel-1; $y++) { # remove leading space char $rightCon[$y] =~ s/\s+//g; }

And this is my attempt at the problem, but clearly the result produced is in the wrong order, as it will find all the left connectors first, then the right connectors. To get the from & to connectors i have read in the file and split at the :

for ( my $x = 0; $x < $numFrom; $x++) { for (my $y = 0; $y < $leftCon-1; $y++) { if (@fromCon[$x] eq @leftCon[$y]) { my $fromConv = @fromCon[$x]; $fromConv = $leftcounter."L"; push @fromConv, $fromConv; } if (@toCon[$x] eq @leftCon[$y]) { my $toConv = @toCon[$y]; $toConv = $leftcounter."L"; push @toConv, $toConv; } $leftcounter++; } for (my $y = 0; $y < $rightConnectors-1; $y++) { if (@fromCon[$x] eq @rightCon[$y]) { my $fromConv = @fromCon[$x]; $fromConv = $leftcounter."R"; push @fromConv, $fromConv; } if (@toCon[$x] eq @rightCon[$y]) { my $toConv = @toCon[$y]; $toConv = $leftcounter."R"; push @toConv, $toConv; } $rightcounter++; } } print @fromConv; print @toConv;

Hopefully someone out there can help me solve this problem and maybe shed some light on whats wrong perl2exe (or possibly me ;-))? Cheers, Steve


In reply to string manipulation with arrays by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.