Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!convex!convex.COM From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: "MOST COMMON NOVICE BUG" contest Message-ID: <100628@convex.convex.com> Date: 16 Mar 90 12:45:04 GMT References: <15230@bfmny0.UU.NET> <15232@bfmny0.UU.NET> <1118@etnibsd.UUCP> <1990Mar16.010322.18464@tc.fluke.COM> Sender: usenet@convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 129 In article <1990Mar16.010322.18464@tc.fluke.COM> inc@tc.fluke.COM (Gary Benson) writes: > 1. It seems that perl scripts are compiled just prior to execution. > Would there be any benefit to a perl compiler that, like C, would > JUST do the compilation and create a file (a.out might be a cool name > or maybe p.out). This way, I could store the executable somewhere and > not have to recompile each and every time. Well, you can compile it, sort of, using the dump operator or the -u flag. I really love the speedup I get from a dumped perl script, but the disk space of including all of perl in each one is almost always prohibitive. I would like perl to dump JUST the dataspace and enough special text that says to re-invoke itself in a way indicating that it should load its data from this file. > > 2. I am studying C right now, too, and have just learned about > multi-dimensional arrays. Are these possible in perl? Do you set them > up similarly to the way C does it? If they are possible, does anyone > have a (well-commented) example showing how to set up and access, With the expression subscripting, we're getting closer to multi-dimmed arrays. It's still not very feasible though. You can use the $foo{$a, $b, $c} emulation, but when you ask for its keys, you'll only get back one set of them, not three. Consider this: @a = ( "this is", "that was", "those aren't" ); printf "a[1] is \"%s\"\n", $a[1]; printf "a[1][1] is \"%s\"\n", $a[1][1]; that line doesn't work. printf "a[1][1] is \"%s\"\n", split(' ',$a[1])[1]; neither does that printf "a[1][1] is \"%s\"\n", (split(' ',$a[1]))[1]; but that one does. It's pretty expensive an operation, and I wouldn't like to generalize it for N dimensions. You could hide this in a subroutine calling &a($i,$j) to get at pieces. Larry addressed the multi-dim'd array question in his message <7125@jpl-devvax.JPL.NASA.GOV> addressed to this group on 21 Feb. > 3. I have not been able to sort the output of a perl script yet. I > always have the script just create a "raw" file, then go out to unix > to do the sort. Can someone show me the best way to do those kinds of > things inside perl? Here's one way: while (<>) { # munge $_ push(@output, $_); } print sort @output; If you don't like the default sorting and can take a performance hit, do something like this: print sort myfun @output; where myfun is a subroutine that has its own idea of collation. Here's one that sorts numerically highest to lowest according to the digits held in columns 20-24. sub myfun { (substr($a,20,5) > substr($b,20,5)) ? 1 : -1; } To sort by field $N, you can do something like this print &sort_by_field($N, @output); where &sort_by_field is this # somewhat warmed-over Randal code sub sort_by_field { local($fieldno, @whole) = @_; local(@x,@f,$a,$b); sub byfield { $f[$a] lt $f[$b] ? -1 : 1; } for (@whole) { @x = split(' '); push(@f,$x[$fieldno]); } return @whole[sort byfield $[..$#whole]; } If you're storing your data in a hashed array %AZ, you could do something like this: $, = "\n"; print @A{sort keys %A}; or ( $, , $\ ) = ( "\t", "\n" ); # assumed for following examples, too for $key (sort keys %A) { print $key, $A{$key}; } You could also reopen STDOUT to be a sort pipe and send your output there. Here's an easy way: open (">-", "|sort"); for $key (keys %ENV) { print $key, $ENV{$key}; } But you could even do the work yourself (not claiming this is a realistic example, but it works): if ($kid = open (">-", "|-")) { die "can't pipe: $!" if $kid == -1; for $key (keys %ENV) { print $key, $ENV{$key}; } } else { print sort ; } exit 0; That should give you more than enough ways to sort. --tom -- Tom Christiansen {uunet,uiucdcs,sun}!convex!tchrist Convex Computer Corporation tchrist@convex.COM "EMACS belongs in : Editor too big!"