THANKS,
i was not aware of that,
so, is it possible to use strict, and declare variables in a one liner
outside of BEGIN ?
(i now realize of course that the my is local to the block)
where should my%t=(); be ? (it isn't in INIT)
without the strict , it works
head -2 U__E__103.tsv | perl -MData::Dumper -wple
'INIT{my%t=();};BEGIN{my @months=qw(jan feb mar apr may jun jul aug
sep oct nov dec);@t{map uc,@months}=1..12;} ;
s/(?<=\d\d-)(...)(?=-\d\d)/$t{"$1"}/e;print$1;'
also of course i can remove the init block
and it still works (with %t as a global probably)
-- vish
On 27 July 2011 14:19, Gaal Yahas <[email protected]> wrote:
> Since you put "use strict" inside the BEGIN block, it didn't tell you that
> %t is not defined in the first oneliner outside that block. If you did perl
> -Mstrict -MData::Dumper ...., you'd get a better error message.
> How come the second example is any different? Well, it shouldn't be, but
> "$a" happens to be a magical name. Don't believe me? Try the same code with
> $baz; it'll fail.
> $a and $b are special package vars that Perl allows you to use without
> declaration even when use strict is in place. The reason they exist is that
> you can write comparators succinctly (e.g., sort { $b <=> $a } @list).
> On Wed, Jul 27, 2011 at 3:59 PM, Avishalom Shalit <[email protected]>
> wrote:
>>
>> head file | perl -MData::Dumper -wple 'BEGIN{use strict;my %t=(); my
>> @months=qw(jan feb mar apr may jun jul aug sep oct nov dec);@t{map
>> uc,@months}=1..12;$a=\%t;} ; s/(?<=\d\d-)(...)(?=-\d\d)/$t{"$1"}/;'
>>
>> fails (specifically JUN is replaced with an empty string)
>>
>> $ head -2 U__E__103.tsv | perl -MData::Dumper -wple 'BEGIN{use
>> strict;my %t=(); my @months=qw(jan feb mar apr may jun jul aug sep oct
>> nov dec);@t{map uc,@months}=1..12;$a=\%t;} ;
>> s/(?<=\d\d-)(...)(?=-\d\d)/$a->{"$1"}/;'
>>
>> works
>>
>>
>> -- vish
>>
>>
>>
>>
>>
>> On 27 July 2011 12:55, Gaal Yahas <[email protected]> wrote:
>> > Please paste the command you're trying. Hash subscript works for me.
>> > $ perl -le '%t = (a => "b", c => "d"); print $t{a}'
>> > b
>> > On Wed, Jul 27, 2011 at 2:05 PM, Avishalom Shalit <[email protected]>
>> > wrote:
>> >>
>> >> to reiterate the problem
>> >>
>> >> $t{something}
>> >> is interpreted as ($t) ({something})
>> >>
>> >> in a one liner
>> >>
>> >> (not in a script)
>> >> -- vish
>> >>
>> >
>> > --
>> > Gaal Yahas <[email protected]>
>> > http://gaal.livejournal.com/
>> >
>> > _______________________________________________
>> > Perl mailing list
>> > [email protected]
>> > http://mail.perl.org.il/mailman/listinfo/perl
>> >
>> _______________________________________________
>> Perl mailing list
>> [email protected]
>> http://mail.perl.org.il/mailman/listinfo/perl
>
>
> --
> Gaal Yahas <[email protected]>
> http://gaal.livejournal.com/
>
> _______________________________________________
> Perl mailing list
> [email protected]
> http://mail.perl.org.il/mailman/listinfo/perl
>
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl