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

Reply via email to