Solved

Input files and tabs

Posted on 2000-02-28
17
161 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 200 total points
ID: 2565752
You need double quotes around the \t:

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

Rob

0
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now