[32378] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3645 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Mar 20 21:09:29 2012

Date: Tue, 20 Mar 2012 18:09:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 20 Mar 2012     Volume: 11 Number: 3645

Today's topics:
    Re: getting hash reference from a package (Greg Bacon)
    Re: Increment with '#' and decrement with 'b' <ben@morrow.me.uk>
        Problem with splitting data <dave@invalid.invalid>
    Re: Problem with splitting data <ben@morrow.me.uk>
    Re: Problem with splitting data <dave@invalid.invalid>
    Re: Problem with splitting data <rweikusat@mssgmbh.com>
    Re: Problem with splitting data <rweikusat@mssgmbh.com>
    Re: Problem with splitting data <dave@invalid.invalid>
    Re: Problem with splitting data <rweikusat@mssgmbh.com>
    Re: Problem with splitting data <dave@invalid.invalid>
    Re: Problem with splitting data <cartercc@gmail.com>
    Re: Problem with splitting data <ben@morrow.me.uk>
    Re: Problem with splitting data <uri@stemsystems.com>
        Why is postgresql beter than mysql? <gavcomedy@gmail.com>
    Re: Why is postgresql beter than mysql? <cartercc@gmail.com>
    Re: yet another question about numbers and strings <cartercc@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 20 Mar 2012 10:28:16 -0500
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: getting hash reference from a package
Message-Id: <3fidne4ap6iNP_XSnZ2dnUVZ_gydnZ2d@posted.hiwaay2>

Rainer Weikusat wrote:

