Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: string manipulation with arrays
by jmcnamara (Monsignor) on Oct 22, 2003 at 10:46 UTC | |
|
Re: string manipulation with arrays
by TomDLux (Vicar) on Oct 22, 2003 at 15:54 UTC | |
|
Re: string manipulation with arrays
by Anonymous Monk on Oct 22, 2003 at 09:26 UTC |