Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: remove first and last character of string

by flieckster (Scribe)
on Oct 14, 2020 at 02:43 UTC ( #11122803=note: print w/replies, xml ) Need Help??


in reply to Re: remove first and last character of string
in thread remove first and last character of string

Thanks Bill, i like the idea of using Regex, but the example provided isn't removing quotes, it seems to remove most of the string?
  • Comment on Re^2: remove first and last character of string

Replies are listed 'Best First'.
Re^3: remove first and last character of string (updated)
by AnomalousMonk (Bishop) on Oct 14, 2020 at 03:27 UTC

    ... the example provided isn't removing quotes, it seems to remove most of the string ...
    I don't understand. The example code removes balanced quotes from the ends of a string. What return do you want from '"654321_1111"'?

    Update: Note that this substr solution removes any characters from the ends of a string, whereas this solution removes only balanced double-quotes from the ends of a string, and this solution removes only double-quotes, balanced or not, from the ends of a string. It's a question of exactly what you want.


    Give a man a fish:  <%-{-{-{-<

      It's a question of exactly what you want.

      Precisely so. This is why (IMHO) How to ask better questions using Test::More and sample data should be one of the first posts read by all wisdom seekers. Here's a fuller set of tests building on the example of BillKSmith. It's up to flieckster to flesh out the @tests array with more exhaustive data sets.

      use strict; use warnings; use Test::More; my @tests = ( { have => '"654321_1111"', want => '654321_1111' }, ); plan tests => 5 * @tests; for my $t (@tests) { is billksmith ($t->{have}), $t->{want}, 'BillKSmith'; is syphilis ($t->{have}), $t->{want}, 'syphilis'; is grandfather ($t->{have}), $t->{want}, 'GrandFather'; is rsfalse ($t->{have}), $t->{want}, 'rsFalse'; is hippo ($t->{have}), $t->{want}, 'hippo'; } sub billksmith { my $string = shift; my $regex = qr/^\"(.+)\"$/; my ($got) = $string =~ m/$regex/; return $got; } sub syphilis { my $str = shift; substr ($str, 0, 1, ''); chop $str; return $str; } sub grandfather { my $str = shift; $str =~ s/^"|"$//g; return $str; } sub rsfalse { my $string = shift; for (1 .. 2) { $string = reverse $string; chop $string; } return $string; } sub hippo { my $str = shift; $str =~ tr/"//d; return $str; }

      🦛

      Hi Bill, the output for the first post resulted in this:
      1..1 ok 1 - Remove quotes
      first off thank you for taking the time to answer my post! I appreciate any help i can get for sure. I was hoping to remove the quotes from the string only, "654321_1111" would return 654321_1111. you can see where i was confused? thank you again.

        Let us assume you were confused because you weren't expecting tests.

        A piece of code which accepts input and produces output is useful to the end user, but slightly less so to the developer trying to determine if a particular requirement of the code is met or not. Since your initial description of the problem is (and continues to be) very vague it is just about impossible for us or anyone else to produce a solution which matches precisely the unknown spec.

        In such situations therefore, we produce instead a test. The test shows what output we expect for a given input, analyses the result of the code we use to try to satisfy the test and outputs the result of that test (using TAP - which is the output you have quoted here).

        Here are some pages for you to read to undestand all this:

        It may seem daunting and/or unnecessary now but it will help you a great deal in the long run.


        🦛

Re^3: remove first and last character of string
by BillKSmith (Monsignor) on Oct 14, 2020 at 19:16 UTC
    My test proves that my solution satisfies the only test-case you have provided. Please replace my $string and $expected values with a case that fails. Post the complete test. Perhaps then, we can understand your problem. If you really want to unconditionally remove the first and last character of the string, I like the AnomalousMonk solution of substr with a length of -1. (I never would have thought of that myself.)
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11122803]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-05-21 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?