: Since there's no class instance in the example above, it obviously
: doesn't make sense to call it 'a singleton'. OTOH, both serve the
: same purpose: Express the concept of 'a subsystem', that is, some set
: of state variables shared by some set of 'public' interface routines
: intended to solve a subproblem of the problem the program containing
: this code is supposed to solve, in Java. For the purpose of this
: discussion, ie, the 'global variable dressed in fancy clothes' issue,
: both have the same properties and there's actually nothing wrong with
: that: A subsystem is isomorph to a single instance of some class
: (that's exploited by the so-called 'singleton pattern') and this
: implies that any single object is nothing but 'a fancily dressed
: global variable'. 

I'm not shooting from the hip here. A well-known point of anguish
with singletons is they make testing difficult and sometimes
excruciating. Substitution and mocking are painful. As with global
variables, interaction with singletons is not expressed in the
interface, so the poor developer has to remember that particular
implementation detail.

These considerations apply to bags of static methods too because the
approach also comes with these severe drawbacks.

: Greg Bacon wrote
:
: > Rather than imposing the single-instance constraint, creating a
: > singleton as a shortcut to bypass threading the instance through
: > the call graph is a frustratingly common practice.
:
: Hmm ... what is this supposed to mean?

The singleton pattern is supposed to enforce the constraint that a
given system will never have more than a single instance.

As for actual usage in the wild, I wouldn't be shocked at all
to learn that 4 out of 5 singletons have no single-instance
requirement. Without this constraint, the singleton pattern as
commonly implemented offers the specious benefit of providing access
to the "singleton" instance without having to go to all the trouble
of, you know, passing the "singleton" instance to client methods.

In other words, it tends to be a sign of sloppy programming, sloppy
for the same reasons and because of the same drawbacks as global
variables.

Greg
-- 
"Those who deliberately sign their names to deception will be punished,"
[President Bush] said, leaving out that this is precisely what happens
every time he signs a budget or a law, or Congress votes.
    -- Lew Rockwell


------------------------------

Date: Tue, 20 Mar 2012 15:12:18 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Increment with '#' and decrement with 'b'
Message-Id: <ibjm39-eoe1.ln1@anubis.morrow.me.uk>


Quoth Henry Law <news@lawshouse.org>:
> 
[ representing musical notes as A=>0, A#=>1, Bb=>1, B=>2, &c.]
>
> Actually in this project I'm using a notation that already exists, for 
> better or worse; it's the one in the vast number of chord charts that I 
> have which need transposing for different singers!  I need to be able to 
> transpose C Am7 Dm7 G7#5b9 to, say, Eb Cm7 Fm7 Bb7#5b9 (and without it 
> coming out as A#7#5b9 either).  And so on.

I don't think you can do that with this representation. Consider
transposing

    C   F   G   C   =>  3   8   10  3

into B major

    2   7   9   2   =>  B   E   F#  B

and Db major

    4   9   11  4   =>  Db  Gb  Ab  Db

The enharmonic information you need to distinguish between F# and Gb is
exactly what you've just thrown away.

I would do a transposing job like this with a circle-of-fifths
representation, perhaps something like

    my %sharps  = qw( F 0  C 1  G 2  D 3  A 4  E 5  B 6 );
    my %acc     = qw( # +7  b -7  x +14  ## +14  bb -14 );
    my ($accs)  = map qr/$_/, join "|", keys %acc;
    $acc{""}    = 0;

    my ($note, $acc, $mods) = /([A-G])($accs)(.*)/i
        or die "not a valid chord symbol: '$_'\n";

    my $chord = $sharps{uc $note} + $acc{lc $acc};
    # transpose $chord

    my %base    = reverse %sharps;
    my %offset  = reverse %acc;
    $offset{14} = "x";              # or ## if you prefer
    
    my $base    = $chord % 7;
    my $offset  = $chord - $base;

    $offset > 14    and die "triple-sharp required\n";
    $offset < -14   and die "triple-flat required\n";

    my $symbol  = "$base{$base}$offset{$offset}$mods";

I don't really like starting with F, but it seems to be the easiest way
to make it work: otherwise you end up with code like

    my $base = $chord % 7;
    $base == 6 and $base = -1;

which is just ugly when it can easily be avoided.

Ben



------------------------------

Date: Tue, 20 Mar 2012 13:23:22 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Problem with splitting data
Message-Id: <fV45K0OBJxbE-pn2-3BetiOd2i0mf@localhost>

I need a quick and dirty method to hack strings out of a binary file. 
The data looks like this, via a hex dump

02414343 54444953 4B4E414D 45000000

Loads of hex 00 later another string appears and so on for a lot of 
data.

open BINFILE............
binmode BINFILE;
my $data = <BINFILE>;

my @a = split //, $data # get each character

Now I need the first byte as a number, the characters as characters 
and skip the hex '00's But any test I do in a for loop on @a whinges 
about not numeric in test.Or the opposite depending on the test and/or
the value of the byte. At the very basic level I can't even count how 
many hex'00's there are as I can't figure out a test that works. There
is no indication as to where the strings appear, no buried lengths 
etc..I suspect I need to use unpack or something but am at a loss.

TIA
 
-- 
Regards
Dave Saville


------------------------------

Date: Tue, 20 Mar 2012 14:10:34 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Problem with splitting data
Message-Id: <qnfm39-mmd1.ln1@anubis.morrow.me.uk>


Quoth "Dave Saville" <dave@invalid.invalid>:
> I need a quick and dirty method to hack strings out of a binary file. 
> The data looks like this, via a hex dump
> 
> 02414343 54444953 4B4E414D 45000000
> 
> Loads of hex 00 later another string appears and so on for a lot of 
> data.

Are there any \x00 within the strings? Can the initial byte be 0?

> open BINFILE............

Use variables for your filehandles:

    open my $BINFILE, ...;

> binmode BINFILE;
> my $data = <BINFILE>;

Assuming 'no' above, you want something like

    local $/ = "\0";
    while (<$BINFILE>) {
        chomp;
        length and last;
    }

Reading with the default of $/="\n" will break up the strings in
completely the wrong places.

If you can't assume the initial byte is not 0, you will need to read it
separately (for which you might as well use getc, though I would
probably set $/=\1 and use <> instead, just out of habit). If you can't
assume the string doesn't contain any nuls you will need to work out how
you *can* tell where the end is supposed to be.

> my @a = split //, $data # get each character

This is usually a bad idea. Perl has lots of powerful string operators,
but rather fewer for lists, so it's generally best to leave strings as
strings.

> Now I need the first byte as a number, the characters as characters 
> and skip the hex '00's But any test I do in a for loop on @a whinges 
> about not numeric in test.Or the opposite depending on the test and/or
> the value of the byte. At the very basic level I can't even count how 
> many hex'00's there are as I can't figure out a test that works.

    tr/\0//

or, if you only want to count the ones at the end

    my ($nuls) = /(\0*)$/;
    my $count = length $nuls;

> There
> is no indication as to where the strings appear, no buried lengths 
> etc..I suspect I need to use unpack or something but am at a loss.

Do you mean this?

    my ($byte, $str) = /(.)(.*)/s;
    $byte = ord $byte;

You could also use unpack, like

    my ($byte, $str) = unpack "Ca*";

(assuming your byte is unsigned).

Ben



------------------------------

Date: Tue, 20 Mar 2012 17:27:54 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: Problem with splitting data
Message-Id: <fV45K0OBJxbE-pn2-3Q9OMOBrQJzv@localhost>

On Tue, 20 Mar 2012 14:10:34 UTC, Ben Morrow <ben@morrow.me.uk> wrote:

Hi Ben

> 
> Quoth "Dave Saville" <dave@invalid.invalid>:
> > I need a quick and dirty method to hack strings out of a binary file. 
> > The data looks like this, via a hex dump
> > 
> > 02414343 54444953 4B4E414D 45000000
> > 
> > Loads of hex 00 later another string appears and so on for a lot of 
> > data.
> 
> Are there any \x00 within the strings? Can the initial byte be 0?
> 
> > open BINFILE............
> 
> Use variables for your filehandles:
> 
>     open my $BINFILE, ...;
> 
> > binmode BINFILE;
> > my $data = <BINFILE>;
> 
> Assuming 'no' above, you want something like
> 
>     local $/ = "\0";
>     while (<$BINFILE>) {
>         chomp;
>         length and last;
>     }
> 
> Reading with the default of $/="\n" will break up the strings in
> completely the wrong places.
> 

I had set it to undef. I see what you are doing but balked because 
there are thousands of consecutive x'00's :-)

> If you can't assume the initial byte is not 0, you will need to read it
> separately (for which you might as well use getc, though I would
> probably set $/=\1 and use <> instead, just out of habit). If you can't
> assume the string doesn't contain any nuls you will need to work out how
> you *can* tell where the end is supposed to be.
> 
> > my @a = split //, $data # get each character
> 
> This is usually a bad idea. Perl has lots of powerful string operators,
> but rather fewer for lists, so it's generally best to leave strings as
> strings.
> 

Too used to C loops over char foo[] :-)
 
> > Now I need the first byte as a number, the characters as characters 
> > and skip the hex '00's But any test I do in a for loop on @a whinges 
> > about not numeric in test.Or the opposite depending on the test and/or
> > the value of the byte. At the very basic level I can't even count how 
> > many hex'00's there are as I can't figure out a test that works.
> 
>     tr/\0//
> 
> or, if you only want to count the ones at the end
> 
>     my ($nuls) = /(\0*)$/;
>     my $count = length $nuls;
> 
> > There
> > is no indication as to where the strings appear, no buried lengths 
> > etc..I suspect I need to use unpack or something but am at a loss.
> 
> Do you mean this?
> 
>     my ($byte, $str) = /(.)(.*)/s;
>     $byte = ord $byte;
> 

Yes that is exactly what I need. I was only trying to count as a micky
mouse to get the null not null test correct. Had not come across ord 
before.

Thanks ever so much - now getting the one bit of data I need out of a 
60K file of mostly zeros!
-- 
Regards
Dave Saville


------------------------------

Date: Tue, 20 Mar 2012 17:38:19 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Problem with splitting data
Message-Id: <87obrri35g.fsf@sapphire.mobileactivedefense.com>

"Dave Saville" <dave@invalid.invalid> writes:
> 4B4E414D 45000000
>
> Loads of hex 00 later another string appears and so on for a lot of 
> data.

This is actually fairly simple. Assuming that you have the binary data
in a Perl scalar, a solution would be:

-----------------
use Devel::Peek;

my $in = "\x2\x41\x43\x43\x0\x0\x0\x0\x2\x54\x44\x49\x53\x0\x0\x0\x0\x0\x2\x4B\x4E\x41\x4D\x0";
my @s;

@s =  $in =~ /\x0*([^\x0]+)/g;

Dump($_) for @s;
-----------------


------------------------------

Date: Tue, 20 Mar 2012 17:51:34 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Problem with splitting data
Message-Id: <87k42fi2jd.fsf@sapphire.mobileactivedefense.com>

Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> "Dave Saville" <dave@invalid.invalid> writes:
>> 4B4E414D 45000000
>>
>> Loads of hex 00 later another string appears and so on for a lot of 
>> data.
>
> This is actually fairly simple. Assuming that you have the binary data
> in a Perl scalar, a solution would be:
>
> -----------------
> use Devel::Peek;
>
> my $in = "\x2\x41\x43\x43\x0\x0\x0\x0\x2\x54\x44\x49\x53\x0\x0\x0\x0\x0\x2\x4B\x4E\x41\x4D\x0";
> my @s;
>
> @s =  $in =~ /\x0*([^\x0]+)/g;
>
> Dump($_) for @s;
> -----------------

Variant which actually takes the two parts apart:

-----------
my $in = "\x2\x41\x43\x43\x0\x0\x0\x0\x2\x54\x44\x49\x53\x0\x0\x0\x0\x0\x2\x4B\x4E\x41\x4D\x0\x3\x44";

printf("#%u, '%s'\n", unpack('CA*', $1)) while $in =~ /\x0*([^\x0]+)/g;
-----------

						


------------------------------

Date: Tue, 20 Mar 2012 17:54:08 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: Problem with splitting data
Message-Id: <fV45K0OBJxbE-pn2-kksvyIBNzbpT@localhost>

On Tue, 20 Mar 2012 17:38:19 UTC, Rainer Weikusat 
<rweikusat@mssgmbh.com> wrote:

> "Dave Saville" <dave@invalid.invalid> writes:
> > 4B4E414D 45000000
> >
> > Loads of hex 00 later another string appears and so on for a lot of 
> > data.
> 
> This is actually fairly simple. Assuming that you have the binary data
> in a Perl scalar, a solution would be:
> 
> -----------------
> use Devel::Peek;
> 
> my $in = "\x2\x41\x43\x43\x0\x0\x0\x0\x2\x54\x44\x49\x53\x0\x0\x0\x0\x0\x2\x4B\x4E\x41\x4D\x0";
> my @s;
> 
> @s =  $in =~ /\x0*([^\x0]+)/g;
> 
> Dump($_) for @s;
> -----------------

Neat, but it was Ben's trick of using null as the input line delimiter
that makes it managable. I was trying to do it when the whole file was
in a scalar.

-- 
Regards
Dave Saville


------------------------------

Date: Tue, 20 Mar 2012 18:09:29 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Problem with splitting data
Message-Id: <87fwd3i1pi.fsf@sapphire.mobileactivedefense.com>

"Dave Saville" <dave@invalid.invalid> writes:
> On Tue, 20 Mar 2012 17:38:19 UTC, Rainer Weikusat 
> <rweikusat@mssgmbh.com> wrote:
>
>> "Dave Saville" <dave@invalid.invalid> writes:
>> > 4B4E414D 45000000
>> >
>> > Loads of hex 00 later another string appears and so on for a lot of 
>> > data.
>> 
>> This is actually fairly simple. Assuming that you have the binary data
>> in a Perl scalar, a solution would be:
>> 
>> -----------------
>> use Devel::Peek;
>> 
>> my $in = "\x2\x41\x43\x43\x0\x0\x0\x0\x2\x54\x44\x49\x53\x0\x0\x0\x0\x0\x2\x4B\x4E\x41\x4D\x0";
>> my @s;
>> 
>> @s =  $in =~ /\x0*([^\x0]+)/g;
>> 
>> Dump($_) for @s;
>> -----------------
>
> Neat, but it was Ben's trick of using null as the input line delimiter
> that makes it managable. I was trying to do it when the whole file was
> in a scalar.

For something as small as '60K of data', reading the complete file in
one go,

	$/ = undef;
        $in = <$binfile>

and parsing it in memory with the help of the regex engine makes a lot
more sense than reading it as sequence of 0-delimited records,
especially if a sequences of 0s will result in as many empty records
the program needs to throw away.


------------------------------

Date: Tue, 20 Mar 2012 18:29:39 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: Problem with splitting data
Message-Id: <fV45K0OBJxbE-pn2-3oyB4GWL1lud@localhost>

On Tue, 20 Mar 2012 18:09:29 UTC, Rainer Weikusat 
<rweikusat@mssgmbh.com> wrote:

> "Dave Saville" <dave@invalid.invalid> writes:
> > On Tue, 20 Mar 2012 17:38:19 UTC, Rainer Weikusat 
> > <rweikusat@mssgmbh.com> wrote:
> >
> >> "Dave Saville" <dave@invalid.invalid> writes:
> >> > 4B4E414D 45000000
> >> >
> >> > Loads of hex 00 later another string appears and so on for a lot of 
> >> > data.
> >> 
> >> This is actually fairly simple. Assuming that you have the binary data
> >> in a Perl scalar, a solution would be:
> >> 
> >> -----------------
> >> use Devel::Peek;
> >> 
> >> my $in = "\x2\x41\x43\x43\x0\x0\x0\x0\x2\x54\x44\x49\x53\x0\x0\x0\x0\x0\x2\x4B\x4E\x41\x4D\x0";
> >> my @s;
> >> 
> >> @s =  $in =~ /\x0*([^\x0]+)/g;
> >> 
> >> Dump($_) for @s;
> >> -----------------
> >
> > Neat, but it was Ben's trick of using null as the input line delimiter
> > that makes it managable. I was trying to do it when the whole file was
> > in a scalar.
> 
> For something as small as '60K of data', reading the complete file in
> one go,
> 
> 	$/ = undef;
>         $in = <$binfile>
> 
> and parsing it in memory with the help of the regex engine makes a lot
> more sense than reading it as sequence of 0-delimited records,
> especially if a sequences of 0s will result in as many empty records
> the program needs to throw away.

Yup that was why I was trying to do it in one lump - I might play 
again later if I need a lot more data actully pulled out of the file 
but for the current purpose it's OK. The data I want is very near the 
front so it's not too bad.
-- 
Regards
Dave Saville


------------------------------

Date: Tue, 20 Mar 2012 14:49:48 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Problem with splitting data
Message-Id: <a8bf9ab4-2463-4d0c-95d7-64aaafb03cb1@i18g2000vbx.googlegroups.com>

On Mar 20, 9:23=A0am, "Dave Saville" <d...@invalid.invalid> wrote:
> I need a quick and dirty method to hack strings out of a binary file.
> The data looks like this, via a hex dump
>
> 02414343 54444953 4B4E414D 45000000
>
> Loads of hex 00 later another string appears and so on for a lot of
> data.

I don't know if this will work in Perl, but it certainly works well in
Lisp!

Think of your file as a list of characters, and read the file
character by character, or byte by byte, feeding each character/byte
into a conditional. If the character matches a digit, concat the digit
on to a string variable. If the character matches a white space
character, skip it, push the previous variable on to an array, and
look at the next character.

When you finish, you can iterate through the array looking at each
array element, converting the digits to a hex or not, however you want
to do it. If it matches a printable character, it's part of a string.
If it doesn't, skip the element and go on to the next element.

Zeros and ones are just zeros and ones. You can interpret them as
ASCII or as numbers or as anything else. Lisp doesn't have a
tokenizer, like split() for example, but it's so easy to write your
own tokenizer that you don't miss it. Surely you should be able to so
something similar with Perl.

I have copied below a (pretty bad, I admit) script and a sample data
file, which is actually a perl program to illustrate what I mean.

CC.

#! perl

use strict;
use warnings;

open IN, '<', 'testhex.bin' or die $!;
binmode IN;
my ($buffer, $data, $number);
my ($first, $second, $finaltext);
my $temp =3D '';
my @data =3D ();

while (($number =3D read IN, $data, 1) !=3D 0)
{
    $buffer .=3D $data;
    if ($data =3D~ /\s/)
    {
       print "data matches whitespace [$data]\n";
       push @data, $temp;
       $temp =3D '';
       next;
    }
    else
    {
       print "data does not match whitespace [$data]\n";
        $temp .=3D $data;
    }
}
close IN;

foreach my $ele (@data)
{
    unless ($ele =3D~ /\w/) { $finaltext .=3D ' '; next; }
    $ele =3D~ /(\w{2})(\w{2})/;
    $first =3D chr(hex($1));
    $second =3D chr(hex($2));
    $finaltext .=3D "$first$second";
}

print "printing data string\n--------------------\n$buffer\n";
print "printing data array\n--------------------\n@data\n";
print "printing final text\n--------------------\n$finaltext\n";

exit(0);

2321 2070 6572 6c0d 0a75 7365 2073 7472
6963 743b 0d0a 7573 6520 7761 726e 696e
6773 3b0d 0a0d 0a6d 7920 2824 6831 2c20
2468 322c 2024 6833 293b 0d0a 0d0a 2468
3120 3d20 7b20 6669 7273 7420 3d3e 2027
5761 7368 696e 6774 6f6e 272c 0d0a 2020
2020 2020 2020 2020 2073 6563 6f6e 6420
3d3e 2027 4164 616d 7327 2c0d 0a20 2020
2020 2020 2020 2020 7468 6972 6420 3d3e
2027 4a65 6666 6572 736f 6e27 2c0d 0a20
2020 2020 2020 2020 2020 666f 7572 7468
203d 3e20 274d 6164 6973 6f6e 272c 0d0a
2020 2020 2020 2020 2020 2066 6966 7468
203d 3e20 274d 6f6e 726f 6527 2c0d 0a20
2020 2020 2020 7d3b 0d0a 0d0a 2468 3220
3d20 7b20 5761 7368 696e 6774 6f6e 203d
3e20 3137 3838 2c0d 0a20 2020 2020 2020
2020 2020 4164 616d 7320 3d3e 2031 3739
322c 0d0a 2020 2020 2020 2020 2020 204a
6566 6665 7273 6f6e 203d 3e20 3138 3030
2c0d 0a20 2020 2020 2020 2020 2020 4d61
6469 736f 6e20 3d3e 2031 3830 382c 0d0a
2020 2020 2020 2020 2020 204d 6f6e 726f
6520 3d3e 2031 3831 362c 0d0a 2020 2020
2020 207d 3b0d 0a0d 0a70 7269 6e74 2022
5c6e 6669 7273 7420 6861 7368 3a5c 6e22
3b0d 0a66 6f72 6561 6368 206d 7920 246b
2028 6b65 7973 2025 7b24 6831 7d29 207b
2070 7269 6e74 2022 246b 203d 3e20 2468
312d 3e7b 246b 7d5c 6e22 3b20 7d0d 0a70
7269 6e74 2022 5c6e 7365 636f 6e64 2068
6173 683a 5c6e 223b 0d0a 666f 7265 6163
6820 6d79 2024 6b20 286b 6579 7320 257b
2468 327d 2920 7b20 7072 696e 7420 2224
6b20 3d3e 2024 6832 2d3e 7b24 6b7d 5c6e
223b 207d 0d0a 0d0a 666f 7265 6163 6820
6d79 2024 6b20 286b 6579 7320 257b 2468
317d 290d 0a7b 0d0a 2020 2020 2468 332d
3e7b 246b 7d20 3d20 7b20 6e61 6d65 203d
3e20 2468 312d 3e7b 246b 7d2c 0d0a 2020
2020 2020 2020 2020 2020 2020 2020 2020
7965 6172 203d 3e20 2468 322d 3e7b 2468
312d 3e7b 246b 7d7d 2c0d 0a20 2020 2020
2020 2020 2020 2020 207d 0d0a 7d0d 0a0d
0a70 7269 6e74 2022 5c6e 7468 6972 6420
6861 7368 3a5c 6e22 3b0d 0a66 6f72 6561
6368 206d 7920 246b 3120 286b 6579 7320
257b 2468 337d 290d 0a7b 0d0a 2020 2020
7072 696e 7420 225c 6e24 6b31 203d 3e20
5c6e 223b 0d0a 2020 2020 666f 7265 6163
6820 6d79 2024 6b32 2028 6b65 7973 2025
7b24 6833 2d3e 7b24 6b31 7d7d 290d 0a20
2020 207b 0d0a 2020 2020 2020 2020 7072
696e 7420 2220 246b 3220 2d3e 2024 6833
2d3e 7b24 6b31 7d7b 246b 327d 2c20 223b
0d0a 2020 2020 7d0d 0a7d 0d0a 0d0a 6578
6974 2830 293b 2020 2020 2020 2020 2020
200d 0a


------------------------------

Date: Tue, 20 Mar 2012 22:06:24 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Problem with splitting data
Message-Id: <0kbn39-ojl1.ln1@anubis.morrow.me.uk>


Quoth "Dave Saville" <dave@invalid.invalid>:
> On Tue, 20 Mar 2012 14:10:34 UTC, Ben Morrow <ben@morrow.me.uk> wrote:
> > 
> >     local $/ = "\0";
> >     while (<$BINFILE>) {
> >         chomp;
> >         length and last;
> >     }
> > 
> > Reading with the default of $/="\n" will break up the strings in
> > completely the wrong places.
> > 
> 
> I had set it to undef. I see what you are doing but balked because 
> there are thousands of consecutive x'00's :-)

