CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Extracing tab-separated fields

 



princepawn
Novice

Dec 18, 2000, 9:52 AM

Post #1 of 3 (1606 views)
Extracing tab-separated fields Can't Post

I have six fields which are tab-separated. My regular expression succesfully matches them, but I don't know of an easy way to get out all of the fields:


Code
use strict; 

open G, 'genres.txt';

while (<G>) {
if (/(.*)([\t].*){5}/) {
warn sprintf "s: $_ l: %d", length $_;
}
}



(This post was edited by japhy on Dec 18, 2000, 11:23 AM)


Rivotti
User

Dec 18, 2000, 11:55 AM

Post #2 of 3 (1604 views)
Re: Extracing tab-separated fields [In reply to] Can't Post

Hi princepawn,

I imagine you have a file like:

FIELD1\tFIELD2\tFIELD3\tFIELD4\tFIELD5\tFIELD6

In that case:

open(INFILE,"<yourfile.txt") || die "Argh!\n";
@trash = <INFILE>;
close(INFILE);

$fields_with_tabs = join("",@trash);

# Split string in the tabs
@fields = split(/\t/,$fields_with_tabs);

# Now use the array
foreach(@field) {
....

There are better ways I sure, but this one works.

Rivotti



japhy
Enthusiast / Moderator

Dec 18, 2000, 12:26 PM

Post #3 of 3 (1603 views)
Re: Extracing tab-separated fields [In reply to] Can't Post

To get at the fields, I'd suggest using split():


Code
open FILE, $filename or die "can't open $filename: $!"; 
while (<FILE>) {
chomp;
my @fields = split /\t/;
# ...
}
close FILE;

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives