Re: Remove Newlines from String

2016-09-09 Thread Chas. Owens
On Tue, Sep 6, 2016 at 3:24 PM Shawn H Corey  wrote:


> > #Change the value to the maximum you want
> > my %HEXCODES = map{$_ => sprintf("%03X", $_)} (0..128);
>
> my %HexCodes = map { ord($_) => sprintf '%02X', $_ } ( 0 .. 128 );
>

Just your friendly reminder that Unicode exists and unless you are opening
the file with the ":raw" IO layer, you can easily get unprintable
characters above 0x7F:

perl -e 'for (0 .. 0x10) { printf("U+%04x is not printable\n", $_)
unless chr =~ /[[:print:]]/  }'

Even limiting ourselves to values that fit in one byte, the first 32 values
above 127 (U+0080 - U+009F) are all control characters (the C1 set).


Re: Remove Newlines from String

2016-09-06 Thread Shawn H Corey
On Tue, 6 Sep 2016 22:08:40 +0200
David Emanuel da Costa Santiago  wrote:

> 
> 
> Thanks :-)
> 
> A little bug:
> 
> > my %HexCodes = map { ord($_) => sprintf '%02X', $_ } ( 0 .. 128 );
> my %HexCodes = map { chr($_) => sprintf '%02X', $_ } ( 0 .. 128 );
> 
> 
> 
> 

Oops. :)


-- 
Don't stop where the ink does.

Shawn H Corey
mailto:shawnhco...@nili.ca

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove Newlines from String

2016-09-06 Thread Shawn H Corey
On Tue, 6 Sep 2016 17:01:52 -0400
Uri Guttman  wrote:

> On 09/06/2016 04:42 PM, X Dungeness wrote:
> > It's kinda hard to see but I included the /x switch because
> > I inserted blanks on the pattern as well as the replacement
> > side. Without /x, the match will fail.
> >
> > $str =~ s{  ([^[:print:]])  }{ sprintf( "(%#2X)", ord $1) }gex;
> >   ^  ^  
> you are correct. /x affects whitespace in the pattern. i was stuck 
> thinking about the replacement part. in fact now that i am thinking 
> about it more clearly, /x doesn't affect the replacement at all.

True but /e does. ;)


-- 
Don't stop where the ink does.

Shawn H Corey
mailto:shawnhco...@nili.ca

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove Newlines from String

2016-09-06 Thread Uri Guttman

On 09/06/2016 04:42 PM, X Dungeness wrote:

It's kinda hard to see but I included the /x switch because
I inserted blanks on the pattern as well as the replacement
side. Without /x, the match will fail.

$str =~ s{  ([^[:print:]])  }{ sprintf( "(%#2X)", ord $1) }gex;
  ^  ^
you are correct. /x affects whitespace in the pattern. i was stuck 
thinking about the replacement part. in fact now that i am thinking 
about it more clearly, /x doesn't affect the replacement at all.


thanx,

uri



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove Newlines from String

2016-09-06 Thread X Dungeness
It's kinda hard to see but I included the /x switch because
I inserted blanks on the pattern as well as the replacement
side. Without /x, the match will fail.

$str =~ s{  ([^[:print:]])  }{ sprintf( "(%#2X)", ord $1) }gex;
 ^  ^



On Tue, Sep 6, 2016 at 1:06 PM, Uri Guttman  wrote:
> On 09/06/2016 03:59 PM, X Dungeness wrote:
>>
>> $str =   "ab\rcd\nef\ngh\fij";
>>
>> $str =~ s{  ([^[:print:]])  }{ sprintf( "(%#2X)", ord $1) }gex;
>>
>> >   ab(0XD)cd(0XA)ef(0XA)gh(0XC)ij
>>
>>
> that is a nice use of /e (don't think you need /x when you already have /e
> as code can handle blanks. but the # comment feature is enabled with /x.).
> but the other answers had a nice optmization. they built the hash of numbers
> to hex strings so their replacement only did a hash lookup and they don't
> even need /e to work. that is a basic caching optimization that newbies can
> learn to use. this is a good case to see the coding differences. as an
> exercise, some of you could even write up a benchmark comparing them. post
> your results to this thread.
>
> thanx,
>
> uri
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove Newlines from String

2016-09-06 Thread David Emanuel da Costa Santiago


Thanks :-)

A little bug:

> my %HexCodes = map { ord($_) => sprintf '%02X', $_ } ( 0 .. 128 );
my %HexCodes = map { chr($_) => sprintf '%02X', $_ } ( 0 .. 128 );




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove Newlines from String