It makes less difference than you might think, since Perl's IO is
buffered by default. Still, if you're happy to slurp the whole file
something like

    use File::Slurp qw/slurp/;

    my $data = slurp $binfile;

    while (
        my ($byte, $str) = $data =~ s/^\0*(.)([^\0]*)//g
    ) {
        $byte = ord $byte;
        ...
    }

is simpler and (probably) faster.

Ben



------------------------------

Date: Tue, 20 Mar 2012 19:23:24 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Problem with splitting data
Message-Id: <874ntiuaab.fsf@stemsystems.com>

>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:

  BM>     use File::Slurp qw/slurp/;

  BM>     my $data = slurp $binfile;

most people use read_file. slurp is there for way backwards
compatibility.

and you still need to binmode this. slurping assumes text mode. on linux
it makes no difference but on winblows it does.

  BM>     while (
  BM>         my ($byte, $str) = $data =~ s/^\0*(.)([^\0]*)//g
  BM>     ) {
  BM>         $byte = ord $byte;
  BM>         ...
  BM>     }

  BM> is simpler and (probably) faster.

or you can even split on /\0+/ and deal with each non-zero segment that
way. easy enough to get and delete the first byte with 4 arg substr:

	foreach my $seg ( split $data, /\0+/ ) {

		my $byte = substr( $seg, 0, 1, '' ) ;

# $seg now has the rest as the leading byte was replaced with ''

	}

