Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Sorting strings

by spartan (Pilgrim)
on Aug 31, 2001 at 00:41 UTC ( #109201=note: print w/replies, xml ) Need Help??

in reply to Sorting strings

++hofmator,++ducky... I now understand how map works. My next problem is this: map  {$_->[0]}. I know that map is operating on $_[0] (I think) but what is this: ->[0] ???. I read the FMTEYEWTK, and while Mr. Christiansen does a good job explaining, I must admit that I would rather have read a more exhaustive dissertation with more examples of what @alist, $a, @blist, $b, etc.. were set to. I found that I could understand some of the things Tom was explaining, and on others I just got lost in the code without knowing what the values of the variables he was operting in were. I guess I'm thinking very rigidly, because I can understand this:
@single = 1..10; @treble = map { $_ * 3 } @single; foreach (@treble) { print $_." "; } print "\n";
I can follow EXACTLY what's happening in that code snippet, because I know what @single is equal to each time the map function iterates over one of the values. The output is as follows:
3 6 9 12 15 18 21 24 27 30
However, when I tried following the code examples in FMTEYEWTK, I had a horrible time understanding what he was trying to convey. I know I won't know everything the moment I read it, and I intend to read FMTEYEWTK a few more times, but I have to say I'm a bit frustrated. I do know this: I'm not giving up. I might not understand this today, or tomorrow, but I will at some point.
If anybody would like to take a shot at helping me understand, I would appreciate it GREATLY. Hopefully I can pass this (or other) knowledge along to someone else someday.

Very funny Scotty... Now PLEASE beam down my PANTS!

Replies are listed 'Best First'.
Re: Re: Sorting strings
by demerphq (Chancellor) on Aug 31, 2001 at 06:32 UTC
    My next problem is this: map {$_->[0]}. I know that map is operating on $_[0] (I think) but what is this: ->[0] ???.
    I think you need to get the idea of refrences to arrays and arrays. Consider
    my $array=[]; #$array is a refrence to an anonymous array my @array;
    Now if I need to get an element of @array directly I use this syntax:
    but if I want to get it through the refrence i have to do
    The -> tells perl to treat the '$array' as the SCALAR with the name 'array' and not as another type with the same name, the [] tells perl that the scalar refers to an array and not a hash. The same thing applies to the variables $_ @_ %_

    So in the ST you wrap you data and your keys, one at a time, in anonymous arays (Notice the square brackets in the rightmost map body). Then SORT goes through each of the elements, which are refrences to arrays, and has to dereference them to get at the keys, finally the left most map takes of the anonymous array 'wrapper' and throws it away, returning the sorted list.
    BTW Maybe you know this, but for map/sort/grep/join its often a good idea to read them from right to left. (Or my preference from bottom to top)

    my @sorted=map {$_->[0]} sort{$a->[1] <=> $b->[1]} map {[$_,keyfunction($_)]} @list; my $nasty=join(",", map {$_->[0]} sort{$a->[1] <=> $b->[1]} map {[$_,keyfunction($_)]} grep {/^\w/} @list );

      but if I want to get it through the reference I have to do

      That should be:

      but if I want to use the array that $array refrences I have to do

      Sorry, I changed the example slightly and didnt change that line.

      Anyway, the point to keep in mind that

      $_=[]; # $_ now is a refrence to empty array (anonymous array) $_[0]=[]; # the zeroth element of the array @_ $_->[0]=[];# the zeroth element of the anonymous array $_{0}=[]; # the value associated with 0 in %_ $_{0}->[0]; # element zero in ^^^^^^^^ $_[0]->[0]; #zeroth element in array refrenced by the zeroth elemtn + of @_ $_->[0][0]={}; #dont need a second -> $_->{0}[0]={}; #this works too $_->[0]->[0]->{0}="but we can use -> if we want"; $_->{0}[0]{0}="This is getting confusing";
      I dont think %_ is used that much, but I could be wrong.

      I need to do some homework. I still do not understand. Thank you so much for your explanation though, I just don't get it. :(

      Very funny Scotty... Now PLEASE beam down my PANTS!
        ++spartan. Knowing when to do homework is a *good* thing. Wish I had when I was in highschool. :-)
        Ok maybe this will help: (Gurus, details are fudged for understanding here)
        Perl has two flavours of variables, lexical variables and dynamic variables. These are declared in a module in the following way (this is perl 5.6):
        my $lexical="Value"; our $dynamic="Value";
        Dynamic variables are package scoped, which means that such a variable (unless local()ized, which we may get to later) is available and shared throughout the *whole* package. These variables are entered into something called a 'pad' or more formally the package symbol table. This table is essentially a hash where the names (without type symbols $ % @ &) are the keys, and the values are these things called typeglobs (which are accessed through the * type symbol) this hash is reffered to inside a package as %:: and outside as %PACKAGE::. A typeglob is essentially a wrapper around the various types that a given name represents. Or in other terms a typeglob is a bundle of all of the dynamic variables that have a given name.This can be seen here
        #assuming warnings and strict our $Variable="Variable"; our %Variable=(hash_key=>"Variable"); our @Variable=split(//,"Variable"); sub Variable{ return "Variable"; } open Variable,">>c:/temp/variable.txt" or die "Couldnt open! $!"; { no strict 'refs'; *Other_Variable=*Variable; } $\="\n"; $,=","; print $Other_Variable; print keys %Other_Variable; print @Other_Variable; print Other_Variable(); print Other_Variable "Written to Other_Variable at ".localtime();
        If you run it you will see that accessing any variable type with the name Other_Variable really access the equivelent type with the name Variable (and vice versa, they are aliased to each other.) All with ONE assignment.
        Now lets have a quick look at the symbol table for the package the above code ran in. Add this code
        sub dump_symbol_table { print "SYMBOL TABLE\n--------------------------------------------- +----------\n"; foreach my $key (sort keys %::) { next unless $key=~/^[\x20-\xff]/; (my $value=$::{$key})=~s/([\x00-\x1f])/sprintf("\\x%02x",ord($ +1))/ge; printf "%20s = %-20s\n",$key,"'$value'"; } print "\n"; } BEGIN { dump_symbol_table } END { dump_symbol_table }
        Now you should see something like
        Other_Variable = '*main::Variable' ... Variable = '*main::Variable' ... \ = '*main::\' _ = '*main::_'
        Notice that there is a typeglob '_'? This is the typeglob that holds the dynamic variables $_ @_ %_ and the others. Also notice the aliasing? Other_Variable and Variable both have the same value.
        So now on to lexical variables. again these have symbol tables, but they are anonymous and bound to each level of scope in the program. But the typeglob issue is still the same.
        So on to the point (sorry its taking so long)
        Perl needs a way to tell each of these names apart. Normally it does this via the type symbol before the name, or its position in a statement (such as a print FH "string"; statement).
        But when we access an element from on ofthe compound data structures we use something _like_ a scalar context, ie we write $array[1] and not @array[1] The second is a list slice and returns a list, the first is an Array fetch from element 1 and returns a scalar. Since there is a $ in front perl could hypothetically get confused and think that we want $array and not @array, but the presence of the square brackets (or a ->[] for a reference) after the variable name tells perl unequivicably that we are talking about an element of @Array and not the scalar $array.

        Ok, so maybe this will help you a bit, at least with some terms that you can chase down in the documentation.

        You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://109201]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-01-28 06:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found