CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Perl Substitution variable

 



yellowman
Novice

Dec 12, 2006, 12:29 PM

Post #1 of 7 (1341 views)
Perl Substitution variable Can't Post

I have the following code below

$temp_array1[0] = "Nexus";
$temp_array1[1] = "XXXX";

$work1 = "XXXX"
$work2 = "XXXX CCCCC"

if ($work2 =~ /$work1/){
$work1 = $temp_array1[0];
$work2 =~ s/$work2/$work1/g;
}

Now if $temp_array1[1] is contained in the string $work2 swap out the encrypted value in $work2
with its corresponding unencrypted value.

At the beginnining of the code I expect the output of $work2 to be XXXX CCCCC, but once
I pass through my iff statement I want the string to be the unencrypted value....which would be

$work2 = "Nexus CCCCC"

BUT, everytime I run the code the end result of $work2 is always just "Nexus" (without the other encrypted word following it). Any ideas what may be wrong? Thanks.


KevinR
Veteran


Dec 12, 2006, 1:19 PM

Post #2 of 7 (1340 views)
Re: [yellowman] Perl Substitution variable [In reply to] Can't Post

 $work2 =~ s/$work1/$temp_array1[0]/;
-------------------------------------------------


yellowman
Novice

Dec 12, 2006, 1:52 PM

Post #3 of 7 (1338 views)
Re: [KevinR] Perl Substitution variable [In reply to] Can't Post

Hmmmmm.......something is still amiss....

If I code the above.....I get the original encrypted string.....XXXX CCCCC

$work2 =~ s/$work1/$temp_array1[0]/;

If I change the $work1 to $work2 such as...

$work2 =~ s/$work2/$temp_array1[0]/;

I get "Nexus" as the output.....which is closer....

Any ideas?

Thanks.


KevinR
Veteran


Dec 12, 2006, 8:59 PM

Post #4 of 7 (1334 views)
Re: [yellowman] Perl Substitution variable [In reply to] Can't Post


Code
$temp_array1[0] = "Nexus"; 
$temp_array1[1] = "XXXX";

$work1 = "XXXX";
$work2 = "XXXX CCCCC";

if ($work2 =~ /$work1/){
$work2 =~ s/$work1/$temp_array1[0]/g;
}
print $work2;

-------------------------------------------------


yellowman
Novice

Dec 13, 2006, 1:08 PM

Post #5 of 7 (1328 views)
Re: [yellowman] Perl Substitution variable [In reply to] Can't Post

Well, I'm closer than I was before, but it only seems to unencrypt 1 or two elements out
of each string. A copy of the encrypted file and the key are attached.

Here is the complete code:


Code
 #!/usr/local/bin/perl 
print ("What is the dictionary filename (.dict)? ");
$d_file = <STDIN>; unless (open (DICT, $d_file)) {
die ("The selected file cannot be found");
}



@dict = <DICT>;

print ("What is the Usage Report filename (.txt)? ");
$t_file = <STDIN>;



unless (open (TEXT, $t_file)) {
die ("The selected file cannot be found");
}



@txt = <TEXT>;


&cleanup (@txt);
&compare(@txt, @dict);




sub cleanup {
my ($count) = 0;
my ($index) = 0;
my ($string);
my ($size1);
my @temp_array;
my @temp_array1;

while ($txt[$count] ne ""){

if ($txt[$count] =~ /^A[CB]/) {
$temp_array[$index] = $txt[$count];
$index++;
}
$count++;
}
@txt = @temp_array;

$size1 = @txt;
for($index=0; $index <= ($size1-1); $index++){
$string = $txt[$index];
$string =~ tr/(|)|,//d;
@temp_array1[$index] = $string;
}
@txt = @temp_array1;
}




sub compare {
my ($count);
my ($index);
my ($work1);
my ($work2);
my ($size1);
my ($size2);
my @temp_array1;
my @temp_array2;
my @temp_array3;

$size1 = @dict;



for($count=0; $count <= ($size1-1); $count++){

$string = $dict[$count];
@temp_array1 = split(/ +/, $string);
@temp_array2 = @txt;
$size2 = @temp_array2;

for($index=0; $index <= ($size2-1); $index++){
$work1 = $temp_array1[1];
$work2 = $temp_array2[$index];

chop($work1);

if ($work2 =~ /$work1/){
$work2 =~ s/$work1/$temp_array1[0]/gx;
$temp_array3[$index] = $work2;
}
}
}
print("@temp_array3\n");
}



It has to be something small that I am overlooking. It's as if my loops don't go through enough iterations to decrypt the contents or the txt file.
Attachments: susvr0302_wrsd_report_11-01-2006_01-29-2007.dict (3.02 KB)
  susvr0302_wrsd_report_11-01-2006_01-29-2007.txt (5.96 KB)


KevinR
Veteran


Dec 13, 2006, 1:55 PM

Post #6 of 7 (1327 views)
Re: [yellowman] Perl Substitution variable [In reply to] Can't Post

I will look at your files and code later if I get the time.
-------------------------------------------------


yellowman
Novice

Dec 14, 2006, 4:59 AM

Post #7 of 7 (1325 views)
Re: [yellowman] Perl Substitution variable [In reply to] Can't Post

Sounds good....

Until then I will beat on it some more and see if I can figure it out. Thanks.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives