you just use a different regular expression with split.
In the original example the split was done at the whitespaces
the regex is /\s/. Now you want to split at whitespace colon 
whitespace. The regex is /\s:\s/. If you want to allow more
than one whitespace you could /\s*:\s*/ like this. You probably
should do that, because that would work for:

key:value
key : value
key:   value
..

to be concrete:

Just change the line

@words = split;

to

@words = split/\s*:\s*/;


btw \s in the regex matches all sorts of whitespaces (real space,
tab, newline, etc.)

cr

On Fri, 20 Apr 2001 17:00:37 -0500, Sean C. Phillips said:

> Gurus,
>  
>  This was very timely and helpful.  I've got another related question,
>  hence the reply to this.  What if I've got a stanza like:
>  
>  parm : parm1
>  value        : 100
>  
>  parm : parm2
>  value        : 101 
>  
>  .... etc, and I want to keep the name of the parm and the value, and
>  trash the rest?
>  
>  Thanks,
>  Grateful Newbie
>  
>  Steve Lane wrote:
>  > 
>  > Peter Lemus wrote:
>  > > I need to read a huge two column file; I only need the
>  > > data from the second column; I need to asign it a
>  > > variable to that column, for example $FILE, how do go
>  > > about doing this; I appreciate your help.
>  > >
>  > > the file looks like this
>  > >
>  > > md tony
>  > > md ariba
>  > > md arpa
>  > 
>  > in the spirit of TMTOWTDI...
>  > 
>  > here's a "verbose" solution:
>  > 
>  >   #!/usr/bin/perl -w
>  >   # two-column <filename>
>  > 
>  >   use strict;
>  > 
>  >   while (<>) {
>  >     # split line on whitespace
>  >     my @words = split;    # same as: my @words = split / /;  (with
>  > leading nulls discarded)
>  >                           # same as: my @words = split / /, $_;
>  >                           # same as: my @words = split /\s+/, $_;
>  > 
>  >     # assign second word to $FILE
>  >     my $FILE = $words[1];
>  > 
>  >     # do whatever with $FILE
>  >     print "i got '$FILE'\n";
>  >   }
>  > 
>  > usage: two-column file.txt
>  > 
>  > the -n option will (basically) wrap a "while (<>) { }"
>  > around the body of the script, to save some space:
>  > 
>  >   #!/usr/bin/perl -wn
>  >   use strict;
>  > 
>  >   my @words = split;
>  >   my $FILE = $words[1];
>  >   print "i got '$FILE'\n";
>  > 
>  > you can remove @words to get:
>  > 
>  >   #!/usr/bin/perl -wn
>  >   use strict;
>  > 
>  >   my $FILE = (split)[1];
>  >   print "i got '$FILE'\n";
>  > 
>  > you can autosplit (to the @F array) on whitespace with -a:
>  > 
>  >   #!/usr/bin/perl -wan
>  >   use strict;
>  > 
>  >   my $FILE = $F[1];
>  >   print "i got '$FILE'\n";
>  > 
>  > and here's a one-liner you can run at the command line:
>  > 
>  >   $ perl -lane 'my $FILE = $F[1]; print "i got $FILE"' file.txt
>  > 
>  > > I will have a loop going over and reassingn the next
>  > > name to the same variable.
>  > 
>  > i'm having trouble parsing this.
>  > --
>  > Steve Lane <[EMAIL PROTECTED]>
>  
>  -- 
>  Thanks,
>  Sean C. Phillips
>  UNIX Strategic Services
>  mailto:[EMAIL PROTECTED]
>  phone: 469-357-4882
>  
>  

Reply via email to