Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Seekers of Perl Wisdom

( #479=superdoc: print w/ replies, xml ) Need Help??

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
HTML::Template data loading
1 direct reply — Read more / Contribute
by Anonymous Monk
on Sep 16, 2014 at 13:41
    Hi Monks!
    I am trying to use HTML::Template to process the data results returned from my SQL query instead of returning json to the jQuery to process and display it. I can only get it to work if I return json, it ignores my HTML::Template tags in the .tmpl file, is there a way not to send back json and let the template handling it?
    I have sample code to show what I am trying to explain: search.tmpl
    This is the search form using jQuery autocomplete:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ +/"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Search</title> <script type="text/javascript" src="jquery-latest.js"></script> <script type="text/javascript"> var debug = false; $(document).ready(function() { $( "#search" ).autocomplete({ source: function(request, response) { $.ajax({ url: "", dataType: "json", data: { search: request.term, }, success: function(data) { // return data stuff here }, error: function(response){ if (debug == true) {console.log(response)}; alert('Somethings broken in the AJAX return'); } }); }, minLength: 5, delay: 500 }); }); </script> </head> <body> <div id="container"> <h2>TEST</h2> <div id="namesearch"> <form id="form_search" method="get" name="form_search"/> <label>Search: </label> <input type="text" name="search" id="search" class="search_box"/> </form> </div> <div> <p id="submitted"></p> <!-- display resutls from search here --> <div id='RESULTS'></div> <!-- end displaying results --> <!-- All Data - Here is where I want the data to display--> <TMPL_LOOP NAME=ALL_DATA> <TMPL_VAR NAME=NAME> <TMPL_VAR NAME=CITY> <TMPL_VAR NAME=STATE> </TMPL_LOOP> <TMPL_VAR NAME=COUNT> <!-- Data Ends --> </div> </body> </html>

    This is the Perl code that will process the request from the jQuery on the .tmpl

    ... my $main_tmpl = HTML::Template->new(filename => 'search.tmpl', die_on_ +bad_params => 1); ... sub data { my $id = shift || return; my $sql = qq{SELECT distinct name, city, state FROM my_table WHERE name like ? order by name asc}; my $sth = $dbh->prepare($sql); $sth->execute(${id}.'%') or die "SQL Error: $DBI::errstr\n"; my $all_data = $sth->fetchall_arrayref({}); my @loop_data = (); # initialize an array to hold loop # Count things my $c = 0; my @count; foreach my $row (@{$all_data}) { $c++; my %row_data; # get a fresh hash for the row data # fill in these rows $row_data{NAME} = $row->{NAME} || ''; $row_data{CITY} = $row->{CITY} || ''; $row_data{STATE} = $row->{STATE} || ''; push @count, $c; # the crucial step - push a reference to this row into the loop push(@loop_data, \%row_data); } # Count things my $counter = @count; # I need to load the data here but it doesn't. # load values into the template $main_tmpl->param( ALL_DATA => \@loop_data, COUNT => $counter, ); # If sending data through json use this # This works if I use jQuery to process the returned json from here # but I am trying to load the data using HTML::Template =code print $q->header(-type => "application/json"); if(@loop_data){ print to_json(\@loop_data); exit; }else{ my $json_string = [{ result => "No data"}]; print to_json($json_string); exit; } =cut } # end sub

    Thanks for looking!
DBI bind_columns
2 direct replies — Read more / Contribute
by fionbarr
on Sep 16, 2014 at 13:22
    my DBI book has both these formats:
    $sth->bind->columns(\$server); while ( $sth->fetch ) { $sth->bind->columns(undef, \$server); while ( $sth->fetch ) {
    Which is preferred and why, please?
whats wrong? it gives me many error
4 direct replies — Read more / Contribute
by yenne
on Sep 16, 2014 at 10:40
    use strict; use IO::Socket; #initial variables to work with my server my $host, $port, $request, $proto = 'tcp'; my $connectresponses = 2; #my ftp server responds with 2 lines when + you connect. print "What hostname do you want to connect to? "; chomp( $host = <STDIN> ); print "What port do you want to use? "; chomp( $port = <STDIN> ); my $sock = IO::Socket::INET->new( PeerAddr => $host, PeerPort => $port, Proto => $proto ) || die "Failure: $!"; print "Connection to $host on port $port successful!\n"; unless ( $port == 80 ) { for ( $i = 0; $i < $connectresponses; $i++ ) { $_ = <$sock>; print; } } print "Type commands (solely press enter to exit)\n"; &Terminal; close($sock); print "\nConnection to $host on port $port was closed successfully!\n" +; exit; #sub to emulate a terminal sub Terminal { while (1) { $request = ""; chomp( $request = <STDIN> ); print $sock "$request\n"; if ( $port == 80 ) { while (<$sock>) { print; } last; } else { unless ($request) { last; } $_ = <$sock>; print; } } }
    parenthesis missing arounf "my" list at line 7 print (..) interpreted as function at line 37 Global symbol "$port" requires explicit package name at line 7 Global symbol "$request" requires explicit package name at lin +e 7 Global symbol "$proto" requires explicit package name at line +7 Global symbol "$port" requires explicit package name at line 1 +3 Global symbol "$port" requires explicit package name at line 1 +5 Global symbol "$proto" requires explicit package name at line +7 has too many errors.
Problem creating animated GIF using GD and GD::Graph
2 direct replies — Read more / Contribute
by realityczar
on Sep 16, 2014 at 10:29
    Hi, Monks-

    I am trying to create an animated GIF of graphs using GD and GD::Graph. The following code creates individual graph GIF files, but the image.gif that results and is supposed to be the animated version won't open. Where have I gone wrong?

    Update: I searched the archives. I read them. And I didn't apply the wisdom. It's a lack of binmode. Sorry to have bothered you.

    use strict; use warnings; use GD; use GD::Graph::bars; my $img_width = 288; my $img_height = 216; my $number_of_hours = 24; my $img = GD::Image->new($img_width, $img_height); my $gifdata = $img->gifanimbegin(1,0); foreach my $hour (0..$number_of_hours-1) { my @h = CreateHistogram($hour); my @labels = 0..79; my @data = (\@labels, \@h); my $graph = GD::Graph::bars->new($img_width,$img_height); $graph->set( title => "HISTOGRAM HOUR $hour" ); my $gd = $graph->plot(\@data); $gifdata .= $gd->gifanimadd; open(FIL, ">image_$hour.gif"); binmode FIL; print FIL $gd->gif; close FIL; } $gifdata .= $img->gifanimend; open(GIF,">image.gif"); print GIF $gifdata; close GIF; exit; sub CreateHistogram { # stuff to create a histogram }
how to speed up pattern match between two files
4 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 16, 2014 at 10:03
    Input file1: col1 col2 col3 col4 ZGLP1 ICAM4 13.27 0.2425 ICAM4 ZGLP1 13.27 0.2425 RRP1B CDH24 20.8 1 ZGLP1 OOEP 18.79 0.3060 ZGLP1 RRP1B 39.62 0.2972 ZGLP1 CDH24 51.21 0.2560 BBCDI DND1 19.44 0.2833 BBCDI SOHLH2 36.61 0.2909 DND1 SOHLH2 18 0.8
    Input file2: chr8 18640000 18960000 ZGLP1 RRP1B CDH24 #gene number he +re is not fixed can be #4 #5 or more chr8 19000000 19080000 BBCDI DND1 SOHLH2 #gene number he +re is not fixed can be #4 #5 or more

    I have written a code which compares col1 and col2 of file1 with each line of file2 such that, if any of the pair falls anywhere in a line of file2 then programme should print "chromosome pos1 pos2 and the matching content of the file1 with values

    output file: chr8 18640000 18960000 ZGLP1 RRP1B 39.62 0.2972 chr8 18640000 18960000 ZGLP1 CDH24 51.21 0.2560 chr8 18640000 18960000 RRP1B CDH24 20.8 1 chr8 19000000 19080000 BBCDI DND1 19.44 0.2833 chr8 19000000 19080000 BBCDI SOHLH2 36.61 0.2909 chr8 19000000 19080000 DND1 SOHLH2 18 0.8

    so far I have tried this but it is taking so much time as my input files are huge (2gb).

    my perl code open( AB, "file1" ) || die("cannot open"); open( BC, "file2" ) || die("cannot open"); open( OUT, ">output.txt" ); @file = <AB>; chomp(@file); @data = <BC>; chomp(@data); foreach $fl (@file) { if ( $fl =~ /(.*?)\s+(.*?)\s+(.*?)\s+(.*)/ ) { $one = $1; $two = $2; $thr = $3; $for = $4; } foreach $line (@data) { if ( $line =~ /(.*?)\s+(.*?)\s+(.*?)\s+(.*)+/ ) { $chr = $1; $pos1 = $2; $pos2 = $3; } if ( $line =~ /$one/ ) { if ( $line =~ /$two/ ) { print OUT $chr, "\t", $pos1, "\t", $pos2, "\t", $fl, " +\n"; } } } }
what is wrong with this perl script
1 direct reply — Read more / Contribute
by chai6125
on Sep 16, 2014 at 09:34
    Hi Monks, I'm totally confused about what is going wrong here. Here is my Perl code.
    my $COUNT_1; my $parameter1 = 'PU_CLERK'; $COUNT_1 = `sqlplus -s hr/password\@dbname\@sql_script.sql $parameter1 +`; $COUNT_2 = `sqlplus -s hr/password\@dbname\@sql_script1.sql $parameter +2`; if ($COUNT_1 == $COUNT_2) { then do something }

    I'm basically trying to pass a parameter to my sql script from perl namely $parameter1 which is how we do in a typical unix script. Here is a snippet of my oracle script. '&1' is my parameter.

    select count(*) from employees where job_id <> '&1' and salary > 9000 and commission_pct is not null order by first_name desc / exit;

    But my oracle instead of returning a number is returning a error string.

    But when i don't use the parameter and hardcode everything works fine.

    my $COUNT_1; my $parameter1 = 'PU_CLERK'; $COUNT_1 = `sqlplus -s hr/password\@dbname\@sql_script.sql`; $COUNT_2 = `sqlplus -s hr/password\@dbname\@sql_script1.sql`; if ($COUNT_1 == $COUNT_2) { then do something }
    select count(*) from employees where job_id <> 'PU_CLERK' and salary > 9000 and commission_pct is not null order by first_name desc / exit;
    So my question is how to pass arguments properly to oracle. Where am i going wrong :( Please ignore the + sign I don't know how to remove that
count number of overlapping words in a document
1 direct reply — Read more / Contribute
by dmarcel
on Sep 16, 2014 at 09:26

    I am beginning perl user, so I'm sorry in advance if I miss some easy things, but I cannot get it to work and I am not sure whether every step is necessary(I have problems in particular with the while loops and 'saving' its outcome). I would like to do the following:

    I have two text files. File 1 with a word list (one word per line) and File 2 that is a regular text document with text and numbers. I would like to count the number of words in file 2 that I can also find in file 1 and the total number of words in the document (so that I can calculate the percentage of corresponding words)

    this is how far I have come:

    use strict; use warnings; #Part one of the code, the wordlist is a file with one word per line a +nd I transform this into a hash my $filename = "wordlist.txt"; open(INPUT, $filename) or die "Cannot open $filename"; my $line = <INPUT>; while($line = <INPUT>){ chomp($line); my @words = split(/\s+/, $line); my %unique = map {$_ => 1 } @words; my @unique = %unique; #part two of code, open the text file and extract words only (because +the file also includes many numbers), and count the number of occuren +ce stored in a hash open (DATA, "4.txt") or die; my @UnNum; my $x; my %dict; while (<DATA>) { chomp; $_ = lc; s/ -- / /g; s/ - / /g; s/ +/ /g; s/[.,:;?"!_()\[\]]//g; my @UnNum = split(/\s+/); foreach $x(@UnNum){ if ($x =~ /(([a-zA-Z']+-)*[a-zA-Z']+)/ ){ ++$dict{$x}; }}} #part 3 of the code, I try to compare the two different hashes and add + the total number of occurrences while ((my $words,my $number) = each (%dict)) {my $total+= $number; if (exists($unique{$words})){ my $corresponding +=$number; print "There are $corresponding corresponding words of in total $total + words";}} }

    thank you in advance for the help!

AI Neural Networks based Prediciton
1 direct reply — Read more / Contribute
by kulls
on Sep 16, 2014 at 09:04
    I am new to Artificial Intelligence and studying Neural Networks. Can anyone suggest me how to do a prediction program through machine learning, especially stock market data.
    Please suggest me some example.
    Raja K
Can we write perl script to get file transfer rate and time elapsed of a file copy
2 direct replies — Read more / Contribute
by narravijayender
on Sep 16, 2014 at 08:51
    Looping in a Loop
    5 direct replies — Read more / Contribute
    by Violet
    on Sep 16, 2014 at 08:42

      I am completely new to Perl and would like some feedback/assistance. Hoping I don't make anyone cringe too badly.

      Trying to open a list and use each item on the list to bring out information on that item from the open file. Items will be listed in the second column of the file, but if the item matches I need to print all columns for that file.

      When I list all the items out directly in my code instead of opening the list, the code works. However, when I add the element of opening the list, I get no readout whatsoever.

      There are no warnings, so I can't figure out what's wrong. The format of my list maybe?

      #! C:\Perl\bin\perl -w open (LIST, "List.txt" ); @mylist = (<LIST>); open (FILE, "FILE.txt" ); @myfile = (<FILE>); foreach $file(@myfile) { foreach $list(@mylist) { @temp = split (/\t/, $file); chomp @temp; if ( $temp[1] eq $list ) {print ("$file")} } }

    Add your question
    Your question:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (4)
    As of 2014-09-18 04:01 GMT
    Find Nodes?
      Voting Booth?

      How do you remember the number of days in each month?

      Results (107 votes), past polls