Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^2: Hash keys affect sorting

by 0xbeef (Hermit)
on Jul 07, 2006 at 22:00 UTC ( #559872=note: print w/replies, xml ) Need Help??

in reply to Re: Hash keys affect sorting
in thread Hash keys affect sorting

I'm a baby monk only... ;)

Have to admit I have never seen @_ used for anything else than for function arguments, and I don't exactly understand push @data, [@_]; Using @_ seems nice and intuitive, applying it to my example gives me:

use strict; my $output = <<EOD; 4865FA9B 0702 P H rmt0 TAPE OPERATION ERROR DE9A52D1 0704 I S rmt0 DEVICE DUMP RETRIEVED 4865FA9B 0701 P H rmt2 TAPE OPERATION ERROR F3E9B3E2 0620 I O SYSJ2 UNABLE TO ALLOCATE SPACE IN FILE SY +STEM DCB47997 0511 T H hdisk4 DISK OPERATION ERROR EOD my (@data, %identifier); for my $line (split /\n/,$output) { @_ = split /\s+/, $line, 6; push @data, [@_]; $identifier{$_[0]} = $data[-1]; # Index for first field } # Sort on multiple fields, in this case 4th (class) and 2nd (time)... print "\@data:\n"; for my $rec (reverse sort {$a->[3] cmp $b->[3] || $a->[1] <=> $b->[1]} + @data) { print "Error ID: $rec->[0] Time: $rec->[1]\n"; }

Also, if i understand you right, %identifier contains a hash of array references which I can use to access @data elements e.g.

print "key $_ value: $identifier{$_}->[0]\n" for (keys %identifier);

Thanks for the help!


Replies are listed 'Best First'.
Re^3: Hash keys affect sorting
by shmem (Chancellor) on Jul 07, 2006 at 23:03 UTC
    $_ is to scalar what @_ is to array and %_ is to hash, although %_ is seldom seen in the wild.

    push @data, [@_] means: $_ was split into @_; if they said push @data, @_ the array @_ would append it's elements to @data. To append @_ as one unit to @data it must be given boundaries [ ]; this construct creates an array reference. This reference (a scalar) is stored in @data, holding the elements of @_ which are copied into [ ] at the moment of creation of [ ]. See perlreftut. Think of [@_] as "scalarifying" @_ into an (anonymous) array reference; an array can only hold scalars (single values, which references happen to be).

    Wow. What noise ;-)

    hoping not to have confused you further,

    _($_=" "x(1<<5)."?\n".q/)Oo.  G\        /
                                  /\_/(q    /
    ----------------------------  \__(m.====.(_("always off the crowd"))."
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2022-05-22 12:21 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (80 votes). Check out past polls.