> 
> Help. I'm a frustrated newbie who wants to use Perl to make my life
easier.
> 
> The following simple task is only one small part of a program I'm
trying to
> put together to automate some things I currently do manually.
> 
> 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.
> 
> 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.
> 
> I did get this array structure to work:
> 
> my @names = qw(name1     name2     name3);
> print "$names[0] \n";
> print "$names[1] \n";
> print "$names[2] \n";
> 
> So I then spent time unsuccesfully trying to figure out how to get my
> string split into the array. I couldn't get that to work either. More
> Arrggh !
> 
> 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, which it will if I
> can automate some of the tasks I do manually (with the help of macros in a
> text editor).
> 
> My next Perl task after I get my list of one name per line, is to sort the
> list and eliminate duplicate names.
> 
> Thanks again for any help.

You are looking initially for the 'split' function. 

perldoc -f split

So for instance your one line above:

my $line = "name1   name2    name3";

Can be broken into parts,

my @parts = split(/\s+/, $line);

The above says to split on one or more whitespace characters and store
the result as a list in an array.  Then you want to have a go at,
perldoc -f push

You can then loop over a series of lines storing each of the smaller
arrays to a bigger array.  That will give you your complete list. For
sorting and assuring uniqueness you will want to check out:

perldoc -f sort
perldoc -f grep
perldoc -f map

Good luck, don't get frustrated (any more), and I guarantee Perl will
open doors that a text editor macro can only dream of (unless we are
talking emacs or something, but then we are really talking lisp)...

http://danconia.org


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