469,572 Members | 1,308 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

Parsing

Hello Friends,
i am putting some problem here. can anybody know where i am wrong in this code. actully i am spliting string in this format as a sample.s37 file.here some format below
Expand|Select|Wrap|Line Numbers
  1. Stnnaaaadddddddddddddddddddddddddddddddd ... cc
  2. S    = 'S' indicates a Motorola S record
  3.   t    = Record type, '0' = Header, '1'=data, '9'=end of file.
  4.   nn   = Count of number of bytes in record. (in ASCII/HEX)
  5.   aaaa = Load address of data record. (in ASCII/HEX)
  6.   dd   = Actual data bytes in record. (in ASCII/HEX)
  7.   cc   = Checksum of count, address, and data. (in ASCII/HEX)
  8.  
  9. input file:sample.37
  10.  
  11. S1130000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC
  12.  
Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
  3. {
  4. my ($rd_file_handle) = @_;
  5. my @file_contents;
  6. open (INPUT, "sample.s37"); #| die ("Couldn't open file $rd_file_handle: $!\n");
  7. {     
  8.  @file_contents = <INPUT>;
  9. }
  10. close(INPUT);
  11.  my $Length= @file_contents;
  12.  my $s = substr(@file_contents,($Length-1));
  13.  my $t = substr(@file_contents,($Length-2));
  14.  my $nn = substr(@file_contents,2,($Length-4));
  15.  my $aaaa = substr(@file_contents,4,($Length-8));
  16.  my $cc = substr(@file_contents,($Length),-2);
  17.  #my $file_contents = split (// , $rd_file_handle);
  18.  printf("Text:@file_contents\n$s\n$t\n$nn\n$aaaa\n$cc\n");
  19. }
  20.  
expected output:

s S
t 1
nn 13
aaaa 0000
cc FC

