Dharshana Eswaran wrote:
> Hi All,
Hello,
> I have a piece of code which reads as shown below:
>
> unless (open(IN, "in.txt")) {
> die("Cannot open input file \n");
You should include the $! variable in the error message so you know *why* it
failed.
> }
> binmode(IN);
You appear to be reading from a text file so why binmode?
> unless (open(OUT, "+>output.txt")) {
> die("Cannot open input file input.txt\n");
You should include the $! variable in the error message so you know *why* it
failed.
> }
>
> %structure = ( 1 => "A", 2 => "B", 3 => "C",
> );
> @keys = keys %structure;
> %table = ( A => 4, B => 8, C => 32,
> );
> $j=0;
>
> print("ENTER THE SEQUENCE between[1-3]:\n");
> $seq = <STDIN>;
> chop($seq);
You should use chomp() instead of chop().
> @seq = split(/ +/, $seq);
> $seq_len = @seq;
>
> $input = <IN>;
> @input = split(/ +/, $input);
> $new = join ("", @input);
Why not just modify the string instead of splitting and joining:
( my $new = <IN> ) =~ s/ +//g;
> for($i=0; $i<$seq_len; $i++) {
> $read1[$i] = $table{$structure{$seq[$j]}};
> syswrite (OUT, $new, $read1[$i]);
> print OUT ("\n");
perldoc -f syswrite
syswrite FILEHANDLE,SCALAR,LENGTH,OFFSET
syswrite FILEHANDLE,SCALAR,LENGTH
syswrite FILEHANDLE,SCALAR
Attempts to write LENGTH bytes of data from variable SCALAR to the
specified FILEHANDLE, using the system call write(2). If LENGTH
is not specified, writes whole SCALAR. It bypasses buffered IO,
^^^^^^^^^^^^^^^^^^^^^^^^
so mixing this with reads (other than sysread()), "print",
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"write", "seek", "tell", or "eof" may cause confusion because the
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
perlio and stdio layers usually buffers data. Returns the number
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
of bytes actually written, or "undef" if there was an error (in
this case the errno variable $! is also set). If the LENGTH is
greater than the available data in the SCALAR after the OFFSET,
only as much data as is available will be written.
You are using syswrite() *and* print() on the same filehandle which you
shouldn't do.
> $j++;
> }
>
> In the above code, i m trying to read the input in bytes
It *looks* like you are reading lines of text?
> and display
> them in another output file. The reading is done in different sizes
> (4 or 8 or 32bytes), as per the sequence specifed by the User. The
> input file with filehandle IN contains data as shown below:
>
> F1 2F 8A 02 05 09 00 00 00 04 2B 48 00 00 00 68
>
>
> When i use syswrite function, i face the following problem
>
> syswrite (OUT, $new, 4); => Writes 4 bytes properly
> syswrite (OUT, $new, $val); (where $val =4;) => Writes 4 bytes properly
> syswrite (OUT, $new, $read1[$i]); => This does not work. It displays all
> the
> bytes together, without seperators(new line). I dont know the reason.
>
> Can anyone please guide me in this?
When I run your code it seems to work for me, so what exactly is not working?
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/