Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing newlines, tabs and other delimiters from an ascii file

Status
Not open for further replies.

subzero82

Programmer
Jun 21, 2001
6
CA
Please forgive me for this qestion. I am a newbie and I wrote a script that is supposed to take an exported ascii file that contains a delimited list of id numbers, then cut out all the delimiters (these can include ,.;:|, newlines and tabs) and then it takes a substr of the id number (so if the file contains 1234, 2345 the results could be 23, 34). The issue is I cannot figure out how to split tas and newlines. I ave included the code below. Any help is greatly appreciated.

print "Please choose path to your ascii file:\n";
$ascii_path = <STDIN>;
chomp $ascii_path;

print &quot;\n\nPlease choose a starting character postion:&quot;;
$start_char = <STDIN>;
chomp $start_char;

print &quot;\n\nHow long should your finished string be...:&quot;;
$string_length = <STDIN>;
chomp $string_length;

$ascii_file = $ascii_path;

open(FILE1, $ascii_file);
while (<FILE1>)
{
chomp;
@LOAD1 = split(/\n/);
};
close(FILE1);

open(FILE2, &quot;>$ascii_file new&quot;);
foreach $LOAD1 (@LOAD1)
{
chomp $LOAD1;
print FILE2 (substr(&quot;$LOAD1&quot;,($start_char-1),$string_length)).&quot;,\n&quot;;
};

print &quot;\n@LOAD1&quot;;

<STDIN>;

 
Here's the split:

my @tokens = split /[,\.;:\|\t\n]/, $text;

By putting the delimiters in square brackets, you are saying split on any one of these - I escaped the dot and pipe.

Here's a short script that shows it:

---------------------------------------
#!/usr/bin/perl -w

use strict;

my @tokens = ();

### that's a tab between o and p, and a newline after r
my $text = &quot;abc.def;ghi:jkl|mno pqr
stu&quot;;

my @tokens = split /[,\.;:\|\t\n]/, $text;

my $ct = 0;
foreach (@tokens) {
$ct++;
print &quot;token $ct = <$_>\n&quot;;
}

---------------------------------------

Read up on split by doing &quot;perldoc -f split&quot; in *nix - not sure how to do that in Windows.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
First...thanks for the response. I appreciae t and if you ever need a hand with Oracle you can buzz me anytime. The issue is that I have used your example, which works like a charm, however in this situation I cannot get all the values loaded into my array. What happens is this... The ascii file comes out like this:

345671,
125783,
092718,
152618,

When I run my code I only get the first number as output. When I substitute your code it is the same thing. I tried to read the file into one big string and then split it but that doesn't work either. I am sure that I am missing something small and stupid. Any ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top