2016-09-06 Thread Uri Guttman

On 09/06/2016 03:59 PM, X Dungeness wrote:

$str =   "ab\rcd\nef\ngh\fij";

$str =~ s{  ([^[:print:]])  }{ sprintf( "(%#2X)", ord $1) }gex;

>   ab(0XD)cd(0XA)ef(0XA)gh(0XC)ij


that is a nice use of /e (don't think you need /x when you already have 
/e as code can handle blanks. but the # comment feature is enabled with 
/x.). but the other answers had a nice optmization. they built the hash 
of numbers to hex strings so their replacement only did a hash lookup 
and they don't even need /e to work. that is a basic caching 
optimization that newbies can learn to use. this is a good case to see 
the coding differences. as an exercise, some of you could even write up 
a benchmark comparing them. post your results to this thread.


thanx,

uri


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove Newlines from String

2016-09-06 Thread X Dungeness
$str =   "ab\rcd\nef\ngh\fij";

$str =~ s{  ([^[:print:]])  }{ sprintf( "(%#2X)", ord $1) }gex;

>   ab(0XD)cd(0XA)ef(0XA)gh(0XC)ij


On Tue, Sep 6, 2016 at 9:11 AM, Matt  wrote:
> I am receiving log entries as a string and then writing them to a file
> with the date tacked on beginning.  Problem is that sometimes the
> string I receive contains \n and it makes parsing the file with grep
> more difficult.  Looking for a simple way to replace all \n in the
> string with text  or something of that sort.
>
> It might be even better yet to replace all characters that are not
> directly printable with there HEX equivalent since I only need
> readable text log info on one line.  Is there an easy way to do that?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove Newlines from String

2016-09-06 Thread Shawn H Corey
A little refinement.

On Tue, 6 Sep 2016 21:04:04 +0200
David Emanuel da Costa Santiago  wrote:

> If you want to replace all non printable characters you can do
> something like:
> 
> ### START
> 
> #Change the value to the maximum you want
> my %HEXCODES = map{$_ => sprintf("%03X", $_)} (0..128); 

my %HexCodes = map { ord($_) => sprintf '%02X', $_ } ( 0 .. 128 );

> 
> my $s="This is my string! \r\n the end";
> 
> say "String before: $s";
> 
> #Change the character class you want
> $s =~ s/([^[:print:]])/$HEXCODES{ord($1)}/g;

$s =~ s/([^[:print:]])/$HexCodes{$1}/g;

> 
> say "String after: $s";
> 
> END

__END__


-- 
Don't stop where the ink does.

Shawn H Corey
mailto:shawnhco...@nili.ca

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove Newlines from String

2016-09-06 Thread David Emanuel da Costa Santiago

Hi!

You can use "chomp" to remove the $/ from the end of the line.


If you want to replace all non printable characters you can do
something like:

### START

#Change the value to the maximum you want
my %HEXCODES = map{$_ => sprintf("%03X", $_)} (0..128); 

my $s="This is my string! \r\n the end";

say "String before: $s";

#Change the character class you want
$s =~ s/([^[:print:]])/$HEXCODES{ord($1)}/g;

say "String after: $s";

END


Regards,
David Santiago


On Tue, 6 Sep 2016 11:11:35 -0500
Matt  wrote:

> I am receiving log entries as a string and then writing them to a file
> with the date tacked on beginning.  Problem is that sometimes the
> string I receive contains \n and it makes parsing the file with grep
> more difficult.  Looking for a simple way to replace all \n in the
> string with text  or something of that sort.
> 
> It might be even better yet to replace all characters that are not
> directly printable with there HEX equivalent since I only need
> readable text log info on one line.  Is there an easy way to do that?
> 


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove Newlines from String

2016-09-06 Thread Kenneth Wolcott
On Tue, Sep 6, 2016 at 9:11 AM, Matt  wrote:
> I am receiving log entries as a string and then writing them to a file
> with the date tacked on beginning.  Problem is that sometimes the
> string I receive contains \n and it makes parsing the file with grep
> more difficult.  Looking for a simple way to replace all \n in the
> string with text  or something of that sort.
>
> It might be even better yet to replace all characters that are not
> directly printable with there HEX equivalent since I only need
> readable text log info on one line.  Is there an easy way to do that?

Perhaps you might want to look at "chomp"?
http://perldoc.perl.org/functions/chomp.html

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/