map vs. foreach : what am I missing 
Author Message
 map vs. foreach : what am I missing

I think I'm not understanding something fundamental about the way map
works. The way I understand it, blocks (1::) and (2::) below should do
basically the same thing. Block 2:: does what I expect, that is, count
the number of lines in each file in the list. However, when I use the
1:: 'map' method I get these errors:

File "maptest.pl"; Line 23:  Use of uninitialized value in open
File "maptest.pl"; Line 23:  Use of uninitialized value in concatenation
(.)

and then then result of the 'die' function:
Couldn't open : No such file or directory

What am I missing about 'map'?

#---------------8<-------------------------------

#!/usr/bin/perl -w


   "file1.txt",
   "file2.txt",
   "file3.txt"
   );

## 1:: 'map' method

## 2:: 'foreach' method
#for (my $i = 0; $i <= $#filelist; $i++) {
#  $results[$i] = do_open_file( $filelist[$i] );
#}


sub do_open_file {
   my $file = $_[0];
   open FILE, $file
      or die "Couldn't open $file: $!\n" ;

   my $count = 0;
   while (<FILE>) { $count ++; }

   close FILE;
   return $count;

Quote:
}



Sun, 08 May 2005 10:19:56 GMT  
 map vs. foreach : what am I missing

Quote:

> I think I'm not understanding something fundamental about the way map
> works. The way I understand it, blocks (1::) and (2::) below should do
> basically the same thing. Block 2:: does what I expect, that is, count
> the number of lines in each file in the list. However, when I use the
> 1:: 'map' method I get these errors:
> File "maptest.pl"; Line 23:  Use of uninitialized value in open
> File "maptest.pl"; Line 23:  Use of uninitialized value in concatenation
> (.)
> ## 1:: 'map' method


that's perl4-style. subroutines are called do_open_file().
but why do you call the sub without giving it an argument?
then of course in the sub you will get an undefined value.

Quote:
> ## 2:: 'foreach' method
> #for (my $i = 0; $i <= $#filelist; $i++) {
> #  $results[$i] = do_open_file( $filelist[$i] );
> #}

this works because you give do_open_file() an argument.
read
perldoc -f map
for an example of how map works.

hth, tina
--
http://www.tinita.de/        \  enter__| |__the___ _ _ ___
http://Movies.tinita.de/      \     / _` / _ \/ _ \ '_(_-< of
http://PerlQuotes.tinita.de/   \    \ _,_\ __/\ __/_| /__/ perception



Sun, 08 May 2005 10:33:11 GMT  
 map vs. foreach : what am I missing

Quote:

> I think I'm not understanding something fundamental about the way map
> works. The way I understand it, blocks (1::) and (2::) below should do
> basically the same thing. Block 2:: does what I expect, that is, count
> the number of lines in each file in the list. However, when I use the
> 1:: 'map' method I get these errors:

> File "maptest.pl"; Line 23:  Use of uninitialized value in open
> File "maptest.pl"; Line 23:  Use of uninitialized value in concatenation
> (.)

> and then then result of the 'die' function:
> Couldn't open : No such file or directory

> What am I missing about 'map'?

I may be wrong, but I think you're hoping that do_open_file()
defaults to $_ as its argument. Perl is pretty smart, but come on!

Quote:
> #!/usr/bin/perl -w


>    "file1.txt",
>    "file2.txt",
>    "file3.txt"
>    );

> ## 1:: 'map' method


                  ^^^^^^^^^^^^^

Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'



Sun, 08 May 2005 11:42:05 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Performance of map is poor (60% slower than grep or foreach)

2. Using map instead of foreach

3. $_ doesn't work in map, foreach in nested Safe reval

4. Multiple map,grep,while,foreach

5. map + grep replacement for this foreach

6. Help..I am BIT without a MAP

7. setpgrp, setgid - not working, what am I missing?

8. What am I missing ?

9. What am I missing from chat2.pl?

10. What am I missing for setting permissions?

11. What am I missing

12. cgi.pm cookie function - am I missing something?

 

 
Powered by phpBB® Forum Software