[25810] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8049 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 4 14:05:43 2005

Date: Wed, 4 May 2005 11:05:05 -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           Wed, 4 May 2005     Volume: 10 Number: 8049

Today's topics:
        a numbering question <geoff.cox@notquitecorrectfreeuk.com>
    Re: a numbering question <mark.clementsREMOVETHIS@wanadoo.fr>
    Re: a numbering question <jurgenex@hotmail.com>
    Re: a numbering question <tadmc@augustmail.com>
        Filling the Perl Stack <guenther.sohler@newlogic.com>
    Re: How do I parse certain "bits" out of bytes? xhoster@gmail.com
    Re: How do I parse certain "bits" out of bytes? <schneiderNOSPAM@N0SPAM.pobox.com>
    Re: How do I parse certain "bits" out of bytes? <schneiderNOSPAM@N0SPAM.pobox.com>
    Re: How do I parse certain "bits" out of bytes? <1usa@llenroc.ude.invalid>
    Re: How do I parse certain "bits" out of bytes? <tadmc@augustmail.com>
    Re: How do I parse certain "bits" out of bytes? <tadmc@augustmail.com>
    Re: How do I parse certain "bits" out of bytes? <schneiderNOSPAM@N0SPAM.pobox.com>
    Re: regular expression <pilkowsk@informatik.uni-marburg.de>
    Re: start_table problem <noreply@gunnar.cc>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 04 May 2005 13:09:26 GMT
From: Geoff Cox <geoff.cox@notquitecorrectfreeuk.com>
Subject: a numbering question
Message-Id: <d8ih715j12ahn6foloap88jqhsrv1uljtu@4ax.com>

Hello,

I have some images named as

pic001.gif through pic010.gif to pic200.gif

I am not clear how I can use something like

pic$number++

any pointers please!

Cheers

Geoff


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

Date: 04 May 2005 13:15:13 GMT
From: "Mark Clements" <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: a numbering question
Message-Id: <4278cae1$0$11726$8fcfb975@news.wanadoo.fr>

Geoff Cox wrote:

> Hello,
> 
> I have some images named as
> 
> pic001.gif through pic010.gif to pic200.gif
> 
> I am not clear how I can use something like
> 
> pic$number++
> 
> any pointers please!
> 
> Cheers
> 
> Geoff

check out sprintf.

Mark


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

Date: Wed, 04 May 2005 13:17:38 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: a numbering question
Message-Id: <SX3ee.27449$r81.4328@trnddc02>

Geoff Cox wrote:
> I have some images named as
> pic001.gif through pic010.gif to pic200.gif
> I am not clear how I can use something like
> pic$number++

Does it have to be a separate "pic" and $number or would it be possible to 
increment the file name as a whole?

my $name = "pic001";
for (0..20) {
    print $name++ . ".gif\n";
}

jue 




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

Date: Wed, 4 May 2005 11:48:27 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: a numbering question
Message-Id: <slrnd7hv6r.7vu.tadmc@magna.augustmail.com>

Geoff Cox <geoff.cox@notquitecorrectfreeuk.com> wrote:

> I have some images named as
> 
> pic001.gif through pic010.gif to pic200.gif
> 
> I am not clear how I can use something like
> 
> pic$number++
> 
> any pointers please!


   my $filename = sprint 'pic%03d.gif', $number++;  # untested


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Wed, 04 May 2005 16:04:30 +0200
From: Guenther Sohler <guenther.sohler@newlogic.com>
Subject: Filling the Perl Stack
Message-Id: <pan.2005.05.04.14.04.29.980027@newlogic.com>

In my embedded perl I decided to handle a 2D coordinate as
a anonymous reference to an array of 2 numbers like
[ 5.6 , 3.4 ]

Therefore a list of points would be like

[ [ 1,2 ] , [ 3,4 ] , [ 5,6] ]

In C I want to put this onto the perl stack.

I have written this C code.


int j;
AV *points=newAV();
for(j=0;j<pars[i].points.len();j++)
{
	AV *point=newAV();
	av_push(point,newSVnv(pars[i].points[j].x*l.unit_db));
	av_push(point,newSVnv(pars[i].points[j].y*l.unit_db));
	av_push(points,newSVrv((SV *) point,NULL));
}
XPUSHs(sv_2mortal(newSVrv((SV *) points,NULL)));


But the data does not arrive in the perl function.
Did I do an error ?
Do I have to use more/less mortal ??
Do I have to free/not free any references, arrays :?

The perl code looks like

sub test
{
        $coords=shift;
        print scalar(@$coords)." coords found\n";
}

I expect the perl routine to display
"2 coords found"

but it just displays " coords found"

