Bennco99 has asked for the wisdom of the Perl Monks concerning the following question:

I am new to Perl. Trying to teach myself Perl as my first language, so please bear with me..

I have this variable and I am trying to cut off a carriage return and a "^" off of the end. I have tried chop & chomp, with no luck and cannot seem to be able to find match with regexp.

$user = lc"SmithB2 ^"; if ($user =~ "/^\D*\d\i\$/") {

I am on a tight time frame to get this done. Any and all help is greatly appreciated.

Thank you,
Jonathan

Code tags and general formatting added by GrandFather

2006-07-26 Retitled by GrandFather, as per Monastery guidelines

Replies are listed 'Best First'.
Re: help with REGEXP to remove carriage return and caret from end
by ptum (Priest) on Jul 25, 2006 at 15:02 UTC

    In general, chomp() can be trusted to remove a trailing carriage return from your string. If chomp() doesn't work, then perhaps your string doesn't contain what you think it does.

    A few obvious problems with your regular expression:

    • you are quoting it AND using the forward-slash characters to delimit your regex
    • you seem to be looking for a literal '$' character at the end of your string

    If I was looking for a series of word characters, I might prefer something like this, which looks for one or more characters in the word character class at the beginning of the string, and greedily captures them all into the $1 variable:

    my $trimmed_user; if ($user =~ /^(\w+)/) { $trimmed_user = $1; }

    Of course, this assumes that you won't have any characters in a user name that are not in the word character class (A-Za-z0-9_).


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
      Thank you ptum. Your recommendation did the trick. Thank you for your help.
Re: help with REGEXP to remove carriage return and caret from end
by philcrow (Priest) on Jul 25, 2006 at 15:04 UTC
    ^ is a special character in regexes (it usually means match the following, but only at the start of the string). To make it literal, guard it with a backslash:
    if ( $user =~ /(.*)\^/ ) { my $user_name = $1; }
    My only other advice is to concentrate on what you are keeping when you use a match and on what you are replacing when you use a replace (that is when using s///).

    Phil

      For whatever reason when I paste the example into the thread it doesn't display right..

      But the results that I am getting is the user name "SmithB" then on the end of that it adds a square or carriage return, then adds a carrot "^" on the end of that.

      I need to extract the username (without the carriage return and ^) and write that to a text file.

      Here the whole script... I am creating a list of the parameters being passed. Then I need to take ARGV 7 and extract the user name minus the carriage return and /^

      open (TEST,">c:/temp/argtest6.txt") or die "Failed to open argtest.txt + file"; $count = 0; foreach $element (@ARGV) { print TEST $count,$element; $count += 1; } $user = lc("$ARGV[7]"); chomp ($user); if ($user =~ "/^\........\d\$/\n") { print TEST ".. You have a match for $user /\n"; } else { print TEST "... NO match for $user /\n"; } print TEST $user; close(TEST)

      Sorry guys, I am new at this, so please bear with me.... And thanks for the help

      Code tags and general formatting added by GrandFather

        This looks a little better
        open (TEST,">c:/temp/argtest6.txt") or die "Failed to open argtest.txt + file"; $count = 0; foreach $element (@ARGV) { print TEST $count,$element; $count += 1; } $user = lc("$ARGV7"); chomp ($user); if ($user =~ "/^\........\d\$/\n") { print TEST "
        howcome my thread is compacting so much? Is there something I am missing to make it easier to read?
Re: help with REGEXP to remove carriage return and caret from end
by davorg (Chancellor) on Jul 25, 2006 at 15:03 UTC

    If you want to remove an end of line character from the end of a string then chomp is your best bet. If that's not working then you need to tell us more about the data you're getting. Perhaps there's something else there other than your operating system's end of line character.

    And I'm slightly confused by the rest of your question. You say that you want to remove '^' from the end of the string, but the sample data you give doesn't have that character in it.

    I think that in order for us to be any real help to you, you'll need to expand a bit on what you're trying to do. Give us a complete (but small) example program that we can run and explain what unexpected behaviour you are seeing.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: help with REGEXP to remove carriage return and caret from end
by CountZero (Bishop) on Jul 25, 2006 at 21:25 UTC
    Another way of doing it is first doing a chop on the variable (this removes the last character of the string, whatever that character may be) and then doing a chomp on the string, which will remove the end-of-line character(s) (one or two characters depending on your system).

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Not such a smart thing generally. Chop will remove the last character regardless of what it was. If it was a multiple character sequence then you have stuffed things for chomp - it won't remove the mutliated line end sequence. If it was a single character then the chomp is not required. In neither case is chomp going to do anything useful following chop.

      chomp first to remove the line end sequence (which may comprise many characters if $/ has been altered) and then chop if you really want to always remove the last character (doesn't happen often actually).


      DWIM is Perl's answer to Gödel
        I don't agree. From the OP's example it is clear his string is built as follows: 'some_name' + 'EOL-character(s)' + '^'.

        chop removes the '^' and then chomp can do its usual job of handling the 'EOL-character(s)'.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law