Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Head to numb to see it.....

by Fian (Novice)
on Mar 08, 2001 at 22:14 UTC ( #63043=perlquestion: print w/replies, xml ) Need Help??

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

I'm new to this craic so bear with me as these questions will be none to complicated. Whats wrong with this folks? It won't increment the $count scalar varibubble......
if($arr[$lcv] =~ /^CONTACT_ME:/){ $contact_me = substr("$arr[$lcv]", 25); if($contact_me eq 'YES'){ $contact_me_count ++; } }

Replies are listed 'Best First'.
Re: Head to numb to see it.....
by danger (Priest) on Mar 08, 2001 at 22:26 UTC

    Well, assuming you have fixed length data so that you know YES occurs at index 25 in the string given, I'll use my PSI::ESP module and guess that you've read in lines of data into an array (@arr), and that the YES string is supposed to occur at the end of that line and you haven't chomped the newline off the end of the line -- thus you are trying to compare "YES\n" eq "YES". If this guess sounds right, try doing a chomp $contact_me; prior to your inner conditional test as in:

    #!/usr/bin/perl -w use strict; my @arr = <DATA>; my $contact_me_count = 0; for my $lcv (0 .. $#arr){ if($arr[$lcv] =~ /^CONTACT_ME:/){ my $contact_me = substr("$arr[$lcv]", 25); chomp $contact_me; if($contact_me eq 'YES'){ $contact_me_count ++; } } } print $contact_me_count; __DATA__ CONTACT_ME: YES CONTACT_ME: NO
(arturo) Re: Head to numb to see it.....
by arturo (Vicar) on Mar 08, 2001 at 22:30 UTC

    When you're running into troubles like this, you should try to get some debugging info; for example, print out the value of $contact_me to make sure you're getting the part of the string you want (i.e. is that inner condition ever met?).

    Assuming that part is working correctly, I think azatoth has a plausible suggestion about what might be wrong (that space).

    This may not make much sense with the data you're working with, but let me make a suggestion: you appear to have the location of the "YES" hard-coded into $arr[$lcv], which is pretty inflexible. If you can, you might change that and use a regular expression solution (update e.g. $contact_me_count++ if ($arr[$lcv] =~/^CONTACT_ME:.*\bYES\b/); -- change the \b at the end to $ if the YES is at the end of the string)).

    Here's a slightly more succinct way of doing it, given what you've written:

    if ($arr[$lcv] =~/^CONTACT_ME"/ and substr ($arr[$lcv], 25) eq 'YES')) + { $contact_me_count++; }

    Of course, perl allows you to say STATEMENT if EXPRESSION, if that floats your boat ( 3 lines into 1 ... who wouldn't like that?) =)

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      My most humble apologies Folks and sincere gratitude for all your time and effort. I was away for a few days and was amazed to see the reponse for a first timer... I solved it by chomp $contact_me; Sorry.....
Re: Head to numb to see it.....
by azatoth (Curate) on Mar 08, 2001 at 22:17 UTC

      FYI:It is perfectly legal to have a space between a variable and the ++ operator:

      my $count_variable = 0; $count_variable ++; $count_variable ++; print $count_variable; # prints: 2
        $s = 0; print $s ++ for 1..9;
        Will not increment. Apparently, it depends on the context, whether the whitespace matters.


        update: danger, I'm sure you must be right about the prototyping of print().

        My first reaction was also that the space is wrong, but then tye corrected me in the CB so I did a bit of experimenting and found surprising (to me) results. Guess what this snippet will do:
        print $foo; print $foo ++; print $foo, $foo ++;

        I'd like to be able to assign to an luser

Re: Head to numb to see it.....
by Tuna (Friar) on Mar 08, 2001 at 22:17 UTC
    Warning: Completely Untested!!!! But, try this:
    if($arr[$lcv] =~ /^CONTACT_ME:/){ $contact_me = substr("$arr[$lcv]", 25); } elsif($contact_me eq 'YES'){ $contact_me_count++; } else {# define some default behaviour here}

    UPDATE: removed space, as per azatoth's post

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://63043]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2023-06-01 06:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found