tags:

views:

66

answers:

2

hi all

question: how to remove spaces after the numbers if exists? from the parameter?

  • parameter can include any text

my test.pl

    #!/usr/bin/perl



  $parameter[1]='123
             456
             573
             763
             integer' ;

  print $parameter[1];




 ./test.pl > out.pl

more out.pl

           123[space]
                456[space]
                573
                763
                integer
+1  A: 

You're assigning a bunch of text to the second element in the array $array.

If you want to turn the list of numbers to array, use the following code:

@array=qw(123
         456
         573
         763
         integer) ;

This is equivalent to:

@array = (123, 456, 573, 663, 'interger');
jmz
@lidia $array is a scalar and not an array do you want an array reference? and not just a plain array?
xenoterracide
@lidia btw in no language can I think of does the syntax you're using make sense for multiple elements.
xenoterracide
You can't use an array as a scalar. You can pass around a scalar reference to an array, and you can use things such as `join` to turn an array into a scalar. What you are actually trying to achieve? Why aren't you writing idiomatic Perl? Why aren't you using `strict` and `warnings`?
David Dorward
@lidia what do you mean by the array parameter as a scalar? you do realize you can `print @array;` right? or `print join(' ', @array);`
xenoterracide
@xenoterracide: `in no language can I think of does the syntax you're using make sense` - you obviously don't know bash and tcl.
slebetman
@slebetman or I do (bash anyways) I'm pretty sure even in bash that code would only create 1 element. I admit I might be wrong, but even if I am, I would consider that bad code as it wouldn't be doing what I'd expect.
xenoterracide
@Lidia: Consider reading Learning Perl, or some such book which teaches basics of Perl. If you don't know the basics of the language, like how to turn an array into an array reference, you're not going to succeed in whatever program you are trying to create.
jmz
+1  A: 

I am not sure I understand what you want to do, but this removes spaces after numbers, but not before them:

#!/usr/bin/perl

use strict;
use warnings;

my $string =
    "123 \n"         . #these lines have spaces after the number
    "        456 \n" .
    "        573\n"  . #these lines don't
    "        763\n"  .
    "        integer\n";

$string =~ s/(\S+) +$/$1/gm;
$string =~ s/ /./g; #make the spaces visible as periods

print $string;

The first substitution works by matching one or more non-whitespace characters ((\S+)) followed by one or more spaces (+) followed by the end of the line ($) and replacing the match with $1 (which contains the match captured by the parenthesis, i.e. the non-whitespace characters). The /m modifier makes $ match the end of line rather than the end of the string and the /g makes it match as many times as it can (otherwise only the list line would be affected).

The second regex just replaces all of the spaces with periods to make it easier to see if the first regex was successful.

If you have Perl 5.10 or later, you can use the \K escape rather than the capture to tell perl you want to keep the stuff on the left hand side:

#!/usr/bin/perl

use strict;
use warnings;

my $string =
    "123 \n"         . #these lines have spaces after the number
    "        456 \n" .
    "        573\n"  . #these lines don't
    "        763\n"  .
    "        integer\n";

$string =~ s/\S+\K +$//gm;
$string =~ s/ /./g; #make the spaces visible as periods

print $string;

It is significantly faster than the capture. In this case, it is 83% faster:

#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

my $s =
    "123 \n"         . #these lines have spaces after the number
    "        456 \n" .
    "        573\n"  . #these lines don't
    "        763\n"  .
    "        integer\n";

my %subs = (
    keep => sub {
        my $t = $s;
        $t =~ s/\S+\K +$//gm;
        return $t;
    },
    capture => sub {
        my $t = $s;
        $t =~ s/(\S+) +$/$1/gm;
        return $t;
    },
);

for my $sub (keys %subs) {
    print "$sub: ", $subs{$sub}(), "\n";
}

Benchmark::cmpthese -1, \%subs;
Chas. Owens
hi Chas how the "." work in the code as I see "." mean the line cont to next?
lidia
The periods are replacing spaces.
Chas. Owens
in other words you say that period is the line continue am I right?
lidia
@lidia: `.` is the concatenation operator. Please re-read [perldoc perlop](http://perldoc.perl.org/perlop.html).
Ether