Input files and tabs

I am openning a file which has all of it's elements seperated by tabs.  However when I try to do a split on \t it does not work...it seems that the tabs have been replaced by spaces.  Does anybody know anything about that and how to get around it?
pmery010Asked:
Who is Participating?
 
RobWMartinCommented:
You need double quotes around the \t:

@LineArray=split("\t",$TossLine);

Rob

0
 
RobWMartinCommented:
Some editors will automatically replace tabs with spaces.  If you don't have the original, tab-separated file, do you have the application that generated it?  If not, than we will have to consider splitting it some other way, and will need more info on the structure of the file.  Please include as much info as you can, and we'll try to solve this.

Waiting

Rob
0
 
pmery010Author Commented:
open(TossReturn,"$tos3 |");
@TossResult=<TossReturn>;
close(TossReturn);
foreach $TossLine(@TossResult)    
    {
     @LineArray=split(\t,$TossLine);

This is how I generate the information:
tos3 is a script that generates information about all the computers in a database.  Each computer has its own line and all of the information elements are seperated by tabs.  But when I do the \t split the whole line is returned.  If I do a split on ' ' I get each element.  The problem is that some of the elements have spaces in them.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
pmery010Author Commented:
open(TossReturn,"$tos3 |");
@TossResult=<TossReturn>;
close(TossReturn);
foreach $TossLine(@TossResult)    
    {
     @LineArray=split(\t,$TossLine);

This is how I generate the information:
tos3 is a script that generates information about all the computers in a database.  Each computer has its own line and all of the information elements are seperated by tabs.  But when I do the \t split the whole line is returned.  If I do a split on ' ' I get each element.  The problem is that some of the elements have spaces in them.
0
 
guadalupeCommented:
You could also try:

@LineArray=split(\s+,$TossLine);

If there are no other spaces other tahn those which were tabs...or if there are then try counting the spces that were tabs and doing this:

@LineArray=split(\s{5},$TossLine);

Meaning that the tab was converted into 5 consecutive spaces.

 

0
 
guadalupeCommented:
PS:

this will still work for tabs as well (if you use \s+  as the \s means any white space char including tabs and spaces...)
0
 
pmery010Author Commented:
What do the double quotes do?
0
 
maneshrCommented:
the double quotes indicate the extent of your delimiter. so you could have a delimiter with a space in to too!!

Eg. split("this is",$var);

where the words this is form the delimiter.
0
 
ozoCommented:
@LineArray=split("\s+",$TossLine);
0
 
PC_User321Commented:
@LineArray=split(/\s+/,$TossLine
0
 
RobWMartinCommented:
Also, the double quotes are necessary if you want perl to interpret the \t sequence as a tab character.

0
 
jyu_88Commented:
The problem relies you used \t without quoting it. Without quoting it, you are splitting against something not in your input line, thus no split and you get the whole line.

my $line, @my, $debug;
$debug = 1;
while($line=<STDIN>) {
    @my = split ("\t", $line) ;
      # BTW, the newer syntax is
      # @my = split /\t/, $line;
    $debug and print $#my," ",join "|", @my;
}
0
 
PC_User321Commented:
Whoops, left off the last 2 chars in my earlier post:

@LineArray=split(/\s+/,$TossLine);

0
 
PC_User321Commented:
Try
   @LineArray=split /\t/,$TossLine;
(slashes around \t)

Using \t is safer than \s, because the latter will get you into trouble if the fields between the tabs contain spaces.
0
 
pmery010Author Commented:
Thanks for all the comments.
The "\t" worked great so I will accepted the first answer.
0
 
RobWMartinCommented:
Glad to hear that your problem is solved.
0
 
pmery010Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.