can anybody try it out..
bye......
Oct 4 '07 #1
8 1165
KevinADC
4,059 Expert 2GB
substr() does not work on arrays.
Oct 4 '07 #2
substr() does not work on arrays.
thank you kelvin.
can give me any alternative for this code.

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
  3. {
  4. my $file_contents;
  5. open (INPUT, "sample.s37"); 
  6. {     
  7.  $file_contents = <INPUT>;
  8. }
  9. close(INPUT);
  10.  my @index = split (//, $file_contents);
  11.  my $Length= @index;
  12.  my $s = substr($file_contents,$Length-1);
  13.  #my $t = substr(@file_contents,($Length-2));
  14.  #my $nn = substr(@file_contents,2,($Length-4));
  15.  #my $aaaa = substr(@file_contents,4,($Length-8));
  16.  #my $cc = substr(@file_contents,($Length),-2);
  17.  printf("Text:$file_contents\n@index\n$Length\nS:$s");
  18. }
  19.  
this also not working... please give me some better solution.

Regards
Lokesh...
Oct 4 '07 #3
thank you kelvin.
can give me any alternative for this code.

Code:
use warnings;
use strict;
{
my $file_contents;
open (INPUT, "sample.s37");
{
$file_contents = <INPUT>;
}
close(INPUT);
my @index = split (//, $file_contents);
my $Length= @index;
my $s = substr($file_contents,$Length-1);
#my $t = substr(@file_contents,($Length-2));
#my $nn = substr(@file_contents,2,($Length-4));
#my $aaaa = substr(@file_contents,4,($Length-8));
#my $cc = substr(@file_contents,($Length),-2);
printf("Text:$file_contents\n@index\n$Length\nS:$s ");
}
this also not working... please give me some better solution.

Regards
Lokesh...

Sorry ! kevin i wrote wrong name
Oct 4 '07 #4
KevinADC
4,059 Expert 2GB
my @index = split (//, $file_contents);
my $s = $index[0];
Oct 4 '07 #5
my @index = split (//, $file_contents);
my $s = $index[0];
thank you Kevin... i am getting proper output not getting some data...

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
  3. {
  4. #my ($rd_file_handle) = @_;
  5. my $file_contents;
  6. open (INPUT, "sample.s37"); #| die ("Couldn't open file $rd_file_handle: $!\n");
  7. {     
  8.  $file_contents = <INPUT>;
  9. }
  10. close(INPUT);
  11.  my @index = split (//, $file_contents);
  12.  my $Length= @index;
  13.  my $last = scalar($Length-3);
  14.  printf("\nType:@index[0..1]\nCount:@index[2..4]\nAddress:@index[4..8]\nCheckSum:@index[$last..$Length]\n,Text:@index");
  15.  
  16. }
  17.  
output;

Type:S 1
Count:1 3 0
Address:0 0 0 0 F
CheckSum:F C

,Text:S 1 1 3 0 0 0 0 F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F C

but i need data part also...can you help me how can i try for this..
Data: F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F
Oct 4 '07 #6
numberwhun
3,503 Expert Mod 2GB
thank you Kevin... i am getting proper output not getting some data...

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
  3. {
  4. #my ($rd_file_handle) = @_;
  5. my $file_contents;
  6. open (INPUT, "sample.s37"); #| die ("Couldn't open file $rd_file_handle: $!\n");
  7. {     
  8.  $file_contents = <INPUT>;
  9. }
  10. close(INPUT);
  11.  my @index = split (//, $file_contents);
  12.  my $Length= @index;
  13.  my $last = scalar($Length-3);
  14.  printf("\nType:@index[0..1]\nCount:@index[2..4]\nAddress:@index[4..8]\nCheckSum:@index[$last..$Length]\n,Text:@index");
  15.  
  16. }
  17.  
output;

Type:S 1
Count:1 3 0
Address:0 0 0 0 F
CheckSum:F C

,Text:S 1 1 3 0 0 0 0 F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F C

but i need data part also...can you help me how can i try for this..
Data: F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F
Ok, that is twice in the same posting that I have added code tags to your code. Please refer to the "REPLY GUIDELINES" to the right of the Message window when posting for the proper way to add code tags. They are not "Code:". Instead, the start with a "[", have the word "code" in the middle, and end with a "]".

Please use them in all future posts.

Thank you!

Regards,

Jeff
Oct 4 '07 #7
Ok, that is twice in the same posting that I have added code tags to your code. Please refer to the "REPLY GUIDELINES" to the right of the Message window when posting for the proper way to add code tags. They are not "Code:". Instead, the start with a "[", have the word "code" in the middle, and end with a "]".

Please use them in all future posts.

Thank you!

Regards,

Jeff
============================
Expand|Select|Wrap|Line Numbers
  1. ===========================
  2. use warnings;
  3. use strict;
  4. {
  5.  
  6. my @file_contents;
  7. open (INPUT, "sample.s37");
  8. {     
  9.  @file_contents = <INPUT>;
  10.  
  11. }
  12. close(INPUT);
  13. my $i;
  14. for($i=0;$i<@file_contents-1;$i++)
  15.   {
  16.  my $Type = substr ($file_contents[$i], 0,-41);
  17.  my $Count = substr ($file_contents[$i], 2,-38);
  18.  my $Address = substr ($file_contents[$i], 4,-35);
  19.  my $Data = substr ($file_contents[$i], 8,-3);
  20.  my $CSum = substr ($file_contents[$i], 40);
  21.  printf("\nType:$Type\nCount:$Count\nAddress:$Address\ndata:$Data\nCheckSum:$CSum");
  22.  }
  23. }
  24.  
  25. Output:
  26. ===========
  27. Type:S1
  28. Count:130
  29. Address:0000
  30. data:FFFFFFFFFFFFFFFFFFF1111FFFFFFFFF
  31. CheckSum:FC
  32.  
  33. Type:S1
  34. Count:130
  35. Address:0000
  36. data:FFFFFFFFFFFFFFFFFFF2222FFFFFFFFF
  37. CheckSum:FC
  38.  
  39. Type:S1
  40. Count:130
  41. Address:0000
  42. data:FFFFFFFFFFFFFFFFFFF3333FFFFFFFFF
  43. CheckSum:FC
  44.  
  45.  
==============================================
thank you jeff....

Regards
Lokesh....
Oct 5 '07 #8
numberwhun
3,503 Expert Mod 2GB
============================
Expand|Select|Wrap|Line Numbers
  1. ===========================
  2. use warnings;
  3. use strict;
  4. {
  5.  
  6. my @file_contents;
  7. open (INPUT, "sample.s37");
  8. {     
  9.  @file_contents = <INPUT>;
  10.  
  11. }
  12. close(INPUT);
  13. my $i;
  14. for($i=0;$i<@file_contents-1;$i++)
  15.   {
  16.  my $Type = substr ($file_contents[$i], 0,-41);
  17.  my $Count = substr ($file_contents[$i], 2,-38);
  18.  my $Address = substr ($file_contents[$i], 4,-35);
  19.  my $Data = substr ($file_contents[$i], 8,-3);
  20.  my $CSum = substr ($file_contents[$i], 40);
  21.  printf("\nType:$Type\nCount:$Count\nAddress:$Address\ndata:$Data\nCheckSum:$CSum");
  22.  }
  23. }
  24.  
  25. Output:
  26. ===========
  27. Type:S1
  28. Count:130
  29. Address:0000
  30. data:FFFFFFFFFFFFFFFFFFF1111FFFFFFFFF
  31. CheckSum:FC
  32.  
  33. Type:S1
  34. Count:130
  35. Address:0000
  36. data:FFFFFFFFFFFFFFFFFFF2222FFFFFFFFF
  37. CheckSum:FC
  38.  
  39. Type:S1
  40. Count:130
  41. Address:0000
  42. data:FFFFFFFFFFFFFFFFFFF3333FFFFFFFFF
  43. CheckSum:FC
  44.  
  45.  
==============================================
thank you jeff....

Regards
Lokesh....
Well, you got the starting code tag good, but as per the example in the REPLY GUIDELINES, you completely missed the ending tag that is required. The ending tag is between the same "[" and "]" but has the text "/code". If you look at your posting (do a reply to it), you will see I have added the ending code tag.

Regards,

Jeff
Oct 5 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Gerrit Holl | last post: by
16 posts views Thread by Terry | last post: by
9 posts views Thread by ankitdesai | last post: by
5 posts views Thread by randy | last post: by
13 posts views Thread by Chris Carlen | last post: by
7 posts views Thread by Daniel Fetchinson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.