Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Problem with creating Files

by David92 (Sexton)
on Jul 25, 2014 at 09:31 UTC ( [id://1095012]=perlquestion: print w/replies, xml ) Need Help??

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

Hey Monks,

This is a very big program, so it might be, that I wont get an answer here, due to not understanding what do I seek help for, I'll try my best to explain.

--------------------------------------------------------

I have a problem when I create HTML pages with PERL. I use 2 Arrays. In FirstArray there are more elements than in SecondArray.

When I start to create HTML's, I use in one of the Navigation Menus the list of elements in FirstArray.

When you chose one element in the menu, it will open a new HTML (firstArray1.html) that wil display some statistics in a chart.

That HTML has two buttons below the chart: FirstArray and SecondArray (name of buttons).

If you click on SecondArray it will switch from current HTML (firstArray1.html) to (secondArray1.html). And then again if you click on FirstArray button it will switch to (firstArray1.html). The problem is that the FirstArray has more elements than SecondArray. And those elements that are NOT in the SecondArray are causing problems: Lets say we are in firstArray5.html and by clicking SecondArray button the page will try to load secondArrray5.html - HOWEVER there is NONE, because secondArray does not have that element as firstArray has.

I have to keep the code as it is (all other 1000 lines of code). I have a seperate sub where I create the buttons:

sub createButtons{ ($outfile,$filename)=@_; $replace = 'ay'; $find = 'DFT'; if(index($filename, $find) != -1){ $filenameDFT = $filename; $filenameLAY = $filename; $filenameLAY =~s/$find/$replace/; print $outfile " <a href='./$filenameLAY' class='button_l +ay'>Layout</a> <a href='./$filenameDFT' class='button_dft'> +DfT</a>\n </div>\n"; } #print "$filenameDFT\n"; else { $filenameLAY = $filename; $filenameDFT= $filename; $filenameDFT =~s/$replace/$find/; print $outfile " <a href='./$filenameLAY' class='button_l +ay'>Layout</a> <a href='./$filenameDFT' class='button_dft'> +DfT</a>\n </div>\n"; } return(@OutputButtons) }

Filename is for e.g. firstArray1.html. I use a foreach loop in other subs to go over all elements in firstArray and another to go through all elements of secondArray.

In real example filename is: file1LAY.html and file1DFT.html ... that is why I use the index, because I need a button to work for every HTML (filename). I want to make it so, that if file does not exist (but the reference is stored in one of the Buttons) it should change the reference to '#'

This is a hard challenge, but I am in despair, so atleast I can try to ask, someone might have an idea.

BR

Replies are listed 'Best First'.
Re: Problem with creating Files
by AppleFritter (Vicar) on Jul 25, 2014 at 09:43 UTC

    Let me see if I understand this correctly. You have two arrays, let's say @array1 and @array2. Each contains a list of file prefixes, and you're creating HTML files that are called ${prefix1}LAY.html, with $prefix1 ranging over all elements of @array1, and (similarly) ${prefix2}DFT.html, with $prefix2 ranging over all elements of @array2.

    Furthermore, while the elements of @array1 and @array2 come from the same pool of potential values, any given such value might or might not appear in either array: @array1 might have elements that are not in @array2, and @array2 might have elements that are not in @array1. So in those cases, there'll only be a ${prefix}LAY.html or a ${prefix}DFT.html file, not both, and (naturally) you don't want to the one that exists to link to the one that does not.

    Is that right?

    If so, without seeing more of your code, I'd say you should simply pass the information on whether a given prefix appears in either array to your createButtons sub.

      Wow @AppleFilter, you describe my problem that myself! Do you have any suggestion how I might do that? Keep in mind that everything is generalized, meaning:

      I have only 3 files for PERL:

      run.pl mainModule.pm (here I get the statistics info) and htmlModule.pm

      RUN.PL:

      It consists of calls to sub routines.

      1. Are the calls to subroutines in mainModule.pm to get the numbers (for statistic) for each file (html) -> this is stored in hash. After having values, I store also the elements that have this values inside @array1

      2. Back to .pl I loop the Array to store the numbers inside variables, these variables will be then put inside HTML to as values for the chart. I use something like:

      foreach $element (@array1){ $var1 = $hash{value1}; $var2 $hash{value2} createBarchart(....); }

      I use 2 different run.PL, because there are two different database's that I get the values from (for e.g. database1 and database2 -> they differ with unique ID which I provide as an INPUT when I call subs that get me the values)

        I'm not sure I fully understand your problem but try this
        #!perl use strict; # test for ('file1LAY.html','file1DFT.html','file2DFT.html'){ open OUT,'>',$_; createButtons(undef,$_); } sub createButtons{ my ($outfile,$filename) = @_; my %file=(); if ($filename =~ m/(.+)(?:LAY|DFT)\.html/){ for my $s ('LAY','DFT'){ $file{$s} = './'.$1.$s.'.html'; $file{$s} = '#' unless (-e $file{$s}); } } print qq! <a href="$file{'LAY'}" class="button_lay">Layout</a> <a href="$file{'DFT'}" class="button_dft">DfT</a>\n </div>\n!; }
        poj
        I'd like to help you, but it would really help to see a bit more actual code there; based on general descriptions, I'm afraid I can only give general suggestions.
Re: Problem with creating Files (maybe)
by Anonymous Monk on Jul 25, 2014 at 09:44 UTC

    Um, what? Is the answer not this?

    if( fileExists( $filename ) ){ ... realLink( $filename ); } else { ... fakeLink( $filename ); }

    You'll get better traction if you post short self contained program that compiles and runs and demonstrates the problem ... :)

      I was thinking of such solution, but I simply do not know where to put that path of code. Because the HTML is generated only after it returns to .PL.

      in .PL i use createBarchart and doesn't have any output, since he creates the file with:

      open ($outfile, ">", $dir.$filename)||die "Couldnt open the file!\n" +; (@outputHead) = createHeader($outfile,$filename); print $outfile " @outputHead\n"; print $outfile " [@legendArrayBAR],\n"; for($j=0; $j<$rows;$j++){ print $outfile "['$valueMatrixBAR[$j][0]'," +; for($i=0; $i<$cols-1;$i++){ print $outfile " $valueMatrixBAR[$j +][$i+1],"; if($loop_cou +nt == $cols-2){ $line +=~ s/,+$//; } $loop_count++; } print $outfile "],\n"; } print $outfile " ]); var options = { 'width': 800, 'height': 400, title: '$title', hAxis: {title: 'Sprint', titleTextStyle: {color +: 'red'}} };\n var chart = new google.visualization.ColumnChart( +document.getElementById('chart_div')); chart.draw(data, options); }\n"; (@outputFoot) =createFooter($outfile,\@CustomerList,\@SprintList,$fi +lename); print $outfile "@outputFoot\n"; #print "Finished writting a file\n"; close ($outfile);

      I hope this code won't confuse you. It has to be like that, so it can be used generally, not just for this case. But there might be sometimes a new Database, and everything I need to add then is the unique ID of that database, and everything should work like that.

        I was thinking of such solution, but I simply do not know where to put that path of code.

        put it in createButtons, the part that prints links, we're talking about createButtons, right?

        if none of the files exist yet , causing createButtons to be mostly fakeLink, just run the program twice  DoIt(); DoIt(); on first run, lots of the links will be wrong, but on the second run, the links will be right

        I hope this code won't confuse you.

        The code isn't confusing :) but seems to be missing JSON

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found