Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
reading input from a file - changing new line character
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
hollyhawkins  
View profile  
 More options Dec 18 2006, 11:21 am
Newsgroups: comp.lang.perl.misc
From: "hollyhawkins" <holly.hawk...@ynhh.org>
Date: 18 Dec 2006 08:21:48 -0800
Local: Mon, Dec 18 2006 11:21 am
Subject: reading input from a file - changing new line character
I have an hl7 input file where the each segment used to be separated by
a hex "0D", now each segment is separated with a hex "0A". The script
used to read in the whole file as one record instead of parsing it out
segment by segment, which is now happening with new x"0A" at the end of
each segment. How can I change that and have the script read the file
in as one record?  Any help would be greatly appreciated. Thank you.

Here is the code:
Sample input - this editor a

#!/usr/local/bin/perl

#This script will read a directory containing individual records, write
their
#data contents to a single output file for input to elink. This was
developed
#for the transactions from QUEST to Logician. Sept 2005 Holly Hawkins

#define the file directory paths

$datainpath = "C:\\YNHH_Files\\Quest\\Quest_IN";
$dataoutpath = "C:\\YNHH_Files\\Quest\\Quest_OUT";
$tempdir  = "C:\\YNHH_Files\\Quest\\temp";
$archivedir = "C:\\YNHH_Files\\Quest\\archive";

#open the directory that has the input files

opendir THISDIR, "$datainpath" or die "Serious Error: $!";

#read the names of the individual files into an array "@allfiles"

@allfiles = grep !/^\.\.?$/, readdir THISDIR;

closedir THISDIR;
# the follwing line of code was entered by REV to prevent this script
# from running if the dummyrec is the only file existent in the
directory.
if (@allfiles <= 1) {exit}

#print "size of array: " . @allfiles . ".\n";

#writes the names from the directory to a file in tempdir

open RECORDNAMES, ">$tempdir\\recnames.txt"  or die "Serious Error:
$!";

foreach $allfiles (@allfiles) {
#               if ($allfiles != '99999dummyrec.txt')
                {print RECORDNAMES "$allfiles\n"};
#        print "$allfiles\n";
                }

#fileout has the names of the records to be deleted
close "$tempdir\\recnames.txt";

# now take the record names in RECORDNAMES,
#and write the contents of the each of the records to another file.

#open dataout.txt for the data from the records
#the >> will open the file if it does not exist, or append to it
#if it is there already

#reopen filenames file as input

open (RECORDNAMES, "$tempdir\\recnames.txt")  or die "Serious Error:
$!";
open (OUTFILE, ">>$dataoutpath\\questout.txt") or die "cannot open
questout.txt.\n";

#write to the dataout.txt file

select (OUTFILE);

# Read the file of record names
# open each file
#write contents of file to the dataout.txt OUTFILE

#=>>>>This is the problem  - each segment is now read as a separate
record, and written out #=>>>separtley - I want to read the file in one
'chunk".

while (<RECORDNAMES>)  {
 $filename = "$datainpath\\$_";
  open (X, "$filename");
  while (<X>)

   #  {if ($filename != "$datainpath\\99999dummyrec.txt")
      {print  "\x0B$_\x1C\x0D"};

        #  }
                                           }
# just wrote all the contents of the files, close the output file
dded an extra "0D" - strip that out if you want to replicate my
problem.
MSH|^~\&|LAB|QWA||226964|2006REC 1
10449||ORU^R01|20061208578891130000|P|2.3|||||||
PID|1|19230|VD441550||TEST^ALMA^G||19500506|F||||||||||2269640000212|047441 174||||||||||||
NTE|1|TX|NON-FASTING  |


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Lalli  
View profile  
(1 user)  More options Dec 18 2006, 11:24 am
Newsgroups: comp.lang.perl.misc
From: "Paul Lalli" <mri...@gmail.com>
Date: 18 Dec 2006 08:24:36 -0800
Local: Mon, Dec 18 2006 11:24 am
Subject: Re: reading input from a file - changing new line character

hollyhawkins wrote:
> I have an hl7 input file where the each segment used to be separated by
> a hex "0D", now each segment is separated with a hex "0A". The script
> used to read in the whole file as one record instead of parsing it out
> segment by segment, which is now happening with new x"0A" at the end of
> each segment. How can I change that and have the script read the file
> in as one record?  Any help would be greatly appreciated. Thank you.

Please browse the Perl FAQ *before* posting.

perldoc -q entire
Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq5.pod
     How can I read in an entire file all at once?

Paul Lalli


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tad McClellan  
View profile  
(1 user)  More options Dec 18 2006, 9:39 pm
Newsgroups: comp.lang.perl.misc
From: Tad McClellan <ta...@augustmail.com>
Date: Mon, 18 Dec 2006 20:39:25 -0600
Local: Mon, Dec 18 2006 9:39 pm
Subject: Re: reading input from a file - changing new line character

hollyhawkins <holly.hawk...@ynhh.org> wrote:
> I have an hl7 input file where the each segment used to be separated by
> a hex "0D",

Most people call that a "carriage return".

> now each segment is separated with a hex "0A".

Most people call that a "linefeed".

> and have the script read the file
> in as one record?

See the $/ variable in:

   perldoc perlvar

> $datainpath = "C:\\YNHH_Files\\Quest\\Quest_IN";

Variablenamessurearehardtoreadwhenyouwritethemlikethat.

You should use single quotes unless you _want_ one of the two extra
things that double quotes give you.

You can use sensibly-leaning slashes in filenames that are not
going to be fed to the M$ "shell".

   $data_in_path = 'C:/YNHH_Files/Quest/Quest_IN';

> opendir THISDIR, "$datainpath" or die "Serious Error: $!";

Checking the return value. Good.

Quoting a lone variable. Bad.

See:

   perldoc -q vars

       What’s wrong with always quoting "$vars"?

> @allfiles = grep !/^\.\.?$/, readdir THISDIR;

I would suggest that these are easier to read and understand:

   @allfiles = grep $_ ne '.' and $_ ne '..', readdir THISDIR;
or
   @allfiles = grep /^[.]{1,2}$/, readdir THISDIR;

> open RECORDNAMES, ">$tempdir\\recnames.txt"  or die "Serious Error:
> $!";

Checking the return value again. Good.

> close "$tempdir\\recnames.txt";

That statement was working OK for you?

> open (RECORDNAMES, "$tempdir\\recnames.txt")  or die "Serious Error:
> $!";

Checking the return value. Good.

> open (OUTFILE, ">>$dataoutpath\\questout.txt") or die "cannot open
> questout.txt.\n";

Checking the return value. Good.

>   open (X, "$filename");

Not checking the return value. Bad.

You should choose a more meaningful filehandle name too.

>    #  {if ($filename != "$datainpath\\99999dummyrec.txt")

Perl has different operators for comparing numbers or for comparing strings.

--
    Tad McClellan                          SGML consulting
    ta...@augustmail.com                   Perl programming
    Fort Worth, Texas


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google