Re: Using a Sub to pull info. from a Text File
by suaveant (Parson) on Apr 11, 2001 at 00:22 UTC
|
The most common way is to open the file and go through it
line by line, either storing the bits of data you need in
hashes and arrays, etc, or just printing as you go... i.e.
open(DATA, '/tmp/file') or die $!;
$header = 1;
while(<DATA>) {
$header = 0 if(/^$/);
if($header) {
$header .= $_;
} else {
$body .= $_;
}
}
close DATA;
Now, in doing that (example works well with email message or
raw web data, header is terminated by blank line, rest is body info)
younow have the header as one string, and the body as another,
and can pass these off to subroutines... but that is just one way to do it.
BTW: while(<DATA>) goes through the file, line by line, putting each line into $_ for you to use, if you did not know that already.
- Ant | [reply] [d/l] [select] |
|
|
| [reply] |
|
|
That was here and it was wrong. The only reason to not use a filehandle of DATA is in case you will want to upgrade your script to have an __END__ section (or your module to have a __DATA__ section).
So I agree that it is a good idea to not use DATA as a file handle, but I disagree that doing so will cause problems with the script (just potential maintenance problems).
-
tye
(but my friends call me "Tye")
| [reply] |
|
|
I've never had a problem using DATA as a filehandle.
- Ant
| [reply] |
Re: Using a Sub to pull info. from a Text File
by Mission (Hermit) on Apr 11, 2001 at 00:26 UTC
|
It really depends upon where you are beginning. 1. Do you know how to read in a text file? 2. How are your areas marked as different (delimiters) and do you know how to parse the text file? 1. example: open IN, "+<$file" || die "Can not open $file";
while (<IN>)
{
$readINtemp = $_;
$messagein .= $readINtemp;
}
close IN;
This will read in your file and assign it to $mesasgein 2. You will have to know what your text file is delimiting on to do a substr or a split on it to get your values. If you go to the Tutorial section and then click on the link marked String Matching it should help you out. I'm a 'newby' as well, but I wish you luck and hope I helped.
- Mission "Heck I don't know how to do it either, but do you think that's going to stop me?!!" | [reply] [d/l] |
|
|
The text file was pipe delimited. I have already been able to go through and parse the data. I can all of the data out and put it in a table right now. I am trying to take a section of the data in the middle of the file and put it in its own table. Does this help clarify my question?
| [reply] |
Re: Using a Sub to pull info. from a Text File
by rchiav (Deacon) on Apr 11, 2001 at 00:23 UTC
|
I think we need a little more information. Is your question about how to pass a file handle around or is your question on how to get the data out of the file?
It sounds like you want to know how to get the data from the file. If this is the case, a snippit from the file would be very helpful. Rich | [reply] |
|
|
17|Cincinnati Reds
1|5746|Pokey Reese|2B|3|1|1|1|1|0|0|.343|.408|.429
2|5930|Sean Casey|1B|3|0|1|0|1|1|0|.200|.344|.320
3|4305|Ken Griffey Jr.|CF|4|1|2|2|5|0|1|.210|.326|.448
4|5699|Dmitri Young|LF|4|0|1|0|1|0|1|.287|.339|.446
4|5540|Alex Ochoa|LF|0|0|0|0|0|0|0|.297|.357|.595
5|4615|Eddie Taubensee|C|4|0|0|0|0|0|1|.325|.387|.542
6|4159|Dante Bichette|RF|3|0|2|0|2|0|0|.219|.265|.324
6|6174|Scott Williamson|P|0|0|0|0|0|0|0|-|-|-
7|5838|Aaron Boone|3B|3|0|0|0|0|0|0|.270|.389|.438
8|5508|Juan Castro|SS|3|1|1|0|2|0|0|.333|.333|.667
9|5346|Ron Villone|P|1|0|0|0|0|0|0|.222|.222|.222
9|5299|Michael Tucker|RF|0|0|0|0|0|0|0|.167|.375|.500
TOTALS|28|3|8|3|12|1|3
LINESCORE
24|St. Louis Cardinals|2|7|0|8|0|0|0|2|0|0|0|0|0
17|Cincinnati Reds|3|8|0|5|1|0|0|0|1|1|0|0|x
DOUBLES
5046|Craig Paquette|24|St. Louis Cardinals|1|5
5508|Juan Castro|17|Cincinnati Reds|1|1
What I am trying to do is to pull the LINESCORE out and place it in a table at the top of my web page. Leaving all of the other data in its own table. Because write now, I have it set up where it just pulls everything in order. | [reply] [d/l] |
|
|
Hey, this warrants a legimate use of
supersplit! (first use of the third
dimension I have encountered)
use SuperSplit;
$a = supersplit( '|',"\n","\n\n", $data_in_one_string);
print $a->[0][1][2]; #will print 'Pokey Reese'
Jeroen
"We are not alone"(FZ) | [reply] [d/l] |
|
|
use IO::File ();
my $fh = IO::File->new($filename,'r');
### slurp it in
my $txt;
read($fh,$txt,-s $filename);
my $LS = ($txt=~s/^(LINESCORE(.+?)\n\r?\n)//s)
? $1 : '';
And now you have it. The other way is to read the file a line at a time and using sentinels funnel the information into two different variables.
| [reply] [d/l] |
|
|
...open file...
$linescore = 0;
while(<DATA>) {
if($linescore) {
last if /^\s*$/; #break the loop if you find a
## blank line after a LINESCORE
chomp; #clear off \n
push @LS, $_;
} elsif(/^LINESCORE/) {
$linescore = 1
}
}
close DATA;
that puts linescore data into @LS, line by line, for you to work with at your leisure, you could split it and do more where the push is if you wanted...
- Ant | [reply] [d/l] |
|
|
|
|
Re: Using a Sub to pull info. from a Text File
by satchboost (Scribe) on Apr 11, 2001 at 00:19 UTC
|
Basically, it sounds like you want to pass a filehandle around to various subs. Check out FileHandle. (I'm not as cool as tye cause I don't know all the node names yet.) | [reply] |