Help needed: I cannot seem to get this variable to substitute in this pattern 
Author Message
 Help needed: I cannot seem to get this variable to substitute in this pattern

I cannot seem to get this variable to substitute in this pattern:
/^$client_prod_list[$b]/


 for($b=0;$b<=$count1;$b++)
 {
 open(PRODUCTFILE, ">> /usr/cmis/cmisx/prodfile") || die "Can't
open:$!\n";

 while (<>)
 {
 if(/^$client_prod_list[$b]/)
 {
 print PRODUCTFILE;
 }
 }
 }
  What I want it to do:

- open  prodfile for writing in it. OK
- open cislist to get line containing pattern. OK
- while going through the file match pattern NOT OK
  It comes back with an error:

Use of uninitialized value at /usr/cmis/cmisx/cz line 79, <> chunk
915. (line 79 is if(/^$client_prod_list[$b]/)  )  


Thank you in advance

Sylvain




Mon, 17 Jul 2000 03:00:00 GMT  
 Help needed: I cannot seem to get this variable to substitute in this pattern


Quote:


> for($b=0;$b<=$count1;$b++)
> {
> open(PRODUCTFILE, ">> /usr/cmis/cmisx/prodfile") || die "Can't
>open:$!\n";

> while (<>)
> {

Insert this here:

  print STDERR "client_prod_list[$b] = <", $client_prod_list[$b], ">\n";

and see what the output looks like...

Quote:
> if(/^$client_prod_list[$b]/)
> {
> print PRODUCTFILE;
> }
> }
> }

Good luck !

Richard.
--

    "All along this path I tread  /  My heart betrays my weary head
     With nothing but my love to save / From the cradle to the grave"
                                 (Eric Clapton, "From the cradle")



Mon, 17 Jul 2000 03:00:00 GMT  
 Help needed: I cannot seem to get this variable to substitute in this pattern

[posted and mailed]

Quote:


>  for($b=0;$b<=$count1;$b++)


Because the first element is at 0, the last element is at $count1 - 1;

So, that should be:

for($b=0;$b<$count1;$b++)

But there is no need to use a temporary variable:


or

for($b=0;$b<=$#client_prod_list;$b++)


Quote:
> Use of uninitialized value at /usr/cmis/cmisx/cz line 79, <> chunk
> 915. (line 79 is if(/^$client_prod_list[$b]/)  )



The last time through the loop, $client_prod_list[$b] is
past the end of the array, which is why you got that warning.

Chipmunk



Tue, 18 Jul 2000 03:00:00 GMT  
 Help needed: I cannot seem to get this variable to substitute in this pattern

Quote:

> I cannot seem to get this variable to substitute in this pattern:
> /^$client_prod_list[$b]/

Have you checked to see what that variable actually is, at the time of the
substitution? You could either print it out, or you could check it in the
de{*filter*}.

Quote:

>  for($b=0;$b<=$count1;$b++)


Also, as Randal says, "Real Perl programmers don't use subscripts." Unless
you need $b for something, the more Perlian way is something like this:


...although you may wish to choose a better name than $item.

Quote:
>  {
>  open(PRODUCTFILE, ">> /usr/cmis/cmisx/prodfile") || die "Can't
> open:$!\n";

>  while (<>)


almost certainly better to open the file "manually" here, rather than to
use the diamond operator. (That lets you explicitly control the error
message, if the file can't be opened, for example. Since this is being
done within a loop, you could exit the loop early, thereby shielding your
users from a series of error messages.)

Quote:
>  {
>  if(/^$client_prod_list[$b]/)

It's nicer to those who read and maintain it if you indent your code to
show nesting levels. A programmers' editor will help you to accomplish
this.


expressions, you may want to use string comparisons instead of a
pattern match on this line.

Quote:
> Use of uninitialized value at /usr/cmis/cmisx/cz line 79, <> chunk
> 915. (line 79 is if(/^$client_prod_list[$b]/)  )  

That happens when you exceed the array bounds (in the for loop). :-)

Hope this helps!

--
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.*-*-*.com/



Tue, 18 Jul 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. sorry, help needed with variable in a pattern

2. Novice needs help pulling pattern to match as a variable from another file

3. Cannot seem to dup2(FILE, 3)...

4. split and substitute, substitute, substitute

5. Please help cannot display REMOTE_HOST variable

6. Need help substituting text except when in an HTML anchor

7. HELP: Need help with "pattern matching"

8. Pattern Match - substitute a string after the match

9. Seem to be getting substring instead of array element

10. perlish idiom for substitute in many files, only when pattern found

11. Substituting patterns using perl 5

12. sending patterns for substitutes as a parameter to a function

 

 
Powered by phpBB® Forum Software