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

Greetings Perl Monks I have a problem which I'm struggling with due to my limited perl knowledge, which I hope you can assist me with.

I have a string which Dumper displays like this

$VAR1 = [ " echo ", " ----------", " REPVersion", "", " Version + + ", " ---------------------------------------------------------- +--------------------------------------------------------------------- +---------------", " Replication Server/16.0/EBF 27617 SP03 PL03 rs160sp03pl03/ +Linux AMD64/Linux 2.6.32.59-0.19-default x86_64/2214/OPT64/Tue Dec 19 + 19:00:15 2017 ", " echo ", " -----------", " REPErrorlog", "", " Log File Name ", " --------------------------------------------------------", " /logdisk/logs/my.log", " echo ", " --------------", " REPSiteVersion", "", " Site Version", " ------------", " 1600302", " echo ", " -----------", " REPRSSDName", "", " RSSD Dataserver RSSD Database ", " ------------------------- ------------------", " SYBASE_APP123_MYRSSD_SERV SYBASE_APP123_RSSD", " ------------", " REPQueueSize", "", " ", " -----------", " 102396", " ", " ---------------", " REPLossDetected", "", " ", " -----------", " 0", " ", " -------------", " REPSSLEnabled", "", " ", " -----------", " 0" ]; ARRAY(0x20794b0)

I'm trying to remove the leading whitespace using this code

my $result = runDBsql($server,$user,$pass); # Cannot modify runDBsql remove_leadspace($result); local $Data::Dumper::Useqq = 1;# In case it is not whitespace but some + other character print Dumper($result); print "\n$result\n"; # added for further debugging sub remove_leadspace { return if not defined wantarray; return map { s/^\s+//r } @_ if wantarray; return shift =~ s/^\s+//r; }

The remove_leadspace is not removing the leftmost spaces

So when I consequently use the returned data which I reference using $result->[n] it looks like this

(' REPErrorlog', ' /logdisk/logs/my.log') (' REPSiteVersion', ' 1600302') (' REPRSSDName', ' SYBASE_APP123_MYRSSD_SERV SYBASE_APP123_RSSD') (' REPQueueSize', ' 102396') (' REPLossDetected', ' 0') (' REPSSLEnabled', ' 0')

I inherited this code and I think I'm confusing scalar and array strings.

How do I get rid of the leading whitespace ?

Any help much appreciated

Replies are listed 'Best First'.
Re: Problems removing leading whitespace
by haukex (Archbishop) on Jan 30, 2020 at 11:54 UTC
    print Dumper($result); $VAR1 = [ " echo ", " ----------", ...

    $result is a reference to an array of strings. You can access the actual array via @$result (perlreftut, perlref). You can simply use this to remove the leading whitespace from all the elements of $result: s/^\s+// for @$result;

    Update: Also, your sub remove_leadspace is designed to return a modified list of strings, but you're not doing anything with its return value. You could do: @$result = remove_leadspace(@$result); or my @trimmed = remove_leadspace(@$result);

      Expanding on this:

      In the original implementation, you call remove_leadspace() without assigning its result to anything. This means that wantarray is undef, so the original implementation of remove_leadspace() returns without doing anything.

      Because in Perl there is More Than One Way To Do It, another implementation would be something like:

      sub remove_leadspace( s/^\s+// for @{ $_[0] }; return; }

      This is called by remove_leadspace( $result ). That is, it expects a single argument which is an array reference, modifies the elements of the array (note that the /r qualifier qas removed from the s///), and returns nothing.

      Fantastic, many thanks for the prompt reply