Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

ForEach Command struggle..

by Kage (Scribe)
on Nov 01, 2001 at 02:58 UTC ( [id://122449]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: ForEach Command struggle..
by Fastolfe (Vicar) on Nov 01, 2001 at 05:36 UTC
    I'm having a lot of trouble following your code. Part of your problem might be because you seem to be using the wrong variables:
    # You assign values to pluralized versions of variables: ($ips,$times,$locationold,$titles)=split('×',$lines); # Then you *use* singular versions of those variables: $line="$ip×$time×$place×$title";
    Where is $ip set? Please run your script with use strict; in effect and with warnings (-w or use warnings;) enabled. You will probably catch most of your problems by doing that. Also, what are you trying to do with these:
    ${$location}{online} ${$loc}{online}
    Are those two references written correctly? Hope this helps.
Re: ForEach Command struggle..
by tfrayner (Curate) on Nov 01, 2001 at 05:37 UTC
    Okay, here goes...

    First off, you would really benefit from use strict. I know that gets said a lot around here, but it's also true. You appear to be using symbolic references (i.e. terms such as ${$locationold}{online}++ in this context), also Not a Good Thing, since apart from anything else they're easily mistaken for hard refs, and it's not obvious what they're pointing to unless you're very careful.

    I've not dug into the code perhaps as far as I should, but a couple of things stand out. The $place and $title variables are used only once, suggesting a typo (use strict helps here. Really. It saves me on a daily basis). Also, I'm not sure about what you're splitting on. You say it should be '|+|', but your first split is using 'x' as the delimiter.

    There may be fundamental misconceptions in the rest of the code, or it may well be fine. I suppose I might be inclined to use separate %exists hashes for each set of terms you're using it for, in case of overlap (unlikely, but not impossible). I may also have seriously misunderstood something :-)

    Anyway, hopefully this will give you a start. Good hunting,

    Tim

    Update: I just realised, the line $line="$ipx$timex$placex$title"; probably won't work either, because you don't have variables named $ipx, $timex, $placex etc. Writing it

    $line=$ip."x".$time."x".$place."x".$title;
    would be better. Especially if you had variables named $place and $title ;-)
Re: ForEach Command struggle..
by thunders (Priest) on Nov 01, 2001 at 22:11 UTC

    Hey Kage we spoke yesterday on the Chatterbox, and I downloaded and attempted to debug your code. first off there are several statements like this:
    ${$locationold}{online}++;

    doesn't $locationold contain a url string? Why are we dereferencing it? I'm really confused by this. If you're not an expert perl programmer, I think you are overcomplicating things.

    also you need to read the perlopentut man page. In this script you try to open variables that haven't been initialized that can clobber you script, and your opens should really be followed by an or die() for this reason. The param function from CGI.pm is defined but never used... You use the same filehandle over and over through different opens, which is a Bad Thing.

    Anyway, I applaud your effort, but I suggest in the future, when posting here, you add comments to your script before every major block explaining what you want to do. Indent your loops, and explain what errors the interpreter gave you. There are a lot of people on this site, who are much better programmers than me, and very helpful people to boot, but neither they nor i can help much if we cant understand what you are trying to do.

    Fun fact:

    foreach $loctitle (@titleb){ $online++; }
    is better written as:
    $online += @titleb;
Re: ForEach Command struggle..
by thunders (Priest) on Nov 02, 2001 at 05:13 UTC
    I know you said that you don't want us to rewrite your code... But that's what it needs. In fact you posted this earlier as part of Stupid, yet simple, sort question and jeroenes did a fine job of starting you off with some code. You should reread it, it's a big step in the right direction.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found