Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: remove first and last character of string (updated)

by AnomalousMonk (Bishop)
on Oct 14, 2020 at 03:27 UTC ( #11122808=note: print w/replies, xml ) Need Help??


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

... 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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: remove first and last character of string (updated)
by hippo (Bishop) on Oct 14, 2020 at 09:09 UTC
    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; }

    🦛

Re^4: remove first and last character of string (updated)
by flieckster (Scribe) on Oct 15, 2020 at 00:47 UTC
    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.


      🦛

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2022-06-24 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (80 votes). Check out past polls.

    Notices?