[comp.lang.perl] structs?

cwilson@NISC.SRI.COM (Chan Wilson) (07/01/90)

I've got a data file that is of the format struct; that is, data is written out
with a C definition of thus:

	struct load {
	        time_t  time;
	        ushort  load;
	};

(note that this is on a sun, and <time.h> is included, giving the struct for
time)

Now, reading the data from C is trivial; problem is, what I do with the
data once I've read it is far more appropriate to perl than C.  However,
I don't see anything in the man page in regards to structs, other than a brief
mention in the area of pack/unpack.  

Is it possible to read this file from perl, or would it be easier all around to
use a simple C program to read the data, then pipe it into perl?

Thanks..

--Chan
		 -- For the snark >was< a boojum, you see... --
     Chan Wilson 		SRI International, Network Information Center
 Inet:cwilson@nisc.sri.com   AOL:cwilson  GEnie:cwilson6   verbal:415/856-6612
     -- I ain't the prez of this company, don't take my word as gospel. --

merlyn@iwarp.intel.com (Randal Schwartz) (07/02/90)

In article <18249@fs2.NISC.SRI.COM>, cwilson@NISC (Chan Wilson) writes:
| I've got a data file that is of the format struct; that is, data is written out
| with a C definition of thus:
| 
| 	struct load {
| 	        time_t  time;
| 	        ushort  load;
| 	};
[...]
| Is it possible to read this file from perl, or would it be easier all around to
| use a simple C program to read the data, then pipe it into perl?

Naah.  Do it all in Perl...

open(F,"/where/its/at") || die "cannot open 'at': $!";
$buflen = length(pack("lS",0,0)); # how long is struct?
while (read(F,$buf,$buflen) == $buflen) {
	($time,$load) = unpack("lS",$buf);
	$load{$time} = $time; # maybe? or
	# push(@all,"$time $load"); # if you want a whole list
}
close(F);

print grep($_=pack("c",hex($_)),unpack("A2"x 25,"4a75737420616e6f74686572205065726c206861636b65722c"))
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/