in reply to Re^2: loop through file checking for session variable
in thread loop through file checking for session variable

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

Replies are listed 'Best First'.
Re^4: loop through file checking for session variable
by grashoper (Monk) on Oct 30, 2007 at 04:30 UTC
    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; } } }
Re^4: loop through file checking for session variable
by grashoper (Monk) on Oct 30, 2007 at 04:49 UTC
    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(); } }