Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

strip multiple spaces

by Anonymous Monk
on Aug 01, 2000 at 04:40 UTC ( [id://25376]=perlquestion: print w/replies, xml ) Need Help??

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

I would like to split this line so that the numbers end up into variables

test1 22/11/1999     64    47  6%   263 38%    24 38%    47 73%    61 95%    63 98% 
I tried to use :
($opcode,$date,$bw,$delmb,$delpr,$recmb,$recpr) = split(/ /,$line);
$test1 = join('|',$opcode,$date,$recmb);
print "$test1\n";

and i get the output :
test1|22/11/1999|

But i would like to see the output:
test1|22/11/1999|263

Can someone give me a solution ?

Replies are listed 'Best First'.
RE: strip multiple spaces
by autark (Friar) on Aug 01, 2000 at 05:23 UTC
    You have used the regex / / (match one space) as delimiter for split. Unfortunatly the string in $line contains several consecutive spaces, so perl does what it's told, split on one space. As an example:
    $line = "foo bar"; print ++$i, ". $_\n" for split / /, $line;
    $line is assigned the string "foo..bar", where the '.' is meant to be space (it's more visible). That would output:
    1. foo
    2. 
    3. bar
    
    It prints out three lines, one which is empty (because there is nothing between the two spaces). So we have to deal with more than one whitespace. We could do this: split /\s+/, $line Now, perl will look for 1 or more spaces when trying to figure out where to split - and in your example that would probabely work out just fine. (don't use \s* as delimiter as that would match everywhere)
    But, what if your string looked something like this: $line = "  foo   bar  "; Using \s+ as delimiter now would garble things up again. It would return an extra element (which is empty) at the front. How do we fix this then ? Well, the solution may look at bit counterintuitive from what we have learned so far: split " ", $line Hm, wouldn't that just match one single space again ? Well, it should, but it doesn't. This is a special case in perl, and if we apply that to our test case we will get the following result:
    1. foo
    2. bar
    
    Ah, just what we want (in almost all cases I would dare to say). As an interesting note, consider the following:
    perl -MO=Deparse -e 'split " "' split(/\s+/, $_, 0);
    Whereas
    perl -MO=Deparse -e 'split /\s+/' split(/\s+/, $_, 0);
    generates the exact same output - but we know that the semantics is not the same. But as long as perl does The Right Thing(TM), I'm happy :-)

    Autark

Re: strip multiple spaces
by chromatic (Archbishop) on Aug 01, 2000 at 04:45 UTC
    split takes pretty robust regular expressions. Use the + quantifier to mean "at least one space": split(/\s+/,$line);
(jjhorner) strip multiple spaces
by jjhorner (Hermit) on Aug 01, 2000 at 04:49 UTC

    This works:

    #!/usr/bin/perl -w use strict; my $line = "test1 22/11/1999 64 47 6% 263 38% 24 38% 47 73% 61 95% 63 98%"; my @stuff = split('\s+',$line); my $test1 = join("|",$stuff[0],$stuff[1],$stuff[5]); print "$test1\n";
    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-23 17:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found