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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing to Individual Variables

Status
Not open for further replies.

tbohon

Programmer
Joined
Apr 20, 2000
Messages
293
Location
US
I have a Unix text file containing records in the following format:

r1f1|r1f2|r1f3!r2f1|r2f2| ... etc.

I can easily break up the individual 'lines' in this record by using split on the '!' character (actually a chr(13)). This puts each line into an array entry in @segs.

What I now need to do is pull out an individual field from an individual line and put it into a variable.

What I've coded so far (am leaving a lot of extra 'stuff' out here):

Code:
my @segs = ();
my @parts = ();

$ptname = "";

if (open(MYFILE, "xxx"))
{
   $line = <MYFILE>;
   while ($line ne "")
   {
     @segs = &breakup($line);

     foreach $seg (@segs)
     {
       @parts = &breakupsegment($seg);
       ...
      }
     ...
     $line = <MYFILE>;
}

sub breakup
{
  $CR = chr(13);

  @segs = split(/$CR/, $line);
}

sub breakupsegment
{
  @parts = split(/\|/, $seg);
}

What I need to do is assign the value of, say, parts(3) to the variable ptname - but everything I've tried fails.

Thoughts/comments appreciated.

Tnx.

Tom

"My mind is like a steel whatchamacallit ...
 
Code:
open FH, "myfile.txt" or die "$!";
while (<FH>) {
  @parts=split /\m/, $_; #IIRC \m is chr(13)
[COLOR=red]  myvar=$parts[3];[/color]

   ...

}
close FH;
The code above should do what you want, the bit in red is the assignment part you asked for

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I'll give that a shot when I get back to work Wednesday. I've been using a '$' variable, i.e., $myvar=$parts[3]; and it doesn't like that in the least.

Will let you know how it works out - and thanks!!!

"My mind is like a steel whatchamacallit ...
 
PaulTEG's code should be:

$myvar=$parts[3];
 
Which is exactly what I coded and tested and which doesn't work. Ergo, it's not this statement but something else that's causing the problem.

<sigh> I just love computers! :-)

Thanks to both of you.

Best,

Tom

"My mind is like a steel whatchamacallit ...
 
This line is strange:
Code:
@segs = &breakup($line);

You're already assigning the result of the split to the @segs array in the breakup() subroutine, so it doesn't add anything too assign it again there. Also, you're passing in $line as an argument to that subroutine, even though you never access the passed arguments in the subroutine itself, preferring to work directly on the package variable $line.

I'd write the subroutine like this:
Code:
sub breakup
{
  $CR = chr(13);

  return split(/$CR/, shift);
}

It's the same situation for your breakupsegment subroutine too.

When you say that it fails, what does happen? Do you get any errors/warnings? Do you have warnings turned on?
 
isnhid:

I don't have error checking turned on - probably should.

Understand the duplication - copied this from another routine that works (written by someone else a long time ago) and didn't understand why the syntax was as shown. I'll make the changes.

As to failing, I get nothing in the @parts array as far as I can tell - at least nothing shows up for me to check and assign to the local variable.

Let me work with it a bit more today, incorporate your suggested changes, and see what happens.

Thanks - greatly appreciate the information!

Best,

Tom

"My mind is like a steel whatchamacallit ...
 
In PaulTEG's code,

@parts should be split on the '|' character not the \m

All the record would wind up in $parts[0] otherwise.

jeb
 
Let me step back a second and regroup.

Here's the source code (there's a bunch of initialization before this but not germane here):

Code:
...

if (open(MYFILE, "tmp1ready"))
{
  $line = <MYFILE>;

  while ($line ne "")
  {
    @segs = &breakup($line);

    foreach $seg (@segs)
    {

      print "segment is\n";
      print "$seg\n";

      @parts = split(/\|/, $seg);

      $SEGTYPE = substr($seg, 0, 3);

      $ptname = $parts[6];
      [red]print "pt name is:  $ptname\n";
      print "directly from array:  $parts[6]\n"; [/red]
    }  


    $line = <MYFILE>;
  }
}

sub breakup
{

  $CR = chr(13);

  $line =~ s/^(.*)MSH/MSH/;
  @segs = split(/$CR/, $line);
}

The input file contains HL7 segments (if you don't know about HL7, it's a hospital inter-system transfer protocol) with a chr(13) (^M) between segments. Each segment has a series of fields separated by a '|' character. In other words it looks something like this:

Code:
r1f1|r1f2|r1f3^Mr2f1|r2f2|r2f3^Mr3f1 ...

The subroutine breakup successfully breaks on the ^M characters so that I have the individual segments in the @segs array. What I'm trying to do now is to look at each segment in @segs and break it up into the individual fields in @parts so I can extract specific fields. Once I've processed the entire record, I'll then put it back together using just the extracted values.

What I have above works without error - but it doesn't print anything from the two lines in [red]red[/red] that I put in to see what was being put into the @parts.

Does this clarify? Am I doing something horribly wrong here? It's pretty simple Perl code and, as I said earlier, the core is taken from another analyst's routine which works just fine.

Thanks again.

Tom

"My mind is like a steel whatchamacallit ...
 
I fixed the issue - the file I was using as input was corrupted somehow - regenerating it and running the routine above as written solved the problem.

Sorry to have wasted everyone's time on this non-issue ... I should have thought to check the input file one last time before putting out the mayday call.

Now if I just knew how it had become corrupted ... :-(

Appreciate all the input - take care and thanks!

Best,

Tom

"My mind is like a steel whatchamacallit ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top