On 1/3/07, John W. Krahn <[EMAIL PROTECTED]> wrote:
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/ Hi John,
Thanks for the suggestions regarding the code. I still need to learn to optimize the code. When I run your code it seems to work for me, so what exactly is not working? The output what i m getting is The input file contains message as follows: D0 1A 81 03 01 21 80 82 02 81 02 8D 0F 04 54 6F 6F 6C 6B 69 74 20 54 65 73 Sample output: ENTER THE SEQUENCE between[1-3]: 2 1 1 3 The output file contains: D01A8103012180820281028D0F04546F I am unable to give spaces between each entry in the output.I need the output in steps... Something like: D01A8103 0121 8082 0281028D0F04546F I tried printing a new line after the syswrite instruction, but i am unable to achieve that :-( Thanks and Regards, Dharshana