Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Input files and tabs

Posted on 2000-02-28
17
Medium Priority
?
208 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 85

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

824 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