FAQ
Hello...
This is probably a very simple question, but I don't have much
experience using hashes...
I have a simple program that lists all of the files on a
directory:

@files = </dir/*>;
foreach $file (@files) {
print "$file\n";
}

which prints something like this:
/dir/file1
/dir/file2
/dir/file3
etc.

But an array doesn't suit my needs. What I really need is a
hash where each item in the directory is a value associated with a key that
is a number 1-n such that I could issue a print statement that would show
something like (to oversimplify!):

print "$key(%hash) $value(%hash)\n";

1 /dir/file1
2 /dir/file2
3 /dir/file3
etc.

such that I could do something like this:

$key = <STDIN>;
do something with $value associated with $key;

Any clues would be welcome...
-Chris

Search Discussions

  • Wagner, David --- Senior Programmer Analyst --- WGO at Nov 5, 2003 at 10:36 pm

    McMahon, Chris wrote:
    Hello...
    This is probably a very simple question, but I don't have
    much experience using hashes...
    I have a simple program that lists all of the files on a
    directory:

    @files = </dir/*>;
    foreach $file (@files) {
    print "$file\n";
    }

    which prints something like this:
    /dir/file1
    /dir/file2
    /dir/file3
    etc.

    But an array doesn't suit my needs. What I really need
    You could but why. As part of your print you could do somehting like:
    my $MyId = 1;
    foreach $file (@files) {
    printf "%3d $file\n", $MyId++;
    }
    Which should print out just as you want.

    Now the individual can enter say 3, and you can put from @files by subtracting 1 and you would have the file. Using scalar(@files) you now have your Max files within your array, so if they enter 6. You subtract 1 and get 5 but scalar(@files) returns 4, you know they entered an invalid number.

    Just a thought.
    Wags ;)


    **********************************************************
    This message contains information that is confidential
    and proprietary to FedEx Freight or its affiliates.
    It is intended only for the recipient named and for
    the express purpose(s) described therein.
    Any other use is prohibited.
    ****************************************************************
  • McMahon, Chris at Nov 5, 2003 at 10:53 pm
    This works great.
    The light bulb lit up, I guess I didn't really need a hash after
    all-- thanks for pointing out the simple solution!
    (But if any hash solutions make it to the list, I'll probably try
    them too, just for practice...)
    -C

    -----Original Message-----
    From: Wagner, David --- Senior Programmer Analyst --- WGO

    Sent: Wednesday, November 05, 2003 3:36 PM
    To: McMahon, Chris; beginners@perl.org
    Subject: RE: help building hash on-the-fly?

    McMahon, Chris wrote:
    Hello...
    This is probably a very simple question, but I don't have
    much experience using hashes...
    I have a simple program that lists all of the files on a
    directory:

    @files = </dir/*>;
    foreach $file (@files) {
    print "$file\n";
    }

    which prints something like this:
    /dir/file1
    /dir/file2
    /dir/file3
    etc.

    But an array doesn't suit my needs. What I really need
    You could but why. As part of your print you could do somehting
    like:
    my $MyId = 1;
    foreach $file (@files) {
    printf "%3d $file\n", $MyId++;
    }
    Which should print out just as you want.

    Now the individual can enter say 3, and you can put from @files by
    subtracting 1 and you would have the file. Using scalar(@files) you now
    have your Max files within your array, so if they enter 6. You subtract 1
    and get 5 but scalar(@files) returns 4, you know they entered an invalid
    number.

    Just a thought.
    Wags ;)


    **********************************************************
    This message contains information that is confidential
    and proprietary to FedEx Freight or its affiliates.
    It is intended only for the recipient named and for
    the express purpose(s) described therein.
    Any other use is prohibited.
    ****************************************************************
  • James Edward Gray II at Nov 5, 2003 at 10:58 pm

    On Wednesday, November 5, 2003, at 04:52 PM, McMahon, Chris wrote:

    (But if any hash solutions make it to the list, I'll probably try
    them too, just for practice...)
    It's not a hash problem. Hashes are associative arrays, they're for
    when you need to call contents by name. Any question of order, like
    the one you posted, is for array, which are simply an ordered list.
    Numbers instead of names.

    Hope that helps.

    James
  • Motherofperls at Nov 5, 2003 at 11:11 pm
    Hashes are for referencing by a key name. There isn't a need for a hash if
    you're using a number index.
  • Tore Aursand at Nov 6, 2003 at 12:27 am

    On Wed, 05 Nov 2003 15:23:49 -0700, McMahon, Chris wrote:
    But an array doesn't suit my needs. What I really need is a
    hash where each item in the directory is a value associated with a key that
    is a number 1-n such that I could issue a print statement that would show
    something like (to oversimplify!):
    If I understand you correctly, you just want to map each directory/file
    with an incremental number. An array already have an internal number; the
    index of the actual element in the array. So:

    my @files = </dir/*>;
    my $key = <STDIN>;
    my $element = $files[$key - 1] || "No directory/file at position $key";
    print $element . "\n";


    --
    Tore Aursand <tore@aursand.no>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedNov 5, '03 at 10:24p
activeNov 6, '03 at 12:27a
posts6
users5
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase