Dharshana Eswaran am Dienstag, 2. Januar 2007 08:02: > Hi All, Hi Dharshana Eswaran
> I have a piece of code which reads as shown below: > > unless (open(IN, "in.txt")) { > die("Cannot open input file \n"); > } > binmode(IN); > > unless (open(OUT, "+>output.txt")) { > die("Cannot open input file input.txt\n"); > } > > %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); > @seq = split(/ +/, $seq); > $seq_len = @seq; > > $input = <IN>; > @input = split(/ +/, $input); > $new = join ("", @input); > > for($i=0; $i<$seq_len; $i++) { > $read1[$i] = $table{$structure{$seq[$j]}}; > syswrite (OUT, $new, $read1[$i]); > print OUT ("\n"); > $j++; > } > > In the above code, i m trying to read the input in bytes 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? My tip is that you start your code (always) with the two lines use strict; use warnings; and define all your variables. You will get warnings like this for the line above the syswrite line: Use of uninitialized value in hash element at XXX line YYY. Then you can inspect the values in $seq[$j] (with a warn statement), and you'll see what's going wrong :-) There are also missing checks for open, close, and syswrite; the second die statement is misleading; most double quotes can be replaced by single quotes because nothing is interpolated into the strings; some constructs can be simplified and expressed in a shorter way; some statements are "old style" perl. How about modifying the code and reposting it when it does what you want? Happy new year Dani -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/