Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re: Sorting strings

by demerphq (Chancellor)
on Aug 31, 2001 at 06:32 UTC ( [id://109271]=note: print w/replies, xml ) Need Help??


in reply to Re: Sorting strings
in thread Sorting strings

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:
$array[1];
but if I want to get it through the refrence i have to do
$array->[1];
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 );

HTH
Yves

Replies are listed 'Best First'.
Re: (3) Sorting strings
by demerphq (Chancellor) on Aug 31, 2001 at 06:51 UTC
    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.

    Yves
Re: Re: Re: Sorting strings
by spartan (Pilgrim) on Aug 31, 2001 at 19:19 UTC
    ++demrphq
    Egads...
    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. :(
    -spartan

    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.
      HTH

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-24 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found