Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Assigning keys and values to a hash using arrays

by dReKurCe (Scribe)
on Dec 06, 2005 at 14:18 UTC ( [id://514458]=perlquestion: print w/replies, xml ) Need Help??

dReKurCe has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks:

I have a simple question regarding what I think requires a hash slice. The following code produces an output of:
Nlink 1

I would like to print all values and keys.
Any suggestions?
#! /usr/bin/perl use warnings; use strict; my (%inf,@info,@keys,$phile,$key,$value); use Cwd; my $dir=Cwd->getcwd(); opendir(DIR,"$dir")or die "@!"; while ( $phile=readdir(DIR)){ if($phile=~/^\.$/){next;} elsif($phile=~/^\.\.$/){next;} elsif($phile=~/^(.*)$/) {print "$phile\n";info($phile)} info ($phile); } sub info{ @info=stat($phile); @keys=qw(Dev Inode Mode NLink UID GID RDev Size +ATime CTime Blksize Blocks); @inf{@keys}=@info; } ($key,$value)=each %inf; print "@info\n"; print "$key,$value";

Replies are listed 'Best First'.
Re: Assigning keys and values to a hash using arrays
by Tanktalus (Canon) on Dec 06, 2005 at 14:25 UTC

    First comment is style. This is not very readable as you've posted it. Some judicious use of whitespace would make this much easier for a human to parse, without making it any more difficult for the computer.

    Second, you should read the documentation on each. It has an example that shows pretty much the same idea:

    while (($key,$value) = each %ENV) { print "$key=$value\n"; }
    You need to loop through "each" key/value rather than just the first.

    Third - your info function is going to overwrite the same global time after time. Is that really what you want? Your %inf will only hold the last file found.

    And finally, that last elsif will match anything. So I'm not sure why you have the "info ($phile)" outside of the if-elsif-elsif section.

Re: Assigning keys and values to a hash using arrays
by blazar (Canon) on Dec 06, 2005 at 14:40 UTC
    my (%inf,@info,@keys,$phile,$key,$value);

    Generally no need to have such long declarations. Declare as close as possible to the actual use of the variable.

    use Cwd; my $dir=Cwd->getcwd(); opendir(DIR,"$dir")or die "@!";

    "@!"?!?

    while ( $phile=readdir(DIR)){ if($phile=~/^\.$/){next;} elsif($phile=~/^\.\.$/){next;}

    How 'bout

    next if $phile =~ /.../;

    instead? Also, regexen are great, but how 'bout

    next if $phile eq '.' or $phile eq '..';

    which is not exactly what you do, but is what I think you want to do.

    elsif($phile=~/^(.*)$/)
    "else if it matches anything else"...
    {print "$phile\n";info($phile)} info ($phile); }

    Huh?!? Twice?

    sub info{ @info=stat($phile); @keys=qw(Dev Inode Mode NLink UID GID RDev Size ATime CTime Bl +ksize Blocks);

    Huh?!? Why passing it as a parameter if you use it as a "global"? Just adopt a proper form of the former option! (See perldoc perlsub)

    All in all if I understand correctly what you're after, it may be as simple as:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Cwd; my $dir=Cwd->getcwd; opendir my $dh, $dir or die "Can't open `$dir': $!\n"; while (my $file=readdir $dh) { next if $file eq '.' or $file eq '..'; print $file, "\n"; my %tmp; @tmp{qw/Dev Inode Mode NLink UID GID RDev Size ATime CTime Blksize + Blocks/}= stat $file; print Dumper \%tmp; } __END__

    (using Data::Dumper for lazyness!)

Re: Assigning keys and values to a hash using arrays
by dReKurCe (Scribe) on Dec 06, 2005 at 18:27 UTC
    Greets:

    Thanks for the quick response, this script contained some silly errors and oversights, I appreciate your patience.
    I did manage to accomplish what I wanted: the code is on my scratchpad.
    dReKurCe's scratchpad

Log In?
Username:
Password:

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

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

    No recent polls found