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

I want to loop through a text file looking to see if a line matches the value of a sessionvar, does this look right? if not how can I fix?
my $sitelist=xmlFileData("Content", "Sitelist.txt"); WHILE <$sitelist>; chomp($line); SWITCH: if ($Session->{'usrSystem'}=~$line){str.=MLX40Tutorials()")last SWIT +CH;}
xmlfiledata is a subroutine that opens a file first arg is directory second is filename (optional 3rd is subroutine) example session->usrsystem=BAR sitelist.txt=BAR, or some other 3 letter account code

Replies are listed 'Best First'.
Re: loop through file checking for session variable
by GrandFather (Saint) on Oct 30, 2007 at 03:12 UTC

    Well, it ain't off-the-shelf Perl. Maybe you mean something like:

    use strict; use warnings; my $sitelist = xmlFileData ("Content", "Sitelist.txt"); my $Session; my $str; while (<$sitelist>) { chomp; next unless /\Q$Session->{'usrSystem'}\E/; $str .= MLX40Tutorials (); }

    However the code that you posted is so far from anything meaningful that it is pretty much impossible to decide just what you really want to happen.

    I strongly recommend that you use strictures (use strict; use warnings;) which might at least make you focus a little on the scope of variables and where they get initialized.

    Note that a straight character for character match is almost never what you want when processing XML. Perhaps you should tell us something about the bigger picture?


    Perl is environmentally friendly - it saves trees
      I want a line for line comparison with sitelist to match the session variable usrSystem, usrSystem will definitely already have a value, I am checking sitelist to see if it is in the list, if so then I want to do something branch to subroutine that displays newstuff for example, if not then I want to do something else ie old subroutine, with your snippet it looks like conditional would fall out of the loop if the session is not a match, am I reading that correctly? session is global, sitelist can be local. grandfather does this make more sense? I am really trying to clarify. Thanks

        So write that up as a code snippet then we should be able to tell if it makes sense. Even better would be to mock up some data so that you can test the snippet is doing what you expect. Something like:

        use strict; use warnings; my $sampleData = <<DATA; this and that wibble the other DATA open my $sitelist, '<', \$sampleData; my $Session = {usrSystem => 'wibble'}; my $str; while (<$sitelist>) { chomp; if (/\Q$Session->{'usrSystem'}\E/) { print "New stuff for $_\n"; } else { print "Something else for $_\n"; } }

        Prints:

        Something else for This and that New stuff for wibble Something else for the other

        Perl is environmentally friendly - it saves trees