in reply to remove first and last character of string

You can use a regex.
use strict; use warnings; use Test::More tests => 1; my $string = q/"654321_1111"/; my $regex = qr/^\"(.+)\"$/; my $expected = q/654321_1111/; (my $got) = $string =~ m/$regex/; is( $got, $expected, q/Remove quotes/ );

Output:

1..1 ok 1 - Remove quotes
Bill

Replies are listed 'Best First'.
Re^2: remove first and last character of string
by AnomalousMonk (Archbishop) on Oct 14, 2020 at 07:11 UTC

    It's not clear to me if flieckster intends to deal only with strings like '"foo"' (from which it is clear that 'foo' should be extracted), or if he or she may also be dealing with strings like 'foo' '"foo' 'foo"' 'f"o"o' etc., i.e., strings not having double-quotes at both the start and end of the string.

    In the latter case, it should be noted that
        qr/^\"(.+)\"$/
    will not match and will return an empty list, leaving $got undefined.


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

Re^2: remove first and last character of string
by flieckster (Scribe) on Oct 14, 2020 at 02:43 UTC
    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?

      ... 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.
      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