uri


------------------------------

Date: Tue, 20 Mar 2012 11:37:12 -0700 (PDT)
From: quiet_lad <gavcomedy@gmail.com>
Subject: Why is postgresql beter than mysql?
Message-Id: <678bcbd7-1686-4000-987c-1c9cdb288aaf@r2g2000pbs.googlegroups.com>

If it is is it? I hear from most hackers pg is bestest adn even that
firebird db is better than the mysql


------------------------------

Date: Tue, 20 Mar 2012 14:55:52 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Why is postgresql beter than mysql?
Message-Id: <48729003-3302-4a13-8c36-fcd1a84f7623@i18g2000vbx.googlegroups.com>

On Mar 20, 2:37=A0pm, quiet_lad <gavcom...@gmail.com> wrote:
> If it is is it? I hear from most hackers pg is bestest adn even that
> firebird db is better than the mysql

I use MySQL, PostgreSQL, and SQLite regularly.

I have nothing but praise for SQLite for quick and dirty scripts that
need data persistence.

I also like PostgreSQL for web apps better than MySQL because of the
security architecture. PostgreSQL sets up each database as a separate
instance and isolates it from the other databases. In MySQL, all
databases exist under the one instance.

I also have found the PostgreSQL array datatype useful on occasion,
despite it not being in first normal form -- but then I am a long time
and heavy user of UniData, a non-first-normal-form relational
database, so I'm used to multi-valued cells.

If I had my choice, I'd use pgsql, but I haven'[t (yet) got it to work
on Windows, so I use MySQL on Windows. Both will do what you want.

CC.


------------------------------

Date: Tue, 20 Mar 2012 07:30:04 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: yet another question about numbers and strings
Message-Id: <c9cd6712-2efe-493f-a01d-8dccda70897e@hv2g2000vbb.googlegroups.com>

On Mar 19, 7:09=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> This would have made no difference in the OP's case. The problem was
> (essentially) a bug in DBD::Sybase, which was losing information before
> the data ever got back to Perl.

Thanks, Ben. I felt as if I were missing something, but couldn't put
my finger on it.

Would this have happened if he had used an OBDC driver? Or perhaps he
could have used MSDTS or MSSIS to pull the data as a text file?

In my own practice, we use both DTS and SIS to import to and export
data from SQL Server, depending on the preference of the one who's
doing the work, the purpose for the data, and sometimes (it seems) on
the alignment of the planets.

CC.


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3645
***************************************


home help back first fref pref prev next nref lref last post