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

Hi Monks, Here is my piece of code--
sub search_for_username_and_no_of_questions { for(;$index<@records;$index++) { if ($records[$index] =~ /userName\s+:\[(..*)\]/) { $User_Name = $1; print "User_Name = $User_Name\n"; $User_Name =~ s/\s+//g; for(;$index<@records;$index++) { if ($records[$index] =~ /Set\s+of\s+questions\(bitmap\ +)\s+selected\s+:\s+(\d+)/) { print "Inside set of questions bitmap selected\n"; $Bitmap_Number_For_Questions = $1; $no_of_questions{$User_Name}=$Bitmap_Number_For_Qu +estions; print "$Bitmap_Number_For_Questions is no of quest +ions for $User_Name\n"; $timestamp{$User_Name}=$Time_Stamp; print "$Time_Stamp is time stamp for $User_Name\n" +; return 0; } } } } }
suppose my user name is abc@$%, it is not getting stored in the user name filed. What can be the problem here. Thanks NT

20090809 Janitored by Corion: Restored content, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: special characters storage in perl
by moritz (Cardinal) on Aug 05, 2009 at 13:05 UTC
    You use the variable $index in two loops, so as soon as the first if-condition matches, the inner loop also exhausts the outer loop.

    Instead of iterating over the indexes you can simply iterate over the array elements directly, for my $item (@records) { ... } and then use $item where you used to use $records[$index].

    Also you should use strict; use warnings; and declare your variables.

      look at the return in the inner loop. I think the logic is what he intended. I theorize that his data looks like:
      username row
      num questions row
      username row
      num questions row
      username row 
      num questions row
      ...
      
      and that he is calling the function multiple times to get all his users (although I have no idea how he knows when to stop). he probably (shutter) has a loop around the function that tests $index < @records.


      -pete
      "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: special characters storage in perl
by dreadpiratepeter (Priest) on Aug 05, 2009 at 13:24 UTC
    For starters, use strict would solve most of your problems, (but probably give you a host of new ones as you learn all the things you are doing wrong)
    You are using a global variable $index to loop with , which you are never initializing. therefore only the first call to this function has any chance of working correctly.
    How are you determining that $no_of_questions{$User_Name} is not being set properly? Are you saying that the loop never matches and the prints don't happen? or are you saying that they are and some unspecified test after that fact is not showing it?
    If you showed us the data and how you are calling the function, we could be of more help.
    sub search_for_username_and_no_of_questions { my $username; foreach (@records) { if (/userName\s+:\[(..*)\]/) { ($username = $1) =~ s/\s+//g; } elsif (/Set\s+of\s+questions\(bitmap\)\s+selected\s+:\s+(\d+)/) { $no_of_questions{$username} = $1; $timestamp{$username} = $Time_Stamp; } } }
    (untested) should fix some of your more blatent coding problems, but I would want to eliminate the 2 global hashes and the global timestamp.
    Update: I think he calls his function multiple times to get all the results, not just the first. Updated my code sample to work right when called only once for all of them, (assuming 1 no_questions row for each username row). Of course this new version doesn't get the timestamp right, but his code is so screwy and reliant on globals that you can't have everything.


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: special characters storage in perl
by FloydATC (Deacon) on Aug 05, 2009 at 13:09 UTC
    1. Both for() loops use $index, the second overwriting the first one. This means the outer loop won't. Use my $index to fix this.

    2. Consider using

    foreach my $record (@records) { # Do stuff with $record }
    instead of
    for(;$index<@records;$index++) { # Do stuff with $records[$index] }

    -- Time flies when you don't know what you're doing
Re: special characters storage in perl
by Anonymous Monk on Aug 05, 2009 at 12:41 UTC