Results 1 to 6 of 6

Thread: Trying to understand some Perl 5 regexp I found in a book by breaking the code down

  1. #1
    Join Date
    Mar 2007
    Beans
    1,052

    Trying to understand some Perl 5 regexp I found in a book by breaking the code down

    I am trying to analyze some syntax I found in a book by breaking it down and making code that uses a subset of those special characters but, I have not been able to manage to get anything running successfully.

    Here is the syntax I found in the book.:
    Code:
    #!/usr/bin/perl
    
    @lines = `perldoc -u -f atan2`;
    foreach (@lines) {
    	s/\w<([^>]+)>/\U$1/g; #This is the line that I am most confused about.
    	print;
    }
    For example, here is my failed attempt to modify the above code sample so that it simply converts all the letters to uppercase letters.:
    Code:
    #!/usr/bin/perl
    
    #@lines = `perldoc -u -f atan2`;
    @lines = "one\ntwo\n";
    foreach (@lines) {
    	#s/\w<([^>]+)>/\U$1/g; #This is the line that I am most confused about.
    	s/\U$1/g;
    	print;
    }
    When I attempt to run this code, I get the following error message.:
    Substitution replacement not terminated at /tmp/testperlfile.pl line 7.
    What must I do differently in order to make my broken-down code work as intended?

    Any help in successfully breaking down what I found in the book in order to fully understand it would be greatly appreciated!
    Apps for Ubuntu (outdated) ---> http://cid-23a283fc1010a1bb.skydrive...%20Wine|6?uc=1
    Use Mnemosyne to Study for School!

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Trying to understand some Perl 5 regexp I found in a book by breaking the code do

    Well I don't speak perl but I would think that s/\U$1/g is only half of a substitution - it needs to be in the form s/thing to match/thing to replace it with/g

    To replace all characters, you still need to specify a thing to match, I think - but it can be .* (any number of any characters) i.e.

    Code:
    $ echo "something<or other>" | perl -e 'while (<>) { s/(.*)/\U$1/g; print }'
    SOMETHING<OR OTHER>
    or (since you don't really need the grouping) you can use $& to replace the whole match

    Code:
    $ echo "something<or other>" | perl -e 'while (<>) { s/.*/\U$&/g; print }'
    SOMETHING<OR OTHER>

  3. #3
    Join Date
    May 2011
    Beans
    253
    Distro
    Ubuntu 15.10 Wily Werewolf

    Re: Trying to understand some Perl 5 regexp I found in a book by breaking the code do

    This page has an explanation of what steeldriver is talking about:

    "Changing case with regex"
    http://vim.wikia.com/wiki/Changing_c...ar_expressions
    Kevin Harper
    http://www.kevinharper.com/


    Ubuntu: Because rebooting is ONLY for installing hardware

  4. #4
    Join Date
    Dec 2011
    Beans
    28
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Trying to understand some Perl 5 regexp I found in a book by breaking the code do

    Hi,

    Here's one way to try it:

    Code:
    #!/usr/bin/perl
    use warnings;
    use strict;
    my @lines = "one\ntwo\n";
    
    foreach (@lines) {
    	
    	s/($_)/\U$1/g;
    	print;
    }
    The line - s/($_)/\U$1/g; was missing the / (highlighted in red). That's because, the part before the / is what will be Substituted with the part AFTER the /. the () around $_ enable the $1 to sort of capture it.

    The $_ is Perl's default.

    I dont clearly remember, but looks like this is from the first few pages of Learning Perl Book. Its an awesome book. Correct me if I am wrong, but it looks like you jumped off straight to the chapter on Regular Expressions. What I would suggest is learn the same was as the chapters progress. Meaning Chapter 1 first, then 2nd and then 3rd and so on.

    Also, please include "use warnings" and "use strict" as it will save you a lot of trouble.

    Hope this helps. My sincere request would be to try out each and every exercise given at the end of the chapters.
    Last edited by foobantu; December 26th, 2012 at 08:37 AM.
    Ubuntu 12.04 LTS (64 Bit) Lenovo Z570 8 GB RAM.

  5. #5
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Trying to understand some Perl 5 regexp I found in a book by breaking the code do

    #@lines = `perldoc -u -f atan2`;
    @lines = "one\ntwo\n";
    Apart from the regex issues, I just want to point out that the original creates an array with many elements, but the replacement creates an array with precisely one element. So the subsequent loop is executed once, i.e. you don't really need a loop.

    Of course, simply to translate everything to uppercase, it makes no functional difference whether you process line by line or all at once, but for other regex patterns it might.

    If you wanted something more like the original then you might do:
    Code:
    my @lines = ("one\n", "two\n");
    Furthermore, if all you want to do is convert to uppercase, you don't even need a regex. Simply:
    Code:
    foreach (@lines) {
    	print uc;
    }
    This is Perl: there's more than one way to do it

  6. #6
    Join Date
    Dec 2011
    Beans
    28
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Trying to understand some Perl 5 regexp I found in a book by breaking the code do

    +1 @spjackson for that one.
    Ubuntu 12.04 LTS (64 Bit) Lenovo Z570 8 GB RAM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •