> -----Original Message-----
> From: Quincy Ntuli [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, September 04, 2001 1:20 PM
> To: [EMAIL PROTECTED]
> Subject: HELP !! displaying Associate Array value pairs
> 
> 
> 
> greetings
> 
> I am trying to return the value from an associate array; i do 
> not understand what i am doing wrong. I know i made be 
> reinventing the wheel here if you follow what i am trying to do.

A sentence or two on what you're trying to do is often helpful :~)

> 
> thanks
> 
> 
> #!/usr/bin/perl -w

Suggest you "use strict;" and add "my" declarations to variables throughout.

> 
> %theHash = ();
> @dbRec = ();
> @listing = ();
> @sortedListing = ()
> ;
> # making a directory list
> 
>         foreach $f (<data/*>) { if (-f $f) { $theFileItem = "$f\n";
> 
> # list populated  into an array;
> 
>         push (@listing, $theFileItem);}}

Why are you adding the newline? It seems to me that will make the open()
below fail.

> 
> #sorting the list i doubt this works though
> 
>         @sortedListing = sort @listing;

Everything above can be shortened to:

   @sortedListing = sort grep -f, <data/*>;

> 
>         $listLength = @sortedListing;
> 
> for ($i=0; $i<= $listLength-1; $i +=1)
>         {

Ick. Don't use a C-style loop here. Just say:

   for my $fname (@sortedListing) {

$fname will be set to each file name in turn

>                 open(INVIN, "$sortedListing[$i]") or die 

Don't need the quotes.

   open(INVIN, $fname) or die "blah blah...";

> "COULD NOT OPEN $i\n";
> 
>                 <INVIN>; #ignoring the first line
> 
>                         while(<INVIN>)
>                                 {       $TheLine = $_;
>                                         chomp($TheLine);
>                                         push(@dbRec, $TheLine);
>                                 }

This can be shortened to:

   chomp(@dbRec = <INVIN>);

>         }
> 
>         $theHash{'$sortedListing[1]'} =[$dbRec];

Should be @dbRec, not $dbRec (n.b.: "use strict" would have caught that)
Also, remove the single-quotes. The variable won't be expanded inside single
quotes:

   $theHash{$fname} = [ @dbRec ];

> 
>         print "\$sortedListing[1] is 
> ($theHash{'$sortedListing[1]'})\n";

This will print something like:

   $sortedListing[1] is (ARRAY(0x80f80cc))

because $theHash{blah} is an array ref. If you want to print the contents of
the array, you need to dereference it:

   print "\$sortedListing[1] is (@{$theHash{$sortedListing[1]}})\n";

Q: If you're just loading these files into a hash, why sort the filenames?
It doesn't matter what order you load them in, right? Just curious.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to