better way of writing this script

better way of writing this script

am 29.04.2011 09:27:28 von Agnello George

Hi All

is there a better way of writing this script


my @resultss = qw( 0 1 2 3 4 5 ) ;

foreach ( @resultss) {
if ( defined( $jvalue{$_}){
$status .= $jvalue{$_} ;
}
}


Thanks

--
Regards
Agnello D'souza

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

Re: better way of writing this script

am 29.04.2011 09:38:54 von Brian Fraser

--0016e6dd96340f612b04a209c469
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Apr 29, 2011 at 4:27 AM, Agnello George wrote:

> Hi All
>
> is there a better way of writing this script
>
>
"Better" is fairly subjective - And with only a part of the program, there
isn't much that can be said. Is $status a global variable, or lexical?

On the other hand, if you mean more idiomatic way.. That's also subjective
:P But here's my stab at it:
my $status = join '', grep defined $jvalue{$_}, qw( 0 1 2 3 4 5 );

Brian.

--0016e6dd96340f612b04a209c469--

Re: better way of writing this script

am 29.04.2011 09:39:30 von Uri Guttman

>>>>> "AG" == Agnello George writes:

AG> is there a better way of writing this script

this is not a script but a short code snippet. there are many ways to
redo this. many questions could be asked as well.

AG> my @resultss = qw( 0 1 2 3 4 5 ) ;

is that always a fixed list? could @jvalue have more then 6 entries? if
you want all of @jvalue you don't need @resultss at all.


AG> foreach ( @resultss) {

foreach ( 0 .. 5 ) {

or if you want all of @jvalue:

foreach ( 0 .. $#jvalue ) {

and you should use a named var for the loop counter:

foreach $j_ind ( 0 .. $#jvalue ) {

AG> if ( defined( $jvalue{$_}){
AG> $status .= $jvalue{$_} ;
AG> }
AG> }

that part is just getting the defined values from an array. grep is a
way to do that with a list. join can merge them and then you append them
to $status. now we don't even need the loop counter:

$status .= join( '', grep defined, @jvalue ) ;

that is the whole thing in one fairly clear line of code. perl is very
nice this way.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

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

Re: better way of writing this script

am 29.04.2011 09:54:54 von Agnello George

On Fri, Apr 29, 2011 at 1:09 PM, Uri Guttman wrote:
>>>>>> "AG" == Agnello George writes:
>
> =A0AG> is there a better way of writing this script
>
> this is not a script but a short code snippet. there are many ways to
> redo this. many questions could be asked as well.
>
> =A0AG> =A0my @resultss =3D qw( 0 1 2 3 4 5 ) ;
>
> is that always a fixed list? could @jvalue have more then 6 entries? if
> you want all of @jvalue you don't need @resultss at all.
>
>
> =A0AG> foreach ( @resultss) {
>
> =A0 =A0 =A0 =A0foreach ( 0 .. 5 ) {
>
> or if you want all of @jvalue:
>
> =A0 =A0 =A0 =A0foreach ( 0 .. $#jvalue ) {
>
> and you should use a named var for the loop counter:
>
> =A0 =A0 =A0 =A0foreach $j_ind ( 0 .. $#jvalue ) {
>
> =A0AG> if ( =A0defined( $jvalue{$_}){
> =A0AG> =A0$status .=3D $jvalue{$_} =A0;
> =A0AG> =A0}
> =A0AG> }
>
> that part is just getting the defined values from an array. grep is a
> way to do that with a list. join can merge them and then you append them
> to $status. now we don't even need the loop counter:
>
> =A0 =A0 =A0 =A0$status .=3D join( '', grep defined, @jvalue ) ;
>
> that is the whole thing in one fairly clear line of code. perl is very
> nice this way.
>
> uri
>
> --
> Uri Guttman =A0------ =A0uri@stemsystems.com =A0-------- =A0http://www.sy=
sarch.com --
> ----- =A0Perl Code Review , Architecture, Development, Training, Support =
------
> --------- =A0Gourmet Hot Cocoa Mix =A0---- =A0http://bestfriendscocoa.com=
---------
>


Thanks for all the replies

--=20
Regards
Agnello D'souza

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

Re: better way of writing this script

am 29.04.2011 10:02:59 von Uri Guttman

>>>>> "BF" == Brian Fraser writes:

BF> On Fri, Apr 29, 2011 at 4:27 AM, Agnello George wrote:
>> Hi All
>>
>> is there a better way of writing this script
>>
>>
BF> "Better" is fairly subjective - And with only a part of the program, there
BF> isn't much that can be said. Is $status a global variable, or lexical?

BF> On the other hand, if you mean more idiomatic way.. That's also subjective
BF> :P But here's my stab at it:
BF> my $status = join '', grep defined $jvalue{$_}, qw( 0 1 2 3 4 5 );

to pick a nit, the OP had $status .=

also why use qw for sequential numbers when a range will do?

so you can streamline yours with a slice:

my $status = join '', grep defined, @jvalue{ 0 .. 5 } ;

the only difference from mine is your is limited to the first 6 elements
and my used the whole array. without more from the OP you can't tell
which one is better suited.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

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

Re: better way of writing this script

am 29.04.2011 10:15:30 von Brian Fraser

--000325558c36f97e8104a20a4619
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Apr 29, 2011 at 5:02 AM, Uri Guttman wrote:

>
> also why use qw for sequential numbers when a range will do?
>
> Because I went stupid and just blindly copypasted instead of thinking :)
Seriously, why use qw when I'm not _quoting words_?


> so you can streamline yours with a slice:
>
> my $status = join '', grep defined, @jvalue{ 0 .. 5 } ;
>
> This one was on purpose though - The slice might insert new keys into the
hash, and I recently spent the good half of an hour debugging just that..!

without more from the OP you can't tell
> which one is better suited.
>
> While I agree, I think that yours is the way to go -- Hash slices don't get
enough love.

Thanks for the corrections, Uri!

--000325558c36f97e8104a20a4619--

Re: better way of writing this script

am 29.04.2011 11:15:00 von Rob Dixon

On 29/04/2011 09:15, Brian Fraser wrote:
> On Fri, Apr 29, 2011 at 5:02 AM, Uri Guttman wrote:
>>
>> so you can streamline yours with a slice:
>>
>> my $status = join '', grep defined, @jvalue{ 0 .. 5 } ;
>
> This one was on purpose though - The slice might insert new keys into the
> hash, and I recently spent the good half of an hour debugging just that..!

Good call Brian. It's not at all obvious that all the elements of a hash
slice will be created if they don't exist :)

Rob

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

Re: better way of writing this script

am 29.04.2011 11:27:53 von Uri Guttman

>>>>> "RD" == Rob Dixon writes:

RD> On 29/04/2011 09:15, Brian Fraser wrote:
>> On Fri, Apr 29, 2011 at 5:02 AM, Uri Guttman wrote:
>>>
>>> so you can streamline yours with a slice:
>>>
>>> my $status = join '', grep defined, @jvalue{ 0 .. 5 } ;
>>
>> This one was on purpose though - The slice might insert new keys into the
>> hash, and I recently spent the good half of an hour debugging just that..!

RD> Good call Brian. It's not at all obvious that all the elements of a hash
RD> slice will be created if they don't exist :)

and they won't be anyhow. you need have lvalues to autovivify hash (or
array) elements.

perl -le '@x = @y{ qw( a b )}; print keys %y'

%y is empty as you can see.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

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

Re: better way of writing this script

am 29.04.2011 11:33:37 von Rob Dixon

On 29/04/2011 10:27, Uri Guttman wrote:
>>>>>> "RD" == Rob Dixon writes:
>
> RD> On 29/04/2011 09:15, Brian Fraser wrote:
> >> On Fri, Apr 29, 2011 at 5:02 AM, Uri Guttman wrote:
> >>>
> >>> so you can streamline yours with a slice:
> >>>
> >>> my $status = join '', grep defined, @jvalue{ 0 .. 5 } ;
> >>
> >> This one was on purpose though - The slice might insert new keys into the
> >> hash, and I recently spent the good half of an hour debugging just that..!
>
> RD> Good call Brian. It's not at all obvious that all the elements of a hash
> RD> slice will be created if they don't exist :)
>
> and they won't be anyhow. you need have lvalues to autovivify hash (or
> array) elements.
>
> perl -le '@x = @y{ qw( a b )}; print keys %y'
>
> %y is empty as you can see.

I meant in the specific case of the grep that was posted. There are no
lvalues there, yet they are autovivified:

perl -le '@x = grep defined, @y{ qw( a b )}; print keys %y'

Rob

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

Re: better way of writing this script

am 29.04.2011 11:39:52 von Uri Guttman

>>>>> "RD" == Rob Dixon writes:

RD> On 29/04/2011 10:27, Uri Guttman wrote:

RD> Good call Brian. It's not at all obvious that all the elements of a hash
RD> slice will be created if they don't exist :)
>>
>> and they won't be anyhow. you need have lvalues to autovivify hash (or
>> array) elements.
>>
>> perl -le '@x = @y{ qw( a b )}; print keys %y'
>>
>> %y is empty as you can see.

RD> I meant in the specific case of the grep that was posted. There are no
RD> lvalues there, yet they are autovivified:

RD> perl -le '@x = grep defined, @y{ qw( a b )}; print keys %y'

that shouldn't happen IMO. it is only calling defined on the aliased
values of %y. i would call it a bug but some could argue otherwise.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

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

Re: better way of writing this script

am 29.04.2011 14:17:45 von derykus

On Apr 29, 2:39=A0am, u...@StemSystems.com ("Uri Guttman") wrote:
> >>>>> "RD" == Rob Dixon writes:
>
> =A0 RD> On 29/04/2011 10:27, Uri Guttman wrote:
>
> =A0 RD> Good call Brian. It's not at all obvious that all the elements of=
a hash
> =A0 RD> slice will be created if they don't exist :)
> =A0 >>
> =A0 >> and they won't be anyhow. you need have lvalues to autovivify hash=
(or
> =A0 >> array) elements.
> =A0 >>
> =A0 >> perl -le '@x =3D @y{ qw( a b )}; print keys %y'
> =A0 >>
> =A0 >> %y is empty as you can see.
>
> =A0 RD> I meant in the specific case of the grep that was posted. There a=
re no
> =A0 RD> lvalues there, yet they are autovivified:
>
> =A0 RD> =A0 perl -le '@x =3D grep defined, @y{ qw( a b )}; print keys %y'
>
> that shouldn't happen IMO. it is only calling defined on the aliased
> values of %y. i would call it a bug but some could argue otherwise.
>

This happened in 5.10 ... at least after 5.8.

From: http://rt.perl.org/rt3/Ticket/Display.html?id=3D89024

Just like the argument list of sub calls, The list over which
foreach
iterates is evaluated in lvalue context as required by foreach's
aliasing property.

Ex:
perl -MData::Dumper -le '
for ( @y{qw(a b)} ) {$_ =3D "foo" unless defined $_} ;
print Dumper \%y'

$VAR1 =3D {
'a' =3D> 'foo',
'b' =3D> 'foo'
};


grep's aliasing creates the same lvalue context:

perl -MData::Dumper -le '
@x =3D grep {$_ =3D "foo" unless defined $_} @y{ qw( a b )};
print Dumper \%y'

$VAR1 =3D {
'a' =3D> 'foo',
'b' =3D> 'foo'
};

--
Charles DeRykus


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

Re: better way of writing this script

am 29.04.2011 15:04:18 von Rob Dixon

On 29/04/2011 13:17, C.DeRykus wrote:
>
> This happened in 5.10 ... at least after 5.8.
>
> From: http://rt.perl.org/rt3/Ticket/Display.html?id=89024
>
> Just like the argument list of sub calls, The list over which
> foreach
> iterates is evaluated in lvalue context as required by foreach's
> aliasing property.
>
> Ex:
> perl -MData::Dumper -le '
> for ( @y{qw(a b)} ) {$_ = "foo" unless defined $_} ;
> print Dumper \%y'
>
> $VAR1 = {
> 'a' => 'foo',
> 'b' => 'foo'
> };
>
>
> grep's aliasing creates the same lvalue context:
>
> perl -MData::Dumper -le '
> @x = grep {$_ = "foo" unless defined $_} @y{ qw( a b )};
> print Dumper \%y'
>
> $VAR1 = {
> 'a' => 'foo',
> 'b' => 'foo'
> };
>

I think that is intuitive for a for loop. I would expect

for (@hash{qw/a b c/}) {
:
}

to execute three times, regardless of the state of the hash. But for

my @list = @hash{qw/a b c/};

to perform so very differently from

my @list = grep 1, @hash{qw/a b c/};

is a surprise.

I can anticipate reasons for leaving things this way, one of which is
that there must be code out there that modifies $_ in the grep block,
but it is certainly a shame and a major pitfall.

Rob

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