Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense.

My problem is this:
I am attempting regular expression matching and replacement on a file that has a hidden character in the word I am trying to replace.

The character is ^A. It is in the word co^Amputer . It shows up multiple times, and all times has ^A in it.
My code is:


#!/usr/bin/perl
use strict;
my $rules ="/Users/caitlin/scripts/challenge.txt";

open my $FH,'<', $rules or die $!;

while  (my $line = <$FH>){
        $line =~ "s/computer/WINNER/g";
        print $line;
}

According to my father, who is not available right now, I should be able to match and replace with method of matching that I am already familiar with, but it evades me.

And I did searches to try and find ways to match it, but I received answers that did not seem to apply to the issue I am currently experiencing.

I would really appreciate some help :wall:

Thank you very much!

First - you shouldn't use quotes around s/computer/WINNER/g. Second, perl allows use control chars in its strings and regexes like "\cA" for Ctrl-A.

I hope this information is enough for your assignment.

Good luck!

1 Like

Thank you very much! That did fix it! I'll keep this in mind for future reference! =D

Try:

$line =~ "s/co\cAmputer/WINNER/g";

Okay! Thanks for the help with the previous code!

I'm stuck again on something else now though. ^_^;

I have to add the values of an array @save that has numbers in it.

#! usr/bin/perl
use strict;

my $rules ="/Users/caitlin/scripts/challenge2.txt";
open my $FH, '<', $rules or die $!;
my @numbers;
my @save;


while (my $line = <$FH>){
        @numbers = split(m/[^0-9]+/, $line);
        push (@save, @numbers);

}

print @save;

It's not done yet because I have to add all the intergers in the array. The challenge2.txt contains text and numbers that I had to split. The result has empty spaces in it.

I need to take each result from the loop and add the next result to it. And as stated before, I'm dense... :wall: My attempts at it haven't amounted to executable code.

Help would be very much appreciated!
Thank you! :slight_smile: