Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Input files and tabs

Posted on 2000-02-28
17
Medium Priority
?
203 Views
Last Modified: 2010-03-05
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?
0
Comment
Question by:pmery010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +4
17 Comments
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2565683
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
 

Author Comment

by:pmery010
ID: 2565727
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
 
LVL 3

Accepted Solution

by:
RobWMartin earned 800 total points
ID: 2565752
You need double quotes around the \t:

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

Rob

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:pmery010
ID: 2565777
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
 
LVL 3

Expert Comment

by:guadalupe
ID: 2565791
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
 
LVL 3

Expert Comment

by:guadalupe
ID: 2565794
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
 

Author Comment

by:pmery010
ID: 2565821
What do the double quotes do?
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2565839
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
 
LVL 84

Expert Comment

by:ozo
ID: 2565843
@LineArray=split("\s+",$TossLine);
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2565908
@LineArray=split(/\s+/,$TossLine
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2565911
Also, the double quotes are necessary if you want perl to interpret the \t sequence as a tab character.

0
 
LVL 3

Expert Comment

by:jyu_88
ID: 2565916
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
 
LVL 5

Expert Comment

by:PC_User321
ID: 2565951
Whoops, left off the last 2 chars in my earlier post:

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

0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2565989
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
 

Author Comment

by:pmery010
ID: 2566097
Thanks for all the comments.
The "\t" worked great so I will accepted the first answer.
0
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2566109
Glad to hear that your problem is solved.
0
 

Author Comment

by:pmery010
ID: 2581608
Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question