Whats wrong ?






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

Date: 04 May 2005 15:46:13 GMT
From: xhoster@gmail.com
Subject: Re: How do I parse certain "bits" out of bytes?
Message-Id: <20050504114613.655$hT@newsreader.com>

Adam Schneider <schneiderNOSPAM@N0SPAM.pobox.com> wrote:
> Josef Moellers wrote:
> >
> > Perl knows shift and logical operators:
> >
> > $n1 = ($n >> 3) & 0x1f;
> > $n2 = $n & 7;
>
> But what's actually going on here?  To a layperson, the meanings of the
> terms "shift" and "logical operators" are opaque in this context and do
> not obviously correlate with the ">>" and "&" symbols in those
> statements.

They do obviously correlate with those symbols if you look up those
operators in the docs.  That is what I, who also has no formal compsci
edumacation, did.

> (What do the operators do?

">>" shifts the bits of $n over (to less significance) 3 places, so that
the 5 leftmost bits are now the 5 rightmost bits.  The "variable &
constant" is used to mask out bits in the variable you don't want.

>  Why have you used 0x1f there?)

Because 0x1f is the hex code for the 5 rightmost bits, which is what you
want to keep (after the shifting).

> If someone can point me to a page somewhere that will explain how to
> use these sorts of things, starting from square one, I'd appreciate it.

You already know what 'e' is 01100101, so it seems like you are already
beyond square one.  I don't how you got that far, but maybe continuing
whatever it was you did to get that far would be a good idea.  Beyond
that, just googling on "Bitwise operators" leads to tons of useful
hits.  (Don't just stick to a Perl-centric view, take a look at the ones
for Java and especially C)

> As I said, the documentation on these subjects is useless for those of
> us who don't know the old-school programming terminology.

Well, Perl is mostly a "new school" language, so it is to be expected
that the docs don't go out of their way to explain old-school concepts.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Wed, 04 May 2005 08:58:23 -0700
From: Adam Schneider <schneiderNOSPAM@N0SPAM.pobox.com>
Subject: Re: How do I parse certain "bits" out of bytes?
Message-Id: <040520050858234997%schneiderNOSPAM@N0SPAM.pobox.com>

Fabian Pilkowski wrote:
>
> Let's assume $n contains one byte, e.g. 0x65. Its decimal representation
> is 101, and its binary one is 0b01100101. First, have a look at this
> shift operator -- just shift all the bits by 3 positions to the right.
> Since shifting is null-padded you'll get
> 
>     0b01100101 >> 3 = 0b00001100 = 12
> 
> Simplified, this just cuts off the last three bits. The trailing "&0x1f"
> (or "&0b00011111") part ensures that only the last 5 bits are used. This
> could be only a problem if there are more than 8 bits stored in $n.

Thanks, Fabian.  Your explanation is very helpful -- and being able to
write the numbers in binary does make it more clear.  Now, what do you
mean when you say "this could be only a problem if there are more than
8 bits"?  Because in my real-world example, I need to get data from 32
bits.  Can I not use these same techniques?


Adam


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

Date: Wed, 04 May 2005 09:08:56 -0700
From: Adam Schneider <schneiderNOSPAM@N0SPAM.pobox.com>
Subject: Re: How do I parse certain "bits" out of bytes?
Message-Id: <040520050908562952%schneiderNOSPAM@N0SPAM.pobox.com>

Josef Moellers wrote:
>
> > But what's actually going on here?  To a layperson, the meanings of the
> > terms "shift" and "logical operators" are opaque in this context and do
> > not obviously correlate with the ">>" and "&" symbols in those
> > statements.  (What do the operators do?  Why have you used 0x1f there?)
> 
> Sory, I thought if someone programmed in Perl, (s)he isn't a "layperson".

The wonderful thing about Perl is that it's forgiving enough and
(usually) intuitive enough that anyone can use it, even with no formal
programming background.

I've been using Perl to manipulate text and hierarchical data for
almost 10 years now, with no problems understanding the terminology,
but as soon as I venture into binary data, it's like the documentation
is written in another language.  (In addition to the bitwise operators,
the "pack" and "unpack" commands are confusing too; there are not
nearly enough examples in the manual.)


Adam


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

Date: Wed, 04 May 2005 16:23:24 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: How do I parse certain "bits" out of bytes?
Message-Id: <Xns964C7DEDFC93Basu1cornelledu@127.0.0.1>

Adam Schneider <schneiderNOSPAM@N0SPAM.pobox.com> wrote in
news:040520050908562952%schneiderNOSPAM@N0SPAM.pobox.com: 

