in reply to loop through file checking for session variable

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

Replies are listed 'Best First'.
Re^2: loop through file checking for session variable
by grashoper (Monk) on Oct 30, 2007 at 03:34 UTC
    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
        Here is my current subroutine, I would like to use a file instead of an if then structure, so if I go on vacation, someone can just add sitecodes to the file. This works, but adding a file would make it better, I could change the sub depending on the match to call this or the other sub
        sub MLX40Tutorials { my $str = ""; $str .= mLeftIndex(); my $xml = xmlFileData(Content", "Sitelist40.xml", "MLX40Tutorials()") +; if ($Request->item("DocID")->item()) { my $id = $Request->item("DocID")->item(); $xml =~ /(<tut id=\"$id\".*?\/>)/s; $str .= $1; } else { $xml =~ /(<mlxchange40.*?<\/mlxchange40>)/s; $str .= shadowBox("Mlxchange Tutorials", "<tutorials>".$1."</tut +orials>", ($g_docWidth/5*4-(2*$g_docSpace))); } return $str; }
        Sub 2 trying to break into 2 seperate routines..
        sub mTutorials { if ($Session->{'usrSystem'} eq "MRT") { my $str=""; $str.=mLeftIndex(); my $xml=xmlFileData("Content", "Sitelist40.xml", "MLX40Tutorials()") +; if ($Request->item("DocID")->item()) { my $id = $Request->item("DocID")->item(); $xml =~ /(<tut id=\"$id\".*?\/>)/s; $str .= $1; } else { $xml =~ /(<mlxchange40.*?<\/mlxchange40>)/s; $str .= shadowBox("MLXchange Tutorials &amp; Demos", "<tutorials +>".$1."</tutorials>", ($g_docWidth/5*4-(2*$g_docSpace))); #$xml =~ /(<mlxchange.*?<\/mlxchange>)/s; #$str .= shadowBox("MLXchange 3.0 Tutorials", "<tutorials>".$1." +</tutorials>", ($g_docWidth/5*4-(2*$g_docSpace))); } return $str; } if ($Session->{'usrSystem'} eq "RMLS") { my $str=""; $str.=mLeftIndex(); my $xml=xmlFileData("Content", "Sitelist40.xml", "MLX40Tutorials()") +; if ($Request->item("DocID")->item()) { my $id = $Request->item("DocID")->item(); $xml =~ /(<tut id=\"$id\".*?\/>)/s; $str .= $1; } else { $xml =~ /(<mlxchange40.*?<\/mlxchange40>)/s; $str .= shadowBox("MLXchange Tutorials &amp; Demos", "<tutorials +>".$1."</tutorials>", ($g_docWidth/5*4-(2*$g_docSpace))); #$xml =~ /(<mlxchange.*?<\/mlxchange>)/s; #$str .= shadowBox("MLXchange 3.0 Tutorials", "<tutorials>".$1." +</tutorials>", ($g_docWidth/5*4-(2*$g_docSpace))); }return $str; } else { if ($Session->{'usrSystem'} eq "BAR") { my $str=""; $str.=mLeftIndex(); my $xml=xmlFileData("Content", "Sitelist40.xml", "MLX40Tutorials()") +; if ($Request->item("DocID")->item()) { my $id = $Request->item("DocID")->item(); $xml =~ /(<tut id=\"$id\".*?\/>)/s; $str .= $1; } else { $xml =~ /(<mlxchange40.*?<\/mlxchange40>)/s; $str .= shadowBox("MLXchange Tutorials &amp; Demos", "<tutorials +>".$1."</tutorials>", ($g_docWidth/5*4-(2*$g_docSpace))); #$xml =~ /(<mlxchange.*?<\/mlxchange>)/s; #$str .= shadowBox("MLXchange 3.0 Tutorials", "<tutorials>".$1." +</tutorials>", ($g_docWidth/5*4-(2*$g_docSpace))); } return $str; } else { my $str = ""; $str .= mLeftIndex(); my $xml = xmlFileData("Content", "Tutorials.xml", "mTutorials()"); if ($Request->item("DocID")->item()) { my $id = $Request->item("DocID")->item(); $xml =~ /(<tut id=\"$id\".*?\/>)/s; $str .= $1; } else { $xml =~ /(<mlxchange.*?<\/mlxchange>)/s; $str .= shadowBox("MLXchange Tutorials &amp; Demos", "<tutorials +>".$1."</tutorials>", ($g_docWidth/5*4-(2*$g_docSpace))); $xml =~ /(<general.*?<\/general>)/s; $str .= shadowBox("General PC Tutorials", "<tutorials>".$1."</tu +torials>", ($g_docWidth/5*4-(2*$g_docSpace))); } return $str; } } }
        Grandfather does this look good? I can't test from here as it takes forever to do from home..most of sub Tutorials will be pulled into submlx40 with less code as I won't need the if's, and I can chop tutorials back to its previous state which of course was much shorter without all the if thens, which I added today as this was a last minute change request.mlx40 is a start on using the file to switch tween one or the other.
        my $sitelist=(xmlFileData("Content", "sitelist.txt"); while (<$sitelist>){ chomp; if (/\Q$Session->{'usrSystem'}\E/){ $str.=MLX40Tutorials(); } else { $str.=mTutorials(); } }