Stuart Clemons wrote:

> 
> I have a file whose format looks like this:
> 
> name1          name2          name3
> name4          name5          name6, etc.
> 
> The names are separated by spaces.   I need the names to be one name per
> line, like this:
> 
> name1
> name2
> name3, etc.

try:

[panda]# perl -i -pe 's/\s+/\n/g' names.file

this will make names.file look like:

name1
name2
name3
...

> 
> I currently use a macro with a text editor to clean up the file into the
> one name per line format.  I can do this very quickly in contrast to the
> the last two hours I've spent trying to figure out how to get Perl to do
> this very simple task.  Arrggh !
> 
> To simply things, I just tried to take the following string and print it
> out one name per line.
> 
> my $x = "name1     name2     name3";
> 
> I've tried various schemes using regex's and the ///s operator.  Most of
> the time I get syntax errors and the few times I get anything to work,
> it's not what I want.

you can use split, s/// or m//. for your purpose, they are bascially the 
same:

#!/usr/bin/perl -w
use strict;

while(<>){

        chomp;

        #--
        #-- $names[0] = name1
        #-- $names[1] = name2
        #-- ... etc
        #--
        my @names = split(/\s+/);

        #--
        #-- you can even rewrite the above like:
        #--
        #-- my @names = split;
        #--
        #-- when you become more familiar with Perl
        #--

        #--
        #-- prints the name out like:
        #--
        #-- name1
        #-- name2
        #-- name3
        #-- ...
        #--
        print join("\n",@names),"\n";
}

__END__

you can also use m// like:

my @names = /\S+/g;

which search for one or more none space characters that are stick together 
and put each of them into @names.

or you can use s/// like:

s/\s+/\n/g; print "$_\n";

which search for one or more continueous spaces and translate them into a 
newline.
 
> Anyway, any help at this point will be appreciated.  I'm hoping that in
> the long run the time I spend learning Perl will pay off, 

i think so.

david
-- 
s,.*,<<,e,y,\n,,d,y,.s,10,,s
.ss.s.s...s.s....ss.....s.ss
s.sssss.sssss...s...s..s....
...s.ss..s.sss..ss.s....ss.s
s.sssss.s.ssss..ss.s....ss.s
..s..sss.sssss.ss.sss..ssss.
..sss....s.s....ss.s....ss.s

,....{4},"|?{*=}_'y!'+0!$&;"
,ge,y,!#:$_(-*[./<[EMAIL PROTECTED],b-t,
.y...,$~=q~=?,;^_#+?{~,,$~=~
y.!-&*-/:[EMAIL PROTECTED] ().;s,;,
);,g,s,s,$~s,g,y,y,%,,g,eval

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to