Mar 10 21:03:39 * Poseiden_afk is now known as Poseiden Mar 10 21:04:22 HI! Mar 10 21:04:25 hai Mar 10 21:04:28 I DID MY HOMEWORK! Mar 10 21:04:32 hai hai Mar 10 21:04:39 woohoo! Are you the mistress of hashes? Mar 10 21:04:51 No. Mar 10 21:04:54 almost Mar 10 21:04:55 I'm like, to moron of them Mar 10 21:05:00 But I kinda get them Mar 10 21:05:06 eggsalad Mar 10 21:05:11 and I kinda get confused as a baby in a topless bar with them Mar 10 21:05:19 they're like arrays but with more flexible indexes :) Mar 10 21:06:01 JulieAnn: Okay... let's get started. Mar 10 21:06:08 okay :) Mar 10 21:06:18 I printed the whole book. What page do you want me on first? Mar 10 21:06:34 JulieAnn: Book schmook. Books are for studying, not for classwork. Mar 10 21:06:40 okees. Mar 10 21:06:40 heh Mar 10 21:06:42 m'kay... hashes: Mar 10 21:06:54 * JulieAnn took notes and everything... *puts stuff away from her* Mar 10 21:06:57 #### You understand arrays? Arrays are lists, that are numerically indexed. Mar 10 21:07:39 I understand arrays. Mar 10 21:07:44 The first ones we did Mar 10 21:07:50 Good good. Mar 10 21:07:51 the slices I'm ok with I think Mar 10 21:07:59 orange slice! Mar 10 21:08:05 Okay! Excellent. Mar 10 21:08:09 it took henry a while to teach me pop/push and shift/unshift Mar 10 21:08:22 (Thankie, Henry) Mar 10 21:08:23 and nivek had a hell of a time getting me to comprehend the hash stuff. Mar 10 21:08:28 welcome Mar 10 21:08:38 it took a whopping 15 minutes for her to get it :P Mar 10 21:08:40 What's a nivek? Mar 10 21:08:53 Getting back to the hash... Mar 10 21:08:54 nivek/klb/lapjack Mar 10 21:08:55 es. Mar 10 21:09:13 Arrays are lists that are numerically indexed. Mar 10 21:09:29 Hashes are the same thing. Mar 10 21:09:32 Lists. Mar 10 21:09:46 but they can be indexed numerically, or with words Mar 10 21:09:46 The only difference is that hashes are indexed with strings, instead of with numbers. Mar 10 21:10:26 Actually, hashes are _always_ indexed with strings. They can be strings of numbers, but they are still strings. In other words, they are always the stuff that gets put in quotes. Mar 10 21:11:09 ok Mar 10 21:11:31 so...... $hash{`string'} Mar 10 21:13:50 * Disconnected (). **** ENDING LOGGING AT Sat Mar 10 21:13:50 2007 **** BEGIN LOGGING AT Sat Mar 10 21:15:27 2007 Mar 10 21:15:27 * Now talking on #perltutor Mar 10 21:15:32 damndamndamndamndamndamndamndamnndamndamn Mar 10 21:15:34 ok Mar 10 21:15:37 wb Mar 10 21:15:41 The only difference is that hashes are indexed with strings, instead of with numbers. Mar 10 21:15:41 Actually, hashes are _always_ indexed with strings. They can be strings of numbers, but they are still strings. In other words, they are always the stuff that gets put in quotes. Mar 10 21:15:41 ok Mar 10 21:15:41 so...... $hash{`string'} Mar 10 21:15:48 that's the last I had on my screen. Mar 10 21:15:52 ty henry Mar 10 21:16:14 [17:09] (so, for instance, $myhash{2 + 4} _technically_ doesn't make sense. Mar 10 21:16:14 [17:09] ) Mar 10 21:16:14 [17:10] (I say _technically_, because Perl will actually make sense of it and do something pretty close to what you expect. But _technically_ it is not Mar 10 21:16:14 a numerical index. Mar 10 21:16:14 [17:10] ) Mar 10 21:16:15 [17:11] So.. has notation. As you probably already know, hashes are referenced with a '%'. Mar 10 21:16:17 [17:12] But the hash _elements_ are still scalars (just like with arrays,) so you reference them with a '$'. Mar 10 21:16:22 [17:12] To explicitly declare a hash, you use parens just like with an array... but there are two notations for doing so. Mar 10 21:16:32 wb Mar 10 21:17:10 catching up Mar 10 21:17:13 let me reread it again Mar 10 21:17:22 JulieAnn: be careful what direction your quotes face. They must match. 'string' makes sense. `string` makes sense (for a totally different reason). `string' does not make sense in Perl. Mar 10 21:18:33 I used the same quote both times.. :/ Mar 10 21:18:36 xchat is doing pretty quotes for her Mar 10 21:18:36 '' Mar 10 21:18:43 kfoo: Ah, thanks. Mar 10 21:18:44 'string' Mar 10 21:18:53 JulieAnn: Ignore me. It's your irc client. Mar 10 21:18:59 oh, okees Mar 10 21:19:45 /bin/sh: figlet: command not found Mar 10 21:19:59 /bin/sh: -c: line 0: unexpected EOF while looking for matching `"' Mar 10 21:20:00 /bin/sh: -c: line 1: syntax error: unexpected end of file Mar 10 21:20:08 hrm. Mar 10 21:20:10 figlet rocks Mar 10 21:20:27 kfoo: Except that it's breaking. Mar 10 21:21:29 kfoo: Hrm. Apparently I uninstalled it. Oops. Mar 10 21:21:47 /bin/sh: -c: line 0: unexpected EOF while looking for matching `"' Mar 10 21:21:47 /bin/sh: -c: line 1: syntax error: unexpected end of file Mar 10 21:22:14 hrm Mar 10 21:24:55 load .ircrc Mar 10 21:24:57 grr Mar 10 21:25:17 Okay. Whew! Mar 10 21:25:35 No where were we? Ah, yes. Two ways to explicitly initialize hashes. Mar 10 21:26:35 The first notation is the normal list notation: Mar 10 21:26:52 %hash = ("word1", "word2", "word3", "word4"); Mar 10 21:27:22 ok Mar 10 21:27:37 This does _not_ do what it does with an array, however (because it can't). It does not assign each element of the list to an element of the hash. Instead, it treats the first word as a key and the second as a value. The third is a key and the fourth is its value. Mar 10 21:28:10 In other words, this notation does the same thing as: $hash{word1} = "word2"; $hash{word3} = "word4"; Mar 10 21:28:39 (Yes, hashes can be implicitly initiatlized just like arrays) Mar 10 21:29:03 Got it? Mar 10 21:29:16 erm Mar 10 21:29:20 let me read it again Mar 10 21:29:30 (sorry, it helps to reread a couple times) Mar 10 21:29:52 ok.. so... like. Mar 10 21:29:57 if I made it like Mar 10 21:31:12 %hash = ('potato', 'russet', 'candy', 'taffy') Mar 10 21:31:14 * Irinix (~Guest@pool-141-154-12-122.bos.east.verizon.net) has joined #perltutor Mar 10 21:31:17 it'd be Mar 10 21:31:57 making $hash{potato} = russet Mar 10 21:32:11 and #hash{candy} = taffy Mar 10 21:32:12 yes? Mar 10 21:32:14 poseiden, i ended up working a double today so I didn't have time to read the perl basics, and i'm gonna end up missing tonights lesson because I have to work a double tomorrow. (About to go to bed, it's 930) Mar 10 21:32:17 yup. Mar 10 21:32:34 Irinix: Sorry to hear it. You'll get all caught up, though, and the money is worth it! Mar 10 21:32:39 aye. Mar 10 21:32:44 Sorry. :( Mar 10 21:32:59 Irinix: Shit happens. Then you die. That's life in a nutshell. Mar 10 21:34:05 I'm gonna keep my log running though so I'll be able catch up. Mar 10 21:35:57 Irinix: 'kay. There will be some background stuff that we may have to cover, but you'll catch up fine. Mar 10 21:36:15 well, good night. I'll see you tomorrow night at about this time. Thanks for the confidence in my abilities Poseiden. :) Mar 10 21:36:28 Irinix: Good night. Mar 10 21:36:36 g'night Mar 10 21:37:54 my daughter is helping out tonight. there may be some somewhat.... chatic suggestions. like yugygg888 Mar 10 21:38:16 lol Mar 10 21:38:21 understood. Mar 10 21:38:37 also, this confines me to one handed keyboarding. speed will suffer. sorry. Mar 10 21:38:38 Heck, I might understand HER explanations better than yours. Yanno, she's more on my level Mar 10 21:38:39 ;) Mar 10 21:38:45 :P Mar 10 21:40:20 okay, you have the first explicit notation. the second is similar, but more clear. It is "key => value", as in: %hash = ( "word1" => "word2", "word3" => "word4"); Mar 10 21:40:41 Make sense? Mar 10 21:41:23 yeah, I get that. Mar 10 21:41:34 That's how nivek tried explaining it the first time heh Mar 10 21:41:48 Okay. Now your first style point (we'll have plenty more): Always use the second form. Mar 10 21:42:49 The first form, ("key", "value", "key2", "value2"), is hard to figure out. Don't use it. It is a good thing that Perl understands this notation, but that doesn't mean you should ever, ever use it in your own code (as you'll see, its value comes elsewhere). Mar 10 21:43:20 ok. Mar 10 21:43:20 Got it? Mar 10 21:43:22 Got it :) Mar 10 21:43:23 Great. Mar 10 21:43:43 Okay... a bit of cleanup: I forgot to tell you about undef. Mar 10 21:44:03 Scalar variables don't always have values (often because you screw up). For instance, consider the following code: Mar 10 21:44:14 #!/usr/bin/perl Mar 10 21:44:44 if ($dog eq 'retriever') { print "Hiya, Goldie!\n"; } Mar 10 21:44:51 What's the problem there? Mar 10 21:45:10 ummmm Mar 10 21:45:46 I dunno... Mar 10 21:45:48 :/ Mar 10 21:45:56 what is $dog set to? Mar 10 21:46:09 retriever Mar 10 21:46:13 Nope. Mar 10 21:46:18 ? Mar 10 21:46:21 oh Mar 10 21:46:22 wait Mar 10 21:46:25 it's set to Mar 10 21:46:26 There is no assignment there - look again (it's a comparison). Mar 10 21:46:27 Mar 10 21:46:29 or something Mar 10 21:46:37 ok. Mar 10 21:46:41 where is it set to ? Mar 10 21:46:57 It's not.. not just from that one line of code Mar 10 21:47:09 JulieAnn: Right. Mar 10 21:47:16 I was trying to guess Mar 10 21:47:17 :/ Mar 10 21:47:28 It is totally unset. Perl has no idea what $dog might be. Mar 10 21:48:12 shih tzu! Mar 10 21:48:16 possibly. Mar 10 21:48:29 sorry, i thought I was supposed to like, know. Mar 10 21:48:30 (Notice that there are two lines - the shebang, which _must_ be the very first line of your Perl program if it is there at all, and then the branch. Since $dog wasn't set in that space, you can know for certain that $dog is unset at the time of the comparison). Mar 10 21:48:30 :/ Mar 10 21:48:46 ok Mar 10 21:49:12 JulieAnn: np. The point is that $dog is unset. Perl has no idea what its value should be. We are comparing a variable with no declared value to a fixed string. Mar 10 21:49:22 Ok. Mar 10 21:49:44 there is no dog :\ Mar 10 21:50:43 Whenever a variable is created, Perl gives that variable an initial value. The value _looks like_ the empty string ("") when it is compared to a string, and looks like 0 when it is compared to a number. It looks like 'false' when it is compared to a boolean value. But it is not "", 0, or false. Mar 10 21:51:05 Instead, it is undef. That is a Perl keyword, meaning, literally, "having no value". Mar 10 21:51:22 undefined? Mar 10 21:52:13 That's right. It's actually an unfortunate choice, because mathematicians think that "7/0" is "undefined." That word has an intrinsic meaning that is different from the Perl meaning. Mar 10 21:52:49 7/0 is defined on certain pentium chips .. Mar 10 21:53:01 oh, ok... Mar 10 21:53:21 But, anyhow, the moral of the story is that there is a secret neato value of undef. You can test for undef. We'll get into that later. Mar 10 21:53:28 ok Mar 10 21:53:49 Back to hashes. As you've already seen, the index of a hash is in curly braces: {}. Mar 10 21:53:57 yes Mar 10 21:54:07 The index is also called the "key". It is the "key" that corresponds to the value of the hash element. Mar 10 21:55:04 Hashes have some absolutely outstanding properties. One big one is that assignment to $hash{four} (or even $hash{"4"} for that matter) does _not_ imply that there is a $hash{three}. Mar 10 21:55:29 This is different from arrays. Consider: Mar 10 21:55:33 #!/usr/bin/bash Mar 10 21:55:42 $array[4] = 7; Mar 10 21:55:51 $hash["4"] = 7; Mar 10 21:56:12 How many _scalars_ will Perl create (and reserve memory and other overhead for)? Mar 10 21:56:37 3 Mar 10 21:56:39 4 Mar 10 21:56:43 0-3 Mar 10 21:56:52 0, 1, 2, 3 Mar 10 21:57:14 for the array, 0, 1, 2, 4 Mar 10 21:57:17 Right spirit. Remember the two that get created explicitly. Mar 10 21:57:51 (One is an array element, the other is a hash element) Mar 10 21:57:51 so 5? Mar 10 21:58:15 or 8? Mar 10 21:58:26 Six, altogether. $array[0], $array[1], $array[2], $array[3], $array[4], and $hash{"4"}. Mar 10 21:58:29 Because you said just because there is a 4, doesn't mean there's a 3. Mar 10 21:58:56 :/ Mar 10 21:58:57 ok Mar 10 21:59:04 Does that make sense? Mar 10 21:59:35 yes Mar 10 21:59:57 Okay. Mar 10 22:00:01 I wasn't counting $array[4] for some reason Mar 10 22:00:12 Now you'll see why the _first_ form of hash initialization is useful. Mar 10 22:00:40 the one I should never use? Mar 10 22:00:52 Write a program that creates and initializes a hash with 10 key/value pairs. Mar 10 22:01:13 Then have your program 'print %myHash; print "\n";' Mar 10 22:01:17 ok Mar 10 22:01:25 JulieAnn: Right, the one you should never, ever use. Mar 10 22:01:38 (You'll secretly use it all the time, but it'll be under cover of darkness). Mar 10 22:02:03 You want me to do this with the first method, right? Mar 10 22:02:13 No. NEVER use the first method. Mar 10 22:02:16 the forbidden method :P Mar 10 22:02:28 so you want me to use the 2nd method to do it? Mar 10 22:02:33 JulieAnn: That's right. Mar 10 22:02:35 ok. Mar 10 22:07:39 ok Mar 10 22:07:45 Works? Mar 10 22:07:51 it printed them all in one big long string Mar 10 22:08:11 Excellent! Now do the same thing, but with an array instead of a hash. Mar 10 22:08:56 ok. Mar 10 22:10:06 My style is da bomb de bomb de dang di dang diggy diggy Mar 10 22:10:19 Poseiden: ignore me if this is jumping ahead, but what do the numbers represent when you print a hash in scalar context? Mar 10 22:10:52 kfoo: Same thing they do in any list - the length of the list. Mar 10 22:10:58 8/16 ? Mar 10 22:11:10 kfoo: ?? I've not seen that. What perl are you using? Mar 10 22:11:20 5.8.8 Mar 10 22:11:25 it said I have a bad name Mar 10 22:11:26 kfoo: code? Mar 10 22:11:40 JulieAnn: cover your eyes Mar 10 22:11:45 my %hash = qw(one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty); Mar 10 22:11:46 JulieAnn: Well I disagree with that. You have a fine name! Just fine. Tell it to @#@#%@ off. Mar 10 22:11:48 print %hash."\n"; Mar 10 22:12:10 kfoo: HRM! No idea. It should print 20. Mar 10 22:12:24 i figured it out Mar 10 22:12:30 kfoo: I must be wrong about something. I'll look it up. Mar 10 22:13:06 julieann@WhimsicalAgony:~$ ./array.pl Mar 10 22:13:06 Bad name after lemonade' at ./array.pl line 3. Mar 10 22:13:10 it said that cuz I forgot a ' Mar 10 22:15:53 kfoo: HRM! Mar 10 22:16:00 Poseiden: buckets Mar 10 22:16:07 kfoo: curiously, it says the same thing to me. Mar 10 22:16:27 kfoo: shouldn't be buckets. Mar 10 22:16:33 its buckets Mar 10 22:16:44 kfoo: I believe you that it is, but it shouldn't be. Mar 10 22:17:04 kfoo: Let's figure that out. Mar 10 22:17:08 kfoo: later. Mar 10 22:17:19 JulieAnn: Okay, so what did you get for output? Mar 10 22:17:37 julieann@WhimsicalAgony:~$ ./array.pl Mar 10 22:17:37 (peanut phone camel candy lemonade lipgloss purse lighter pencil kfoo) Mar 10 22:17:37 julieann@WhimsicalAgony:~$ Mar 10 22:17:52 just used things that are on my bed heh Mar 10 22:18:03 JulieAnn: code? Mar 10 22:18:17 #!/usr/bin/perl Mar 10 22:18:17 @array = ('peanut', 'phone', 'camel', 'candy', 'lemonade', 'lipgloss', Mar 10 22:18:17 'purse', 'lighter', 'pencil', 'kfoo'); Mar 10 22:18:17 print "(@array)"; Mar 10 22:18:17 print "\n"; Mar 10 22:18:38 JulieAnn: kay. Mar 10 22:18:50 JulieAnn: Code for your hash proggie? Mar 10 22:19:12 #!/usr/bin/perl Mar 10 22:19:12 %hash = ('pepper' => 'cat', 'fish' => 'guppy', 'nut' => 'cashew', Mar 10 22:19:12 'teacher' => 'Poseiden', 'splendiferous' => 'kfoo', 'asshole' => 'nick', Mar 10 22:19:12 'student' => 'julieann', 'cigarettes' => 'trash', 'soda' => 'dietcoke', Mar 10 22:19:12 'food' => 'ribs'); Mar 10 22:19:12 print %hash; Mar 10 22:19:12 print "\n"; Mar 10 22:20:26 JulieAnn: Alright. Make the print statements comparable - either put both in quotes or neither. Mar 10 22:20:41 oh. Mar 10 22:20:45 I'm sorry :( Mar 10 22:21:28 JulieAnn: np. Actually, don't put them in quotes. List interpolation in quotes stinks. Mar 10 22:22:16 julieann@WhimsicalAgony:~$ ./hash.pl Mar 10 22:22:16 assholenicksplendiferouskfoocigarettestrashpeppercatsodadietcokefishguppyfoodribsnutcashewstudentjulieannteacherPoseidenSCALAR(0x8149768)julieann@WhimsicalAgony:~$ Mar 10 22:22:19 that's the output Mar 10 22:23:11 Okay. Two important things to notice. Mar 10 22:24:05 One - everything prints in key/value pairs. Read it: "assholenick" corresponds to 'asshole' => 'nick'. "splendiforouskfoo" == "splendiforous" => "kfoo". Etc. Mar 10 22:24:19 yar Mar 10 22:24:22 heh Mar 10 22:24:25 Two - it is in a totally different order than the order in which you did the assignments. Mar 10 22:24:46 brb Mar 10 22:25:39 right Mar 10 22:28:23 Okay, back. Mar 10 22:28:28 wb :D Mar 10 22:28:28 left Mar 10 22:28:43 Now there is one more thing to note in addition. Mar 10 22:29:42 ok Mar 10 22:29:44 That is - it looks like the same glop of @#$ that drops out when you print arrays. Mar 10 22:30:00 mmhmm Mar 10 22:31:24 the moral of that particular story is that in list mode, a hash is just a list. And it is a list, in no particular order, of strings that are paired key value key value key value ... Mar 10 22:32:02 unless its a tied hash.. Mar 10 22:32:07 This makes it easy to assign hashes to other hashes (ta-da! The magic of the forbidden explicit initialization). Mar 10 22:32:12 kfoo: grrrrr Mar 10 22:32:24 ignore that :D Mar 10 22:32:41 Think about: %hash2 = %hash1; Mar 10 22:33:04 ok Mar 10 22:33:07 When %hash1 dumps out, it'll be a list. Mar 10 22:33:28 Because of the magic forbidden method, that list will assign to %hash2. Mar 10 22:33:49 ok Mar 10 22:33:55 This will make every key in %hash1 into a key in %hash2, and will make every value in %hash2 correspond to the key that has the same value in %hash1. Mar 10 22:34:16 See? Works just you might expect. Mar 10 22:34:35 Also, this property of hashes allows you to switch between hashes and arrays at will: Mar 10 22:34:38 :) Mar 10 22:34:47 %hash = @array; will do something useful. Mar 10 22:35:16 thats good for feeding in arguments to functions.. Mar 10 22:35:21 It also means you can do things that might look useless, but are actually incredibly powerful: Mar 10 22:35:43 %hash2 = reverse %hash1;, for instance. Very valuable. Mar 10 22:36:16 hm yeah. i never realized that Mar 10 22:36:20 Why is that so valuable? Because it is easy (and fast) to count the number of elements in a hash, and each hash key can correspond to only one hash value. Mar 10 22:36:48 So %hash2 = reverse %hash1 gives you an almost instant way to count the number of unique values in %hash1. Mar 10 22:37:09 (Think it through - take it apart slowly - and this will make sense to you). Mar 10 22:37:56 ah Mar 10 22:37:59 * kfoo just learned something Mar 10 22:38:51 JulieAnn: Making any sense? Mar 10 22:38:56 Yes. Mar 10 22:39:01 JulieAnn: Cool! Mar 10 22:39:09 you understand why it will give you only unique values? Mar 10 22:39:15 hold on Mar 10 22:39:18 it's making sense. Mar 10 22:39:23 I didn't say it MADE sense Mar 10 22:39:25 yet Mar 10 22:39:38 :) Fair enough. Mar 10 22:39:43 henry... I need oral. Mar 10 22:39:45 Dinner soon. Mar 10 22:39:48 verbal Mar 10 22:39:49 not oral Mar 10 22:39:52 shit that sounded bad Mar 10 22:39:57 * kfoo blinks Mar 10 22:40:00 "Henry, I need oral." Oh to be young. Mar 10 22:40:10 no.. I meant oral dictation Mar 10 22:40:14 verbal explanation Mar 10 22:40:18 Don't we all? Mar 10 22:40:26 is it bad that that helps me sometimes? :/ Mar 10 22:40:42 Nope! That's good. Or, more accurately, it is good that you know that that helps. Mar 10 22:40:55 i read all of this stuff outloud Mar 10 22:40:59 henry heard me earlier heh Mar 10 22:41:14 Nick;s mom is like "What are you talking to in there?" I said "A piece of paper :/" Mar 10 22:41:17 * Poseiden often wishes he knew what would help him grasp those things that are obscure to him now. Like the female psyche. Mar 10 22:41:43 you mean obscure, or impossible? :) Mar 10 22:41:52 :/ Mar 10 22:41:57 kfoo: shit. I hoped it was merely obscure. Mar 10 22:42:01 Maybe I'm just not a female. Mar 10 22:42:10 yes, you are Mar 10 22:42:29 I'm not hard to understand though Mar 10 22:42:34 I just don't understand things Mar 10 22:42:55 Okay, dinner is served. You two figure out the reverse hash thing? And we'll get into hash functions after supper? Mar 10 22:43:20 sounds good. Mar 10 22:43:26 cheers. bbl. Mar 10 22:43:28 Henry's connection just died. He'll be back in a few Mar 10 22:49:38 bleh Mar 10 22:59:32 bleh blah blooey Mar 10 23:00:05 Okay. Good dinner. Understand reverse hash? Mar 10 23:00:16 think so, making her write an example program Mar 10 23:00:18 If not, no biggie - we'll move into hash functions. Mar 10 23:00:41 Cool. JulieAnn, keep working on that. I'm going to go into hash functions a bit, and you can scroll it and read at your leisure. Mar 10 23:00:54 4pink1red3blue2blueSCALAR(0x81496fc)pink4blue3red1SCALAR(0x8149708)julieann@WhimsicalAgony:~$ Mar 10 23:01:06 There are only three important functions that are specific to hashes that I care about at the moment. Mar 10 23:01:23 #!/usr/bin/perl Mar 10 23:01:23 %hash = ("1" => "red", "2" => "blue", "3" => "blue", "4" => "pink"); Mar 10 23:01:23 print %hash; Mar 10 23:01:23 print \n; Mar 10 23:01:23 %hash2 = reverse %hash; Mar 10 23:01:23 print %hash2; Mar 10 23:01:23 print \n; Mar 10 23:03:40 julieann@WhimsicalAgony:~$ ./print.pl Mar 10 23:03:40 4pink1red3blue2blue Mar 10 23:03:40 pink4blue3red1 Mar 10 23:03:40 julieann@WhimsicalAgony:~$ Mar 10 23:03:42 better Mar 10 23:04:10 Cool. Looks like you've got it. Mar 10 23:04:13 :D Mar 10 23:04:15 so it looks like the 2nd value and so on gets dropped Mar 10 23:04:25 kfoo: You can't actually depend on that. Mar 10 23:04:35 kfoo: Some value will get dropped. Not clear which one. Mar 10 23:04:38 yeah Mar 10 23:05:12 Alright, our three functions. The first is keys(). keys() just lists all of the keys of the hash. Mar 10 23:06:10 values() lists the values! Mar 10 23:06:23 Try it: perl -e '%hash={"hello"=>"world","my heart"=>"San Francisco"); print keys %hash; print "\n";' Mar 10 23:06:28 JulieAnn: Two down. :) Mar 10 23:06:41 Poseiden, it took nivek an hour to explain that. :( Mar 10 23:06:44 when do we get to references? heh Mar 10 23:06:48 The third one is 'delete'. Mar 10 23:06:50 because i wasnt grasping what was what Mar 10 23:06:52 kfoo: not soon. :P Mar 10 23:07:08 Well you've clearly got it now. :) Mar 10 23:07:39 :D Mar 10 23:07:49 Delete removes an element from the hash. $hash{foo}="bar"; delete $hash{foo};. Mar 10 23:08:00 that's simple enough Mar 10 23:08:27 There is one more hash-specific function. It is actually unnecessary, but extremely efficient. Mar 10 23:08:47 The each() function returns the next key/value pair in the hash. Mar 10 23:09:07 So, for instance: Mar 10 23:09:49 while ( ($key,$value) = each(%hash) ) { print "The value for key " . $key ." is ". $value .".\n"; } Mar 10 23:10:22 Do you understand that loop? Mar 10 23:10:28 I think Mar 10 23:10:38 Okay. It isn't hugely important. Mar 10 23:10:43 It's just fast. Mar 10 23:10:49 it'll return the value of each key in a list Mar 10 23:11:45 Here's the thing - when iterating over a hash do _NOT_ change the hash. Mar 10 23:12:07 It will fuck with each()'s head. Mar 10 23:12:11 heheh Mar 10 23:12:27 erm Mar 10 23:12:28 huh? Mar 10 23:12:45 oh Mar 10 23:12:51 henry just splaned it Mar 10 23:12:55 So, for instance: while ( ($key,$value) = each(%hash) ) { print "The value for key " . $key . " is ". $value .".\n"; $hash{pink} = "blue!"; } Mar 10 23:13:20 ^^^^^^^^^^^^^^^ Mar 10 23:13:26 That will cause problems. Mar 10 23:14:00 Okay, cool. Mar 10 23:14:53 Alright, now there is one more concept for hashes. It is not hugely important, and the notation is confusing. I will include it only because it corresponds nicely with our discussion of arrays. If you tell me you do not care about hash slices, however, I will just leave them out and move out. Mar 10 23:15:14 I don't understand ANY slices. Mar 10 23:15:18 hash or otherwise Mar 10 23:15:19 yes you do Mar 10 23:15:20 I'd like to though Mar 10 23:15:23 Okay. Mar 10 23:15:33 Alright, we'll start with anonymous list slices. Mar 10 23:15:34 or maybe I do. Mar 10 23:15:40 Those are simple. Mar 10 23:15:42 review it anyway. Mar 10 23:16:05 If you put something in parens and assign it to something else in parens, it will do about what you expect. So, for instance: Mar 10 23:16:36 ($a, $b, $c) = ("hello", "world", "you're so BEAUTIFUL"); Mar 10 23:16:48 will do the same thing as: Mar 10 23:17:04 $a = "hello"; $b = "world"; $c = "you're so BEAUTIFUL"; Mar 10 23:17:06 $a = hello, $b = world, c = you're so BEAUTIFUL Mar 10 23:17:12 er, yeah Mar 10 23:17:16 perfect! Mar 10 23:17:34 brb. Mar 10 23:18:21 "you\'re so BEAUTIFUL" would be the way to make it work right though, right? Mar 10 23:22:07 umm... yeah. Mar 10 23:22:42 You're right - have to escape the single quote. Mar 10 23:22:51 :D Mar 10 23:24:12 Now this trick works in any sort of list assignment. In fact, this is the mechanism you use for explicit initialization of arrays and hashes (although you would never use this for hashes). Mar 10 23:24:46 Ie - @array = ("hello","world","your fish is so BEAUTIFUL"); Mar 10 23:25:04 Make sense? Mar 10 23:25:11 yar Mar 10 23:25:38 m'kay. Now let's mix things up a bit. Let's make an array with lots of elements: Mar 10 23:25:57 @array = qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20); Mar 10 23:26:22 (as you may have guessed by now (or been told), "qw" quotes words in a list.) Mar 10 23:26:53 Now say you want to refer to the elements with values 8 - 15. Mar 10 23:26:57 OK Mar 10 23:27:07 Perl has some tasty mechanisms to do that. Mar 10 23:27:27 orange slices! Mar 10 23:27:30 heh Mar 10 23:27:44 Basically, you just refer to the range of array indices, instead of a single specific index. Mar 10 23:28:37 So, for instance, $array[7,8,9,10,11,12,13,14]. Mar 10 23:28:52 In a Perl statement: Mar 10 23:29:10 $array[7,8,9] = ("hello","world","I like fish"); Mar 10 23:30:48 ok Mar 10 23:32:03 hold on... I have 5 kids here, and it's a bit of a zoo. brb. Mar 10 23:32:08 kk Mar 10 23:32:19 * JulieAnn goes to the monkey area Mar 10 23:38:55 Okay. Back for a few minutes. Mar 10 23:39:01 (it'll get better soon. I hope.) Mar 10 23:39:11 heh Mar 10 23:39:18 i woke up to the sound of stampeding kids this morning Mar 10 23:39:31 kfoo: Always a special privilege. Mar 10 23:39:33 heh Mar 10 23:39:53 You can have array slices on either side of an assignment (or anywhere else, for that matter): Mar 10 23:39:54 not at 8 am on a saturday morning Mar 10 23:40:30 @array1[0,7,9] = @array2[2,5,3]; Mar 10 23:41:03 scrolling up.... Yup. I screwed you up, up above - I've got '$' where there should be '@'. Mar 10 23:41:19 i fixed it in my thingie Mar 10 23:41:36 JulieAnn: Whew! Sorry about that. Brainfart. Mar 10 23:41:53 Okay, smallest one is done with bottle. brb. Mar 10 23:41:58 kk Mar 10 23:45:50 Whew! Okay, it should be a little bit more sensible in here for a few minutes. Until the other two kids arrive. Mar 10 23:46:13 (at least then my wife will be around). Mar 10 23:46:41 kk Mar 10 23:46:58 So you pretty much understand array slices? Mar 10 23:47:34 I believe so... Mar 10 23:47:39 Okay. Mar 10 23:47:52 You can slice any list in Perl, including list literals. Mar 10 23:48:21 That is, you can slice (1, 2, 3, 4, "hello", "mashed bananas", "fish"). Mar 10 23:49:07 It looks just like an array slice - same basic notation. (1,2,3,4,"hello","mashed bananas","fish")[3,4,5] Mar 10 23:49:29 What values does that refer to? Mar 10 23:50:27 ummm Mar 10 23:50:46 4 Mar 10 23:50:49 "hello" Mar 10 23:50:54 "mashed bananas" Mar 10 23:50:55 ? Mar 10 23:51:08 Perfect! Start counting from 0. Mar 10 23:51:12 I did! Mar 10 23:51:16 :D Mar 10 23:51:21 :D :D :D :D :D :D Mar 10 23:51:45 So that gives you 012 _3_ (the value 4), then 4 (the value "hello"), then 5 (the value "mashed bananas"). Absolutely perfect. Mar 10 23:52:09 Okay, so that's array slices, and list slices in general. Mar 10 23:52:29 :D Mar 10 23:52:31 I get that! Mar 10 23:52:34 I understand it1 Mar 10 23:52:38 er Mar 10 23:52:39 I understand it! Mar 10 23:53:17 w00t! Mar 10 23:53:40 Okay, now next thought - lists and list slices may have _expressions_ as indices. Mar 10 23:53:45 What do I mean? Mar 10 23:54:01 ummm Mar 10 23:54:04 expressions... Mar 10 23:54:08 may I consult my notes? Mar 10 23:54:09 Looks like this: @array[ 2 + 4, $cats + 1, $henry ] Mar 10 23:54:20 JulieAnn: That was rhetorical, sorry. Mar 10 23:54:30 oh. Mar 10 23:54:42 gotcha Mar 10 23:56:27 An expression is any combination of symbols (numeric literals, string literals, list literals, variables, function calls, and operators) that can be evaluated by Perl. So "2+2" is an expression, because it can be evaluated by Perl. So is "2 + $myval". Mar 10 23:56:58 ok Mar 10 23:58:17 Note that the stuff in square brackes is a _list_. Mar 10 23:58:36 So you can use a single expression that returns a list in order to refer to an array slice. Mar 10 23:58:40 Looks like this: Mar 10 23:59:31 @array = (10,15,253,23,2423,234,"hello"); @array2 = (3,5,2); print @array[@array2]; Mar 11 00:00:19 Any guesses? Mar 11 00:00:39 ummm Mar 11 00:00:56 23 Mar 11 00:01:05 234 Mar 11 00:01:10 253 Mar 11 00:01:15 Yeeeeeees! Mar 11 00:01:20 Nice. Mar 11 00:01:21 :D Mar 11 00:02:00 :D :D :D :D :D :D Mar 11 00:02:13 (my curfew tonight, btw, is in 57 minutes :( Mar 11 00:02:21 Just FYI - you can have negative indices. Mar 11 00:02:27 They count from the end. Mar 11 00:03:05 Any idea what: print (10,15,253,23,2423)[-2]; will print? Mar 11 00:03:12 (be careful! This is a trick). Mar 11 00:03:13 253! Mar 11 00:03:22 Gotcha! Mar 11 00:03:25 damnit Mar 11 00:03:27 * JulieAnn cries Mar 11 00:03:31 23. Mar 11 00:03:38 thats a mean one Mar 11 00:03:39 heh Mar 11 00:03:44 :( Mar 11 00:03:54 [0] refers to the _first_ element. [-0] == [0]. Mar 11 00:04:47 Get it? Mar 11 00:05:26 mmhmm Mar 11 00:05:30 Okay. Mar 11 00:06:15 One last thing about lists that is helpful to know in slices. The size of the array is given by @array in scalar context (as you've already seen). This is tricky and often backfires. Mar 11 00:06:56 ok Mar 11 00:07:25 Something that will always work, however, is $#array. $#array always returns the _index of the last element of the list_. (Do you see the difference between the "index of the last element" and the "size of the array"? As a rule what will be the (arithmetic) relationship between the two?) Mar 11 00:08:07 ummm Mar 11 00:08:22 the index of the last element would be the number of it Mar 11 00:08:29 the size of the array is one more Mar 11 00:08:34 Right! Mar 11 00:08:49 In general, @array == $#array + 1;. Mar 11 00:09:03 now Mar 11 00:09:04 ? Mar 11 00:09:24 I thought that perl interprets # in a code as ignoring whatever follows Mar 11 00:09:26 I say in general, because if @array is _empty_, then @array == $#array;. Mar 11 00:10:02 JulieAnn: You're right - usually a hash mark after any amount of whitespace at the beginning of the line is a comment. Mar 11 00:10:26 JulieAnn: But, for better or worse, "$#" has special meaning. Mar 11 00:10:32 oh ok Mar 11 00:11:17 Okay. Now this has some of the worst notation in Perl. I really dislike it. But... is what it is. Mar 11 00:12:15 Hash slice notation is the same as array slice notation, except with curly braces instead of square brackets. Mar 11 00:13:29 %hash = ("hello" => "world", "i like" => "fish", "green hair" => "punk", "heavy metal" => "pick of destiny"); Mar 11 00:14:05 LOL Mar 11 00:14:07 @hash{ "green hair", "i like" } = ("snot wipe", "massage"); Mar 11 00:14:38 ^---- Eh? Starts with an "@"? Unfortunately, yes. Mar 11 00:14:51 thats confusing :/ Mar 11 00:14:54 ^----- At least it has curly braces, like a hash. Mar 11 00:15:18 Yup. Confusing. Stupid. Mar 11 00:15:26 But... the way it is. Mar 11 00:15:29 :/ Mar 11 00:16:14 You can use interpolation the same as an array (ie - expressions in the curly braces): Mar 11 00:16:28 ok Mar 11 00:16:40 @punk = ("Iggy Pop", "Jello Biafra", "Fred"); Mar 11 00:17:15 @stars{@punk} = ("alive", "alive but you could fool me", "dead"); Mar 11 00:17:42 Do you grok the second one? Mar 11 00:17:55 grok? Mar 11 00:18:01 parse and understand Mar 11 00:18:32 oh Mar 11 00:18:40 yes, I think so Mar 11 00:19:06 What would be an equivalent set of assignments? Mar 11 00:19:40 um Mar 11 00:19:56 * JulieAnn thinks Mar 11 00:20:56 I don't wanna be wrong Mar 11 00:20:57 um Mar 11 00:21:00 why not? Mar 11 00:21:07 Just answer. Best best best answer. Mar 11 00:21:21 k Mar 11 00:21:30 (But don't worry if you're wrong. You're among friends, and I'm only paying in egoboo anyhow.) Mar 11 00:21:48 @stars{@punk}[1] = "alive" Mar 11 00:22:09 (It'll be different when you're working for a programming company making $78k/year based on the ingenuity of your code) Mar 11 00:22:14 @stars{@punk}[2] = "alive but you could fool me" Mar 11 00:22:29 @stars{@punk}[3] = "dead" Mar 11 00:22:55 is that wrong? Mar 11 00:22:55 :/ Mar 11 00:22:56 Good answer. Not one you'll want to give the programming company, but a good effort nonetheless. Mar 11 00:23:02 :'( Mar 11 00:23:23 When you are assigning only one value on the right of the '=', there should be a scalar on the left. Mar 11 00:23:45 shit Mar 11 00:23:49 Similarly, when you are referring to only one element of a hash, there should be a scalar in the curly braces. Mar 11 00:24:10 but that's confusing :/ Mar 11 00:24:12 Try again. Mar 11 00:24:44 ok Mar 11 00:24:45 um Mar 11 00:25:07 $stars{@punk}[1] = "alive" isn't right either, is it? Mar 11 00:25:08 :/ Mar 11 00:25:09 umm Mar 11 00:25:16 Nope, but it's closer. Mar 11 00:25:39 wait Mar 11 00:25:44 wait wait Mar 11 00:26:09 shoot, i think im lost too Mar 11 00:26:15 kfoo: heh Mar 11 00:26:19 * Poseiden waits. Mar 11 00:26:20 $stars{@punk}[1] = "Iggy Pop + alive" isn't right either, is it? Mar 11 00:26:25 or is it? Mar 11 00:26:27 JulieAnn: It isn't. Mar 11 00:26:30 fuck fuck fuck Mar 11 00:26:36 is it closer? Mar 11 00:26:40 play hot'n'cold here Mar 11 00:26:46 trying to find another way to do the @stars{@punk} assignment? Mar 11 00:26:47 I'll give you the first one. It's going to hurt, because once you read it it'll be obvious. Mar 11 00:26:54 kfoo: yup. Mar 11 00:27:17 $stars{$punk[0]} = "alive"; Mar 11 00:27:23 is it closer?fuck. Mar 11 00:27:27 fuckfuckfuckfuckfuckfuck Mar 11 00:27:31 I forgot 0 Mar 11 00:27:41 you had it, just in the wrong place Mar 11 00:27:46 goddamnit Mar 11 00:27:52 how can I be that stupdi/ Mar 11 00:27:56 *stupid Mar 11 00:28:06 JulieAnn: shush, it confused me too Mar 11 00:28:09 JulieAnn: Genetic defect? Dropped on the head as a child? Mar 11 00:28:14 * JulieAnn cries Mar 11 00:28:27 JulieAnn: Or maybe this stuff isn't self-evident. To anyone. Mar 11 00:28:41 JulieAnn: Take your pick, whichever explanation makes you most comfortable. Mar 11 00:28:54 I like the 2nd one. Mar 11 00:29:03 JulieAnn: Go with it. Mar 11 00:29:10 but the 1st one sounds rather plausible. Mar 11 00:29:51 JulieAnn: They could all be true. Mar 11 00:30:02 heh Mar 11 00:30:15 Okay, does it make sense now that I've spoonfed you the answer? Mar 11 00:30:22 So now. *blots eyes with tissue* Mar 11 00:30:28 Yes. :/ I'm sorry :( Mar 11 00:30:38 DON'T. APOLOGIZE. EVER. AGAIN. Mar 11 00:31:08 JulieAnn: _That_ is going to piss me off. The self-depracating stuff I can treat as a joke. That, however, I cannot abide. Mar 11 00:31:15 yessir. Mar 11 00:32:05 You now more or less understand list slices, including hash slices. This puts you about 12 light-years ahead of most Perl-mongers (what some Perl hackers call themselves and one another). Mar 11 00:32:40 heh Mar 11 00:32:41 ok Mar 11 00:32:55 Okay, I'm going to give you an assignment. It is in the way of homework - don't start it now, hold it for later and have it ready for when next we meet. Mar 11 00:33:03 The assignment is this: Mar 11 00:34:37 First, create a file that has words in it, one word per line. If you can bribe Henry into randomly generating such a file for you, that would be good. Make them all four-letter words (they don't have to be English words - just four letter strings of lower-case letters). You want at least 10,000 words (and, thus, lines) in the file. Mar 11 00:34:59 holy crap Mar 11 00:35:01 ok Mar 11 00:36:22 In an ideal world, there would be less than 1000 words in the file, each one repeated randomly throughout the file some (random) number of times. Mar 11 00:36:51 so 1000 words repeated to make 10000 lines? Mar 11 00:36:58 i thinki have sufficiently bribed him Mar 11 00:37:03 hold on a sec, i'm dying Mar 11 00:37:59 ok, i'm dead now. Mar 11 00:38:01 Continue. Mar 11 00:38:17 Second, write a program that reads words, one per line, from STDIN. Your program should count how many times each word occurred in the file. Mar 11 00:38:33 The output should be in alphabetical order. Mar 11 00:38:59 It should output an alphabeticacl list of the words, and the number of times each occurred. So, for instance: Mar 11 00:39:12 aaaa occurred 171 times Mar 11 00:39:21 abad occurred 12 times Mar 11 00:39:26 ... Mar 11 00:39:56 alphabeticacl is exactly like alphabetical, FYI. It's just the Turkish spelling. Mar 11 00:40:24 bbiab - have to go beat my child. Mar 11 00:40:24 lol Mar 11 00:40:28 k Mar 11 00:40:36 yes, children are not beaten enough these days Mar 11 00:44:07 omg Mar 11 00:44:11 i can;t stop coughing Mar 11 00:44:13 i think i'm dying Mar 11 00:45:00 * kfoo has quit (Quit: Lost terminal) Mar 11 00:45:51 * Notify: kfoo is online (maple.ix.ca.dal.net). Mar 11 00:46:03 * kfoo (foobar@66.139.79.133) has joined #perltutor Mar 11 00:46:13 that sucked Mar 11 00:46:18 w *cough* b *coughhhhhhhhhhhhhhhhhhhhhhhh* Mar 11 00:47:29 ouchouch Mar 11 00:51:38 whatd i miss? Mar 11 00:52:09 yes, children are not beaten enough these days Mar 11 00:52:09 omg Mar 11 00:52:09 i can;t stop coughing Mar 11 00:52:09 i think i'm dying Mar 11 00:52:09 * kfoo has quit (Quit: Lost terminal) Mar 11 00:52:09 * Notify: kfoo is online (maple.ix.ca.dal.net). Mar 11 00:52:09 * kfoo (foobar@66.139.79.133) has joined #perltutor Mar 11 00:52:28 erm Mar 11 00:52:37 I don't really have you on notify... Mar 11 00:52:45 I just added that to boost yer ego Mar 11 00:52:52 'n' stuff Mar 11 00:53:48 i already knew i was :P Mar 11 00:54:04 oh. Mar 11 00:54:05 well then. Mar 11 00:56:19 Okay.... Mar 11 00:56:31 wb Mar 11 00:56:33 Sorry about that. JulieAnn, you have curfew in about 4 minutes? Mar 11 00:56:40 kfoo? Mar 11 00:56:44 Is that my curfew? Mar 11 00:56:54 are you feelin ok? :P Mar 11 00:57:02 heh... curfew, kfoo... that sounds funny. Mar 11 00:57:12 I feel sore from coughing, but otherwise alright Mar 11 00:57:23 you should probably rest then :P Mar 11 00:57:28 ok :/ Mar 11 00:57:35 then yes, my curfew is in 4 minutes. Mar 11 00:57:47 Okay. Then that will be your homework. Mar 11 00:57:54 Guess what, JulieAnn? Mar 11 00:58:01 You're a Perl programmer. Mar 11 00:58:03 What? Mar 11 00:58:05 Am I? :) Mar 11 00:58:08 You am. Mar 11 00:58:08 yay Mar 11 00:58:14 * kfoo rejoices Mar 11 00:58:17 Well, I still have lots to learn, right? Mar 11 00:58:27 ya know the basics, you'll learn for the rest of your life Mar 11 00:58:29 Oh yes. Lots and lots and lots. Mar 11 00:58:45 i still write bad code! Mar 11 00:58:47 We have plenty more in this class. But you can already write Perl programs. Mar 11 00:58:56 That makes you a Perl programmer. Mar 11 00:59:05 :D Mar 11 00:59:08 By the end of this, you'll be a Perl hacker. Mar 11 00:59:19 how long does it take to become larry wall? Mar 11 00:59:29 kfoo: Thankfully, a very, very long time. Mar 11 00:59:39 hehe Mar 11 00:59:55 kfoo: And longer to be Randall Schwartz (who appears to know more Perl than Wall). Mar 11 00:59:56 :D Mar 11 01:00:20 Alright, you're turning into a pumpkin Julie. Off to bed. Mar 11 01:00:31 Thank you Poseiden *hugs* Mar 11 01:00:42 Cheers. Mar 11 01:00:42 I really appreciate you taking the time out to teach me. Mar 11 01:00:49 And I will work on the apology thing... Mar 11 01:00:55 but I'm quitting smoking. Mar 11 01:00:57 cold turkey. Mar 11 01:01:00 effective now. Mar 11 01:01:02 Anyone who claims they have something better to do is stupid or lying. Mar 11 01:01:16 so..... I may have good reason for the "i'm sorry"s Mar 11 01:01:24 yeah, i'd rather teach someone who is willing to learn than follow behind someone else fixing their mistakes Mar 11 01:01:46 JulieAnn: If we have to, we'll take a long break for a couple weeks while you clean up your lungs. Mar 11 01:01:54 noooooooo Mar 11 01:01:59 I have an oral fixation Mar 11 01:02:08 JulieAnn: I have no problem with that. In fact, vacation is over on Monday. Mar 11 01:02:10 I need something to do instead of sticking a ciggie in my mouth Mar 11 01:02:16 JulieAnn: pencil? Mar 11 01:02:18 ill send you a bucket of chupa chups Mar 11 01:02:22 that'll hurt my teeth Mar 11 01:02:26 kfoo, will you really? Mar 11 01:02:30 that would like... kick ass Mar 11 01:02:34 yeah, if i can find where to order them again Mar 11 01:02:43 BTW, wtf is a chupa chup? Mar 11 01:02:55 mexican lollipop, creamy flavors Mar 11 01:03:02 Ah. Mar 11 01:03:27 i like the ice cream flavors the bestest :D Mar 11 01:04:18 Wow. The logo is a Dali. Mar 11 01:04:48 sorry, they're from spain Mar 11 01:05:05 g;night, boys Mar 11 01:05:11 You're BOTH splendiforous :D Mar 11 01:05:21 night, JulieAnn. Mar 11 01:05:27 kfoo, I'm out. Best to you. Mar 11 01:05:29 g'nite :) Mar 11 01:05:32 thanks, you too Mar 11 01:05:37 * Poseiden is now known as Poseiden_afk Mar 11 07:44:53 * Discon