better way of writing this script

Hi All

is there a better way of writing this script


my [at] resultss = qw( 0 1 2 3 4 5 ) ;

foreach ( [at] resultss) {
if ( defined( $jvalue{$_}){
$status .= $jvalue{$_} ;
}
}


Thanks

--
Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Agnello George [ Fr, 29 April 2011 09:27 ] [ ID #2058941 ]

Re: better way of writing this script

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

On Fri, Apr 29, 2011 at 4:27 AM, Agnello George <agnello.dsouza [at] gmail.com>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--
Brian Fraser [ Fr, 29 April 2011 09:38 ] [ ID #2058942 ]

Re: better way of writing this script

>>>>> "AG" == Agnello George <agnello.dsouza [at] gmail.com> 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 [at] resultss = qw( 0 1 2 3 4 5 ) ;

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


AG> foreach ( [at] resultss) {

foreach ( 0 .. 5 ) {

or if you want all of [at] 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, [at] jvalue ) ;

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

uri

--
Uri Guttman ------ uri [at] 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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Fr, 29 April 2011 09:39 ] [ ID #2058943 ]

Re: better way of writing this script

On Fri, Apr 29, 2011 at 1:09 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
>>>>>> "AG" =3D=3D Agnello George <agnello.dsouza [at] gmail.com> 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 [at] resultss =3D qw( 0 1 2 3 4 5 ) ;
>
> is that always a fixed list? could [at] jvalue have more then 6 entries? if
> you want all of [at] jvalue you don't need [at] resultss at all.
>
>
> =A0AG> foreach ( [at] resultss) {
>
> =A0 =A0 =A0 =A0foreach ( 0 .. 5 ) {
>
> or if you want all of [at] 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, [at] jvalue ) ;
>
> that is the whole thing in one fairly clear line of code. perl is very
> nice this way.
>
> uri
>
> --
> Uri Guttman =A0------ =A0uri [at] 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

--
Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Agnello George [ Fr, 29 April 2011 09:54 ] [ ID #2058944 ]

Re: better way of writing this script

>>>>> "BF" == Brian Fraser <fraserbn [at] gmail.com> writes:

BF> On Fri, Apr 29, 2011 at 4:27 AM, Agnello George <agnello.dsouza [at] gmail.com>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, [at] 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 [at] 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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Fr, 29 April 2011 10:02 ] [ ID #2058945 ]

Re: better way of writing this script

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

On Fri, Apr 29, 2011 at 5:02 AM, Uri Guttman <uri [at] stemsystems.com> 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, [at] 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--
Brian Fraser [ Fr, 29 April 2011 10:15 ] [ ID #2058946 ]

Re: better way of writing this script

On 29/04/2011 09:15, Brian Fraser wrote:
> On Fri, Apr 29, 2011 at 5:02 AM, Uri Guttman<uri [at] stemsystems.com> wrote:
>>
>> so you can streamline yours with a slice:
>>
>> my $status = join '', grep defined, [at] 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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Fr, 29 April 2011 11:15 ] [ ID #2058951 ]

Re: better way of writing this script

>>>>> "RD" == Rob Dixon <rob.dixon [at] gmx.com> writes:

RD> On 29/04/2011 09:15, Brian Fraser wrote:
>> On Fri, Apr 29, 2011 at 5:02 AM, Uri Guttman<uri [at] stemsystems.com> wrote:
>>>
>>> so you can streamline yours with a slice:
>>>
>>> my $status = join '', grep defined, [at] 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 ' [at] x = [at] y{ qw( a b )}; print keys %y'

%y is empty as you can see.

uri

--
Uri Guttman ------ uri [at] 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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Fr, 29 April 2011 11:27 ] [ ID #2058956 ]

Re: better way of writing this script

On 29/04/2011 10:27, Uri Guttman wrote:
>>>>>> "RD" == Rob Dixon<rob.dixon [at] gmx.com> writes:
>
> RD> On 29/04/2011 09:15, Brian Fraser wrote:
> >> On Fri, Apr 29, 2011 at 5:02 AM, Uri Guttman<uri [at] stemsystems.com> wrote:
> >>>
> >>> so you can streamline yours with a slice:
> >>>
> >>> my $status = join '', grep defined, [at] 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 ' [at] x = [at] 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 ' [at] x = grep defined, [at] y{ qw( a b )}; print keys %y'

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Fr, 29 April 2011 11:33 ] [ ID #2058957 ]

Re: better way of writing this script

>>>>> "RD" == Rob Dixon <rob.dixon [at] gmx.com> 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 ' [at] x = [at] 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 ' [at] x = grep defined, [at] 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 [at] 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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Fr, 29 April 2011 11:39 ] [ ID #2058958 ]

Re: better way of writing this script

On Apr 29, 2:39=A0am, u... [at] StemSystems.com ("Uri Guttman") wrote:
> >>>>> "RD" =3D=3D Rob Dixon <rob.di... [at] gmx.com> 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 ' [at] x =3D [at] 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 ' [at] x =3D grep defined, [at] 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 ( [at] 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 '
[at] x =3D grep {$_ =3D "foo" unless defined $_} [at] y{ qw( a b )};
print Dumper \%y'

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

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
derykus [ Fr, 29 April 2011 14:17 ] [ ID #2058963 ]

Re: better way of writing this script

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 ( [at] 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 '
> [at] x = grep {$_ = "foo" unless defined $_} [at] 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 ( [at] hash{qw/a b c/}) {
:
}

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

my [at] list = [at] hash{qw/a b c/};

to perform so very differently from

my [at] list = grep 1, [at] 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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Fr, 29 April 2011 15:04 ] [ ID #2058964 ]
Perl » gmane.comp.lang.perl.beginners » better way of writing this script

Vorheriges Thema: Efficient way to compare 2 data structures particularly hash
Nächstes Thema: calling a config file that has a hash within multiple perl scripts