> Josef Moellers wrote:
>>
>> > But what's actually going on here?  To a layperson, the meanings of
>> > the terms "shift" and "logical operators" are opaque in this
>> > context and do not obviously correlate with the ">>" and "&"
>> > symbols in those statements.  (What do the operators do?  Why have
>> > you used 0x1f there?) 
>> 
>> Sory, I thought if someone programmed in Perl, (s)he isn't a
>> "layperson". 
> 
> The wonderful thing about Perl is that it's forgiving enough and
> (usually) intuitive enough that anyone can use it, even with no formal
> programming background.
> 
> I've been using Perl to manipulate text and hierarchical data for
> almost 10 years now, with no problems understanding the terminology,
> but as soon as I venture into binary data, it's like the documentation
> is written in another language.  (In addition to the bitwise
> operators, the "pack" and "unpack" commands are confusing too; there
> are not nearly enough examples in the manual.)

I have been navigating the streets of Ankara, Turkey successfully for a 
long time now, but I am sure, as soon as I venture to Amsterdam, it's, 
like, the maps are written in a different language. Why should that be? 
After all, it is just another city.

I find it disturbing that in all these years of not being able to 
understand the documentation due to a lack of your knowledge, you never 
had the desire or motivation to pick a simple tutorial on boolean logic, 
or even to try and convert numbers among the commonly used bases such as 
2, 8, 10, and 16.

I find your response to Josef Moellers extremely disturbing. He helped 
you, and you paid him back by being obnoxious.

Sinan
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


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

Date: Wed, 4 May 2005 11:15:39 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: How do I parse certain "bits" out of bytes?
Message-Id: <slrnd7ht9b.7vu.tadmc@magna.augustmail.com>

Adam Schneider <schneiderNOSPAM@N0SPAM.pobox.com> wrote:
> 
> Here's a completely hypothetical example:


Why?

It could easily and unambiguously be an example in Real Perl Code.

Have you seen the Posting Guidelines that are posted here frequently?


> I have a binary file containing one byte: hex 65, or "e" in text.  I
> can get perl to read it and tell me that this is "01100101" in binary. 


If you can read it, then we don't need to know where it came from.

You can replace that whole sentence with a bit of Real Code:

   my $n = 0b01100101;


> But hidden in here are two pieces of information: the first 5 bytes are
> a number ranging from 0 to 32 and the last 3 bytes are another number
> between 0 and 8.  I need those two decimal numbers (in this case, 12
> and 5) put into two variables.


   my $first = ($n >> 3)        # shift all bits 3 postions to the right
                & 0b00011111;   # mask off all but last 5 bits

   my $second = $n & 0b00000111;   # mask off all but last 3 bits


> I have read all the documentation on "pack," "unpack," "vec," bitwise
> operators, and everything else, and I still have no idea how to extract
> a few bits out of a longer string of bytes.  The help files all seem to
> have been written with the assumption that I majored in computer
> science and learned how to program years ago in some pre-Perl language
> -- which I did not.


References for a programming language are not where you learn
how to program, they are where you learn how to write a program
in the language being discussed.

You need to learn what operations can be performed on your data
before you can look for those operations in Perl's fine manual.
Specifically, you can use "shift" and "and" operations to get
bit-ranges.

">>" is how you get a "shift" (right) in Perl.

"&" is how you do a bitwise "and" in Perl.


> Can someone please pass along a simple solution that will make sense to
> someone who doesn't know the hardcore geeky stuff?  


No.

You will need to understand your data in order to write a program
that manipulates your data. That is the nature of the beast.


> I know it's out
> there, I just can't find it.  


Googling for:

   binary tutorial

finds lots of hits.

Once you know what you want to do, *then* you turn your attention
to how to do that in Perl.


> (The closest I came was "vec," 


vec() treats its argument as a string, while you want to treat
it as a number, so vec() is not that close.


> but it
> doesn't let you ask for chunks that aren't powers of 2.)


If you want to use base-2 (binary), then all you *have* are powers of 2,
just as when you are working in base-10 all you have are powers of 10
(eg. ones, place, tens place, hundreds place...).


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Wed, 4 May 2005 11:46:01 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: How do I parse certain "bits" out of bytes?
Message-Id: <slrnd7hv29.7vu.tadmc@magna.augustmail.com>

Adam Schneider <schneiderNOSPAM@N0SPAM.pobox.com> wrote:

> If someone can point me to a page somewhere that will explain how to
> use these sorts of things, starting from square one, I'd appreciate it. 


Google for

   binary tutorial


> As I said, the documentation on these subjects is useless for those of
> us who don't know the old-school programming terminology.


Or those of us who don't know the modern-school programming terminology.

Nor the future-school terminology.

So it ends up being pretty much:

   who don't have schooling in the data type that needs processing

You must resign yourself to getting (just enough) schooling on
binary data if you need to process binary data.

You only need to learn two operations, then look in perlop.pod
to find out how to get those operations using Perl.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Wed, 04 May 2005 10:45:10 -0700
From: Adam Schneider <schneiderNOSPAM@N0SPAM.pobox.com>
Subject: Re: How do I parse certain "bits" out of bytes?
Message-Id: <040520051045109457%schneiderNOSPAM@N0SPAM.pobox.com>

A. Sinan Unur wrote:
> 
> I find it disturbing that in all these years... you never had the 
> desire or motivation to pick a simple tutorial on boolean logic

No, I didn't, for the same reason I've never picked up a tutorial on
speaking Swahili; I didn't need it.


> I find your response to Josef Moellers extremely disturbing. He helped 
> you, and you paid him back by being obnoxious.

Actually, Josef's message was somewhat helpful, but (to me) confusing;
my response only explained why it didn't make sense to me, and I asked
for clarification.  (And I wouldn't go accusing other people of
obnoxiousness after snidely commenting on the "lack of [my]
knowledge.")

The people in this newsgroup are acting as if I were attacking them
personally; my minor complaint is with the documentation, which makes
the assumption that the reader is already familiar with a lot of
technical programming terms, and furthermore has learned other
languages before perl.  (There is indeed good "beginner's"
documentation for perl, but it never really gets into binary data;
neither "Learning Perl" nor the "Perl Cookbook" were able to help.)


Thank you very much to Fabian Pilkowski and Jean-Luc Romano, who posted
very helpful responses without being condescending or attempting to
subtly (or overtly) insult me.  To the rest of you, my apologies for
trespassing in your little clubhouse.


Adam Schneider
Portland, OR


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

Date: Wed, 4 May 2005 15:45:43 +0200
From: Fabian Pilkowski <pilkowsk@informatik.uni-marburg.de>
Subject: Re: regular expression
Message-Id: <3ds1tcF6u7n36U1@individual.net>

* kums schrieb:

> Already i have written that program.
> look at below. but i want to reduce the line of code

You want to learn more about s///. As Bernhard has mentioned it could do
all the work for you. s/// is Perl's substitution operator which could
be ever used when something is to replace, e.g. replace two digits by a
comma followed by those digits.

Have you already looked into `perldoc -q commas` as I recommend in my
posting.

    sub commify {
        local $_  = shift;
        1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
        return $_;
    }

"^([-+]?\d+)(\d{3})" will search for an optional sign and all digits at
the beginning (and store that in $1) up to the last 3 ones which are
stored in $2. The replacement should be self-explanatory: Just insert a
comma between $1 and $2. Once you have understood the sub above you can
go on to modify it.

Doing the same s/// as above only *once* to insert a comma in front of
the last 3 digits. If this is successful you know there are at least 4
digits and you have to go on. Doing the same for each last 2 digits is
the right now, I think. Hence, I'll get something like:

    sub commify {
        local $_  = shift;
        if( s/^([-+]?\d+)(\d{3})/$1,$2/ ) {
            1 while s/^([-+]?\d+)(\d{2})/$1,$2/;
        }
        return $_;
    }

Retrospectively, I think it is not too hard to modify the example from
the FAQ I've already posted, isn't it? Just adding an if condition and
replace one "3" by one "2". Sounds very simple to me ;-)

Oh, since your orignal code doesn't work for negative numbers I'll have
an alternative to avoid regular expressions.

    sub commify {
        scalar reverse join ',', unpack 'A3(A2)*', reverse shift;
    }

regards,
fabian


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

Date: Wed, 04 May 2005 16:15:06 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: start_table problem
Message-Id: <3ds3k0F6u086hU1@individual.net>

Anno Siegel wrote:
> Gunnar Hjalmarsson wrote:
>> Anno Siegel wrote:
>>> Gunnar Hjalmarsson wrote:
>>>> Mark Clements wrote:
>>>>> I may have to abandon reading this newsgroup: there is little point
>>>>> in killfiling you because you generate so much traffic
>>>>
>>>> And *that* is not Nikos' fault, is it?
>>>
>>> It is an immediate consequence of his behavior.
>>
>> True, of course, but...  Don't we who respond have an option? Are we 
>> just helpless victims of the circumstances?
> 
> Individually we have a choice.  As a collective, there will always be
> *someone* who can't let a certain provocation slip by.  Given the number
> of provocations, the kind of thread we are seeing is hardly avoidable.

Self-criticism and reflection aren't harmful.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

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:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#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 V10 Issue 8049
***************************************


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