Ask a Question related to PERL Beginners, Design and Development.
-
Dan Muey #1
Is $data binary or ascii?
Say I have a variable $data that has file contents in it.
Is there a way to tell if it is binary or ascii?
if($data =~ ???) { print "It is binary weeee"; }
elsif($data =~ ???) { print "It is ascii woo hoo"; }
else { print "I do not know what it is, doh!"; }
TIA
Dan
Dan Muey Guest
-
Webservices & binary data?
Hi, We have a database that stores binary data such as word documents. Can these documents be delivered over Webservices by other applications... -
Freehand Binary Data
When printing from my Freehand 9, I often get a message "Page contains EPS pictures which include binary data" What does this mean? I also have had... -
How can I force Illustrator to always print with ASCII data?
I'm a student in an Info-Tech class, and now that we're done programming, we are working on graphic design (etc). The problem is, Illustrator... -
binary data
Greets, Register globals are to OFF - however the files will not upload. Thanking all in advance. TR ............... <? if ($submit) { -
Binary vs ASCII - why is this an issue in CS?
Don't know if this has to do with anything... I get that message in Quark 4 if ASCII is checked in the prining options box. I just check the... -
Paul Kraus #2
RE: Is $data binary or ascii?
if -T ("looks like text file")
If -B ("looks like binary file")
if -T $data ....
if -B $data ....
HTH
Paul
-----Original Message-----
From: Dan Muey [mailto:dmuey@infiniplex.com]
Sent: Tuesday, September 09, 2003 11:57 AM
To: [email]beginners@perl.org[/email]
Subject: Is $data binary or ascii?
Say I have a variable $data that has file contents in it.
Is there a way to tell if it is binary or ascii?
if($data =~ ???) { print "It is binary weeee"; }
elsif($data =~ ???) { print "It is ascii woo hoo"; }
else { print "I do not know what it is, doh!"; }
TIA
Dan
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
Paul Kraus Guest
-
Dan Muey #3
RE: Is $data binary or ascii?
Definitely! Thanks Paul.> if -T ("looks like text file")
> If -B ("looks like binary file")
>
> if -T $data ....
> if -B $data ....
>
> HTH
I never thought of using the file test operators on variables!
Cool.
Thanks!
> Paul
> -----Original Message-----
> From: Dan Muey [mailto:dmuey@infiniplex.com]
> Sent: Tuesday, September 09, 2003 11:57 AM
> To: [email]beginners@perl.org[/email]
> Subject: Is $data binary or ascii?
>
>
> Say I have a variable $data that has file contents in it.
> Is there a way to tell if it is binary or ascii?
>
> if($data =~ ???) { print "It is binary weeee"; }
> elsif($data =~ ???) { print "It is ascii woo hoo"; }
> else { print "I do not know what it is, doh!"; }
>
> TIA
>
> Dan
>
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]
>
>Dan Muey Guest
-
Jeff 'Japhy' Pinyan #4
RE: Is $data binary or ascii?
On Sep 9, Dan Muey said:
I'm not so sure you can. But the docs say that -B looks for at least 30%>>> if -T $data ....
>> if -B $data ....
>I never thought of using the file test operators on variables!
of the characters to be control-chars or high-bit chars. So I'd suggest:
sub is_binary_data {
local $_ = shift;
(tr/ -~//c / length) >= .3;
}
The tr/ -~//c takes the range of characters from space to tilde (32-126),
inverts it (so characters 0-31 and 127-255), and counts how many of them
are in $_. Then we divide that by the length of the string. If that's at
least .3, that means at least 30% of the string is binary data.
--
Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
Jeff 'Japhy' Pinyan Guest
-
Dan Muey #5
RE: Is $data binary or ascii?
> On Sep 9, Dan Muey said:
I was afraid of that. After trying it out I couldn't get any good results.>>> >> >> if -T $data ....
> >> if -B $data ....
> >I never thought of using the file test operators on variables!
> I'm not so sure you can. But the docs say that -B looks for
Excellent! Since the -B needed to be a file I was thinking about how to mimik the -B option.> at least 30% of the characters to be control-chars or
> high-bit chars. So I'd suggest:
>
>
> sub is_binary_data {
> local $_ = shift;
> (tr/ -~//c / length) >= .3;
> }
>
> The tr/ -~//c takes the range of characters from space to
> tilde (32-126), inverts it (so characters 0-31 and 127-255),
> and counts how many of them are in $_. Then we divide that
> by the length of the string. If that's at least .3, that
> means at least 30% of the string is binary data.
>
That is excellent, thanks for the explanation and info!
Thanks Jeff!
Dan
> --
> Jeff "japhy" Pinyan [email]japhy@pobox.com[/email]Dan Muey Guest
-
John W. Krahn #6
Re: Is $data binary or ascii?
Dan Muey wrote:
>
> Say I have a variable $data that has file contents in it.
> Is there a way to tell if it is binary or ascii?
>
> if($data =~ ???) { print "It is binary weeee"; }
> elsif($data =~ ???) { print "It is ascii woo hoo"; }
> else { print "I do not know what it is, doh!"; }
if ( $data =~ /[^[:ascii:]]/ ) {
print "\$data contains non-ASCII characters\n";
}
else {
print "\$data contains only ASCII characters\n";
}
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Dan Muey #7
RE: Is $data binary or ascii?
> Dan Muey wrote:
Interesting John.>> >
> > Say I have a variable $data that has file contents in it.
> > Is there a way to tell if it is binary or ascii?
> >
> > if($data =~ ???) { print "It is binary weeee"; }
> > elsif($data =~ ???) { print "It is ascii woo hoo"; }
> > else { print "I do not know what it is, doh!"; }
>
> if ( $data =~ /[^[:ascii:]]/ ) {
> print "\$data contains non-ASCII characters\n";
> }
> else {
> print "\$data contains only ASCII characters\n";
> }
Anyone know where is the [:ascii:] class/group/whatever you call it documented?
I'd like to get more info on it to compare the other method mentioned earlier in the thread,
{which works good on all my tests so far)
Thanks
Dan
>
>
>
> JohnDan Muey Guest
-
John W. Krahn #8
Re: Is $data binary or ascii?
Dan Muey wrote:
perldoc perlre>
> Anyone know where is the [:ascii:] class/group/whatever you call it documented?
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Dan Muey #9
RE: Is $data binary or ascii?
Thanks I'll have a look then!> Dan Muey wrote:>> >
> > Anyone know where is the [:ascii:] class/group/whatever you call it
> > documented?
> perldoc perlre
>
>
> John
> --
> use Perl;
> program
> fulfillment
>
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]
>
>Dan Muey Guest
-
Dan Muey #10
RE: Is $data binary or ascii?
> > Dan Muey wrote:
Looks like it's a Posix thing added in 5.8.0 which is cool and can be written as> you call it> > >
> > > Anyone know where is the [:ascii:] class/group/whatever>> >> > > documented?
> > perldoc perlre
> Thanks I'll have a look then!
[:^ascii:]
Instead of [^[:ascii:]]
Very cool, although it only works on my 5.8.0 and not 5.0
(I don't have a 5.6 ish anyone know when the Posix support was added to regexes?)
This could be usefull if I could do
if($perlversion >= 5.8.0) { do the posix [:^ascii:] version }
Else { do it the -B emulation way }
Anyone know how to check the version without putting use 5.8.0; at the top?
Dan
>>> >
> >
> > John
> > --
> > use Perl;
> > program
> > fulfillment
> >
> > --
> > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> >
> >
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]
>
>Dan Muey Guest
-
James Edward Gray II #11
Re: Is $data binary or ascii?
On Tuesday, September 9, 2003, at 01:28 PM, Dan Muey wrote:
I don't know the version it was added in, but it works in Perl 5.6.0.> Looks like it's a Posix thing added in 5.8.0 which is cool and can be
> written as
> [:^ascii:]
> Instead of [^[:ascii:]]
>
> Very cool, although it only works on my 5.8.0 and not 5.0
> (I don't have a 5.6 ish anyone know when the Posix support was added
> to regexes?)
James
James Edward Gray II Guest
-
Dan Muey #12
RE: Is $data binary or ascii?
> On Tuesday, September 9, 2003, at 01:28 PM, Dan Muey wrote:
Yeah 5.6.0 had a lot of goodies like that added. It was probably then.>> and can be> > Looks like it's a Posix thing added in 5.8.0 which is cool>> > written as
> > [:^ascii:]
> > Instead of [^[:ascii:]]
> >
> > Very cool, although it only works on my 5.8.0 and not 5.0
> > (I don't have a 5.6 ish anyone know when the Posix support was added
> > to regexes?)
> I don't know the version it was added in, but it works in Perl 5.6.0.
Anybody else then, 5.6.0 going once going twice....
>
> James
>
>Dan Muey Guest
-
John W. Krahn #13
Re: Is $data binary or ascii?
James Edward Gray II wrote:
It was added to Perl 5.6.0.>
> On Tuesday, September 9, 2003, at 01:28 PM, Dan Muey wrote:
>>> > Looks like it's a Posix thing added in 5.8.0 which is cool and can be
> > written as
> > [:^ascii:]
> > Instead of [^[:ascii:]]
> >
> > Very cool, although it only works on my 5.8.0 and not 5.0
> > (I don't have a 5.6 ish anyone know when the Posix support was added
> > to regexes?)
> I don't know the version it was added in, but it works in Perl 5.6.0.
perldoc perldelta
[snip]
POSIX character class syntax [: :] supported
For example to match alphabetic characters use
/[[:alpha:]]/. See the perlre manpage for details.
$ perl -v
This is perl, v5.6.0 built for i586-linux
John
--
use Perl;
program
fulfillment
John W. Krahn Guest



Reply With Quote

