art-ificial ramblings...

Friday, March 18, 2005

Perls Are A Girl's Best Friends

I believe I am fairly artistic and I tend to spend majority of time in that area. But once in a while, the geek in me does surface. With our company migrating to Oracle, comes the opportunity to learning new tools, the obvious being Oracle/PLSQL. But in addition, using Oracle with Perl. I've bagged on Perl enough. It's about time I buckle down and actually learn Perl.

I came across a Perl puzzle that stumped me yesterday.

So what does this print?

@a = ();
$h{'a'} = 'b';
push @a, %h;
print "@a";

The newbie in me thought it would print b.

WRONG...


so I evaluated each line:

1) @a = (); # Creates an empty array

2) $h{'a'} = 'b'; # huh?

3) push @a, %h; # pushing hash %h into array @a

4) print "@a"; # print array @a


Well, being really new to perl, I had no clue what was $h{'a'} = 'b';

I am assuming, you are adding value 'b' to the anonymous hash that is already initialized with key 'a'.

So $h{'a'} = 'b' is a scalar reference to hash %h and key 'a' and you are assigning 'b' to it??

3) pushes hash @h to array @a

4) print @a will output: a b


I believe the answer is right, but my reasoning could be wrong?

Wow... learning a new programming language is fun. Problem is I have to stop applying the same rule in another language to a new language. #2 stumped me because I'm so used to working with languages that make you declare variable before you use it. Also, I'm not used to notations such as %$@ to designate a variable type.

7 Comments:

  • I have used PHP a little but have had little desire to pick up on PHP or PERL more.

    Between all the dollar signs in php and what not it just looks so ugly and I don't read it well. I know if I just spent, even an afternoon with it I would overcome that initial WTF but I have yet to do a project for fun or work that has required that

    at 1:57 PM  

  • Um, yeah.... you know what that looked like to me?

    3283jklahgrkajhtiqyu38qyhjhgkahge

    You should send your dad that post and see if he can tell what it means, since he apparently knows all:)

    at 2:45 PM  

  • Omigod I'm so confused.
    I'm off to drool in a bucket now. Doh!

    at 2:47 PM  

  • When you've punished yourself suficiently in Perl, you can always tackle AWK, another scripting language. It's good for parsing data, etc..

    LOL... have fun :)

    DB

    at 4:01 PM  

  • Wow.... (o.o) !

    - Steve :]

    at 1:12 PM  

  • Perl and awk scripts (parsed medical data daily) were fun to do. I never understood C though.

    ...but that was so long ago for me. I am not programming savvy anymore.

    Good luck to you!

    at 4:40 PM  

  • Aymiee. You are such a geek...and I mean that in a good way =)

    Actually...I have only had to fix some perl scripts. I was offended by Perl's accumulation of syntax oddities, so I have gotten by on shell scripting with awk, sed, and grep when I needed it. Who was it that chose the period for string catenation anyways? Still I have friends that are big fans.

    BUT, with that said, and without actually running the code, my guess would be the same as yours.

    I've been doing a TON of PL/SQL programming lately. It's a far cry from Java and C++, but I still get to fit some of that in occasionally as well. You'll have to let me know how your Oracle integration is coming along, because ours is quite humorous (and not in a good way).

    Drop me a line sometime...you know where to find me. =)

    at 2:54 PM  

Post a Comment

<< Home