?
Solved

Ascii "chr" function - ignore?

Posted on 2006-05-12
5
Medium Priority
?
5,953 Views
Last Modified: 2011-08-18
I've got the following script:
#!/usr/bin/perl
        my @a = ();
        while ($line = <>) {
        while (@a) {
        pop @a;
        }
        chomp $line;
        while ($line =~ m/^([\d]+)[\D]/) {
        push @a, $1;
        $line =~ s/($1).//;
        }
        push @a, $line;
        for ($x = 0; $x < $#a; $x++) { print chr($a[$x]); }
        print chr($a[$x]), "\n";
}

Which converts dot seperated ascii values like the following to plain-text
7.101.120.97.109.112.108.101 = example  (there are 7 chars in the word example)
and 7.101.120.97.109.112.108.101.10.115.101.114.118.105.99.101.49.48.49 equates to example service101 (as example is 7 chars long, and service101 is 10 chars)
The problem is that when the char_count is a "9" or a "10", in ascii those are Horizontal Tab and Line feed respectively. I'm actually very positive I won't need any ascii code below 32 (if there are any, aside from 9&10)
Actually we can eliminate the "char_count" seperator and replace it with a space in the output or as ascii before the conversion I guess.

So the output of  9.116.101.115.116.105.110.103.49.50.9.116.101.115.116.105.110.103.49.50  would convert to "testing12 testing12" rather than "testing12testing12" note the space placement.

Again I don't need any ascii value 0-31 to be translated via "chr" anything below 31 can simply be converted to a space.
9.116.101.115.116.105.110.103.49.50.9.116.101.115.116.105.110.103.49.50    (testing12 testing12)
10.116.101.115.116.105.110.103.49.50.51    (testing123)
11.100.105.100.45.105.116.45.119.111.114.10710.116.101.115.116.105.110.103.49.50.51
10.116.101.115.116.105.110.103.49.50.50.9.116.101.115.116.105.110.103.49.50
reference http://xinn.org/Css-Rosetta-Stone.html

thanks!
-rich
0
Comment
Question by:Rich Rumble
  • 3
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 16672575
while( <> ){
   my $line=pack"C*",/(\d+)/g;
   $line =~ s/[\0-\17]/ /g;
    print "$line\n";
}
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 2000 total points
ID: 16672597
while( <> ){
   my @a=/(\d+)/g;
   while( my $count = shift @a ){
       print pack"C*",splice(@a,0,$count),32;
   }
   print "\n";
}
0
 
LVL 38

Author Comment

by:Rich Rumble
ID: 16675908
As usual, perfect! This works great!

#!/usr/bin/perl
 while( <> ){
   my $line=pack"C*",/(\d+)/g;
   $line =~ s/[\0-\17]/ /g;
    print "$line\n";
}
 while( <> ){
   my @a=/(\d+)/g;
   while( my $count = shift @a ){
       print pack"C*",splice(@a,0,$count),32;
   }
   print "\n";
}

Just for prosperity...
usage, "echo 11.100.105.100.45.105.116.45.119.111.114.107.10.116.101.115.116.105.110.103.49.50.51 | ./ascii2txt.pl" ( I named the above ascii2txt.pl) or you can cat a file through it, "cat file1.txt | ./ascii2txt.pl"
-rich
0
 
LVL 85

Expert Comment

by:ozo
ID: 16676015
You only needed one of the two while loops
\17 should probably have been \37
The second treats the count as a count even if it is > 32

cat file1.txt | ./ascii2txt.pl
unnecessarily invokes cat to do the same thing as
./ascii2txt.pl < file1.txt
0
 
LVL 38

Author Comment

by:Rich Rumble
ID: 16676187
Thanks for the correction, I was wondering why there were two seperate posts, I never tried them seperately, both work very well thanks again.
-rich
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question