Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: scoping problem?

by rocroc (Initiate)
on Dec 07, 2011 at 00:38 UTC ( #942142=note: print w/ replies, xml ) Need Help??


in reply to Re^2: scoping problem?
in thread scoping problem?

Thanks chromatic. Here's the script with additional tests to see what's in $_. at each point.

use strict; use warnings; my $username; my $color; while(<>){ chomp; s/"//g; ($username,$color) = (split /,/,$_)[2,3]; print STDOUT "test of username: $username\n";#NEW print STDOUT "test of dollar-underscore: $_\n";#NEW ALSO if ("agag" =~ m/($username)/){ print STDOUT "here is the username: $username\n"; print STDOUT "here is dollar-underscore: $_\n"; } }
here's the output of 'perl test.pl test.txt' (script includes "use warnings" this time, so they are included in the output)
test of username: adad test of dollar-underscore: ADELMAN,John,adad,Ray test of username: agag test of dollar-underscore: AGAN,John,agag,Aditya test of username: ahah test of dollar-underscore: AHMED,John,ahah,Conor Use of uninitialized value $username in concatenation (.) or string at + test.pl line 11, <> line 4. test of username: test of dollar-underscore: Use of uninitialized value $username in regexp compilation at test.pl +line 13, <> line 4. Use of uninitialized value $username in concatenation (.) or string at + test.pl line 14, <> line 4. here is the username: here is dollar-underscore:

Note that $_ and $username are both coming out as they should (i.e. the split is working) before we get to the if statement. It just looks as though the match "agag" =~ m/($username)/ is failing. (note that the warnings are only being issued at the fourth "line" of the text file -- that is at the empty last line.)

still stumped. Perhaps I'm missing something really basic and obvious about the match operator?


Comment on Re^3: scoping problem?
Select or Download Code
Re^4: scoping problem?
by choroba (Abbot) on Dec 07, 2011 at 09:29 UTC
    Realy strange. Works for me:
    test of username: agag test of dollar-underscore: AGAN,John,agag,Aditya here is the username: agag here is dollar-underscore: AGAN,John,agag,Aditya
    Are you sure your input does not contain any non-printable characters?
Re^4: scoping problem?
by AnomalousMonk (Monsignor) on Dec 07, 2011 at 09:56 UTC

    My results are similar to choroba's. I suspect rocroc is being a bit coy with us about the code or data actually being used because I don't get the same output from the the posted code of Re^3: scoping problem?. I am using a  data file with the OPed data plus a blank line at the end.

    I have made minor code changes as follows:

    1. The substitution  s/"//g; is changed to  s/\x22//g; (0x22 is the hex value of ASCII " (double-quote)) because my little command-line editor gets very confused by unbalanced double-quotes.
    2. I have added single-quotes around the variables in the diagnostic print statements to clearly delineate the strings being printed.

    As you can see, the 'agag' record is detected (as well as the blank line, of course).

    >perl -wMstrict -le "my $username; my $color; while(<>){ chomp; s/\x22//g; ($username,$color) = (split /,/,$_)[2,3]; print STDOUT \"test of username: '$username'\n\"; print STDOUT \"test of dollar-underscore: '$_'\n\"; if (\"agag\" =~ m/($username)/){ print STDOUT \"here is the username: '$username'\n\"; print STDOUT \"here is dollar-underscore: '$_'\n\"; } } " data test of username: 'adad' test of dollar-underscore: 'ADELMAN,John,adad,Ray' test of username: 'agag' test of dollar-underscore: 'AGAN,John,agag,Aditya' here is the username: 'agag' here is dollar-underscore: 'AGAN,John,agag,Aditya' test of username: 'ahah' test of dollar-underscore: 'AHMED,John,ahah,Conor' Use of uninitialized value $username in concatenation (.) or string at + -e line 1, <> line 4. test of username: '' test of dollar-underscore: ' ' Use of uninitialized value $username in regexp compilation at -e line +1, <> line 4. Use of uninitialized value $username in concatenation (.) or string at + -e line 1, <> line 4. here is the username: '' here is dollar-underscore: ' '

      Not sure what to say about being coy with the code -- what I've posted is exactly what I'm running.

      As for the data, I've been cutting-and-pasting directly from the file I'm working with.

      In any event, perhaps choroba is right that there are non-printable characters in the file. How would I go about figuring that out?

      Slightly related idea: could something wierd about the encoding of the text file be going on?

      Again, thanks to all of you for your help

        How would I go about figuring that out?
        On linux, you can run od, hd or hexdump on your data to see. I prefer flags -tx1c for od and -C for hexdump. On Windows, you can write a Perl script to print it out:
        while (<>) { printf q/%x /, $_ for map ord, split //; print "\n"; }
        coy, esp. WordNet (r) 2.0, sense 2: "showing marked and often playful or irritating evasiveness or reluctance to make a definite or committing statement".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://942142]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-09-03 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls