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

Push Buffered string into array

Status
Not open for further replies.

Geek8

Programmer
Apr 23, 2003
31
US
How would I push a buffered string of 5 char's into an array that would break them into individual pieces? What I am trying to do is open a file, access the first of 5000 records which are each 5 bytes long and separated by a linefeed. Check to ensure values 2 and 4 are equal and 1 and 3 are not. Then output a count to an output file and then grab the next record and process the same till I get to the end of all records. This is what I have so far, but any help or ideas would be much appreciated. I have just begun using Perl, and am still getting into it and am not aware of all its components.

#!/usr/bin/perl -w
use strict;

undef $/;

open(INPUTFILE, "/u01/home/test_perl.dat");
or die "Couldn't open /u01/home/test_perl.dat for reading: $!\n";
open(OUTPUTFILE, ">//u01/home/countfile.dat");
while (read INPUTFILE, $buffer, 5) {
$string = $buffer; <-----THIS IS WHERE I AM CONFUSED
@array = split(//,$string);

foreach (@array){


Thanks in advance for you help.

Geek8
 
If your linefeed character is &quot;\n&quot; then you can read the file line by line. If it is &quot;\r&quot;, then you can set the input record separator ($/) to &quot;\r&quot; and then just use normal line-by-line reading semantics such as
Code:
    while(<INPUTFILE>) {

More details in the perlvar man page.

Either way, you will then have your five-byte atoms available in $_ at which point [tt]unpack[/tt] is your friend:

Code:
    ($a,$b,$c,$d,$e) = unpack('C5',$_);
will assign your five bytes to $a through $e. You could also just:
Code:
    my @byte = unpack('C5',$_);
and then use
Code:
$byte[0]
,
Code:
$byte[1]
, etc.

More details of unpack in the perlfunc manpage.

The completed fragment (?) would then be:
Code:
    #!/usr/bin/perl -w
    use strict;

    my $ifile = '/u01/home/test_perl.dat';
    open(INPUTFILE, $ifile) # note no semicolon here!
         or die &quot;$0: Couldn't open $ifile: $!&quot;;
    my $ofile = '/u01/home/countfile.dat';
    open(OUTPUTFILE, &quot;>$ofile&quot;)
         or die &quot;$0: Couldn't open $ofile: $!&quot;;

    while (<INPUTFILE>) {
        my @byte = unpack('C5',$_);
        die &quot;2 != 4&quot; unless $byte[1] == $byte[3];
        die &quot;1 == 3&quot; unless $byte[0] != $byte[2];
        # don't understand what you want with the countfile
    }

I hope that this helps,

Yours,


fish

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Thanks Fish. I will try it out. It makes much more sense. The countfile is for keeping a running count of the records and how many times certain records happen. Ex. How many times a certain indicator is on while another is off, then how many times both are off..etc. Is there a better way to do this thank what you have shown above? If not, thanks for your help and I will try it out anyway.
 
According to the response Fish gave me above, would this have a problem for a record that is 2733 bytes in lenght and using Unpack I try to place it into a array. For example:

my @byte = unpack('C2733',$_);

the when trying to reference $byte[3], I get the following error.

&quot;Use of uninitialized value in concatenation (.) or string&quot;

Can someone tell me why? The only change I have made to the code Fish provided is changing C5 to C2733.

Thanks

Geek
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top