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
Why isn't this simple method to join elements of 2 arrays not working
2 direct replies — Read more / Contribute
by Axlex
on Sep 21, 2014 at 10:35
    Hello wise Monks; i am trying to concatenate the lines of 2 files and came up with this simple code
    my (@In, $In, @Out, $Out, $InFile, $OutFile); + # declare variables open $InFile, "<", "e:/File1.txt"; # open the exported +file in read mode open $OutFile, ">>", "e:/File2"; @In = <$InFile>; @Out = <$OutFile>; close $InFile; close $OutFile; for my $k ( 0 .. $#In ) { $Out[$k] = "$Out[$k],$In[$k]"; } open $OutFile, ">", "e:/File2.txt"; # open the outfile, + deleting all content (writemode) print $OutFile @Out; close $OutFile;

    but i am getting the error: "Use of uninitialized value within @Out in concatenation(.) or string at join.pl"

    I know, my programming skills are bad but please, i dont know any other way.

CGI SCRIPT ITSELF IS DISPLAYED
3 direct replies — Read more / Contribute
by anikng
on Sep 21, 2014 at 03:46
    Hellow Monks, I saw some posts similar to my following doubt. But i couldnt figure it out. So kindly help me (am not an expert). I have a front end html (with buttons) and a backend PERL cgi script. The code is correct and tested in another windows system. Now, i placed two files in www folder (under var) in Ubuntu 14.04. But when i clcik the button, instead of runing the cgi, script itself is displayed. Someone kindly help me what changes to be made (in a simple way). I also attach the contens of front end html and backend test.cgi

    Front end.html <html> <body> <h3> <center>TRANSCRIPTION FACTOR FAMILY</center></h3> <form action="/var/www/test.cgi" method="GET"> <input type="submit" value="ABC"> </form> </body> </html>

    test.cgi,

    #!/usr/bin/perl use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); $q=new CGI; print "Content-type: text/html\n\n"; @field=$q->param(); $name=$q->param("value"); print<<"OUTPUT"; <html> <head> <title> FAMILY SELECTION </title> </head> + <body> <img src="/var/www/bb.jpg" size=100"> <br><br><br><br><br> <center><table border=0> <tr><td> </td></tr> </table></center> </body> </html> OUTPUT
    Thanks, anikng
Extract table from a block of text
1 direct reply — Read more / Contribute
by reaper9187
on Sep 21, 2014 at 03:01
    Hello Monks, I have a text file which contains a lot of information, most of it unimportant. However, there is a block of text that I need to parse which contains information in a tabular format as shown below.
    INFO START STIME ETIME COLUMN3 COLUMN4 COLUMN5 aaaa1 bbb1 ccc1 ddd1 eee1 aaaa2 bbb2 ccc2 ddd2 eee2 aaaa3 bbb3 ccc3 ddd3 eee3 aaaa4 bbb4 ccc4 ddd4 eee4 END
    The sample output should be a table(hash maybe ?) that maps each element to its corresponding row as follows:
    aaaa1:bbb1 ccc1 aaaa1:bbb1 ddd1 aaaa1:bbb1 eee1 aaaa2:bbb2 ccc2 aaaa2:bbb2 ddd2 aaaa2:bbb2 eee2 aaaa2:bbb3 ccc3 aaaa2:bbb3 ddd3 aaaa2:bbb3 eee3
enumerating values for slopes
2 direct replies — Read more / Contribute
by Datz_cozee75
on Sep 20, 2014 at 23:46

    Hello monks,

    I'm in the middle of a window project, and it's the part where I'm supposed to make the field measurements to determine the slope of the sill. It's a tricky measurement with significant consequences, and I want to have a list of values to judge from. I typically do this type of thing in fortran, but this just isn't working for me in a variety of ways. There probably isn't anything wrong with it, it's just that I need to have more flexibility, so that I can see the values under differing assumptions. The fortran program I want to replace follows

    $ gfortran -Wall -Wextra -o out marvin1.f90 marvin1.f90:4.36: real :: rise, run, theta, deg_to_rad, rad, pi 1 Warning: Unused variable 'deg_to_rad' declared at (1) $ ./out pi is 3.14159274 rise is 0.421622515 $ cat marvin1.f90 program main implicit none real :: rise, run, theta, deg_to_rad, rad, pi run = 3.0 theta = 8.0 pi = 4.0*atan(1.0) print *,"pi is", pi rad = theta * pi / 180.0 rise = run * tan (rad) print *, "rise is ", rise end program main ! gfortran -Wall -Wextra -o out marvin1.f90 $

    The perl replacement will have a list of values for runs, a list of values for rises, and it will calculate all the permutations and list them in order. This is my modest achievement so far. The values are carefully chosen for this very task:

    $ perl slope1.pl slopes are 0 5 8 11 14 30 90 runs are 2.25 3.25 4 5 $ cat slope1.pl #!/usr/bin/perl -w use strict; use 5.010; my @slope = (0.0, 5.0, 8.0, 11.0, 14.0, 30.0, 90.0); say "slopes are @slope"; my @run = (2.25, 3.25, 4, 5); say "runs are @run"; __END__ $

    One of the reasons I usually revert to fortran is that I don't want to have to re-think getting numbers like pi right. So it is that I will have a math module for perl that will give me pi and degrees_to_radians at the end of this thread. I think there's a lot of ways to achieve these results and was fishing for methods and people's experience with the same material.

    My question then is: how do I solve this problem using perl, whilst generating a *robust* module that will never have me starting from scratch again like this (famous last words)?

How to compare the data of two files (.xml and .html) using perl(regex)?
3 direct replies — Read more / Contribute
by flora
on Sep 20, 2014 at 15:32
    I tried using regex by using foreach loop for .xml files as there are many .xml files and for .html file as it is only one file I just opened, read and closed the directory. But when it comes to searching for a particular pattern in both the files, the code doesn't enter the while/if loop. Can please someone help me.
Where does perl looks for perl skripts [Answered]
2 direct replies — Read more / Contribute
by BluePerl
on Sep 20, 2014 at 14:54
    Hi,

    I found, that perl only looks in
    * the actual directory, or
    * in the path which I provided for the script


    Is it possible to provide something like a PATH environment variable, which tells perl where to look for *.pl skripts?

    I created foo.pl for testing purposes in the directory C:\MyDir:
    print "bar\n";

    And tested here:
    C:\MyDir>perl foo.pl bar C:\MyDir>cd .. C:\>perl foo.pl Can't open perl script "foo.pl": No such file or directory C:\>perl -I C:\MyDir foo.pl Can't open perl script "foo.pl": No such file or directory C:\>set PATH=%PATH%;C:\MyDir C:\>perl foo.pl Can't open perl script "foo.pl": No such file or directory C:\>perl C:\MyDir\foo.pl bar

    As noted above, I would like to start foo.pl by
    C:\>perl foo.pl
    ------
    I carefully readed http://perldoc.perl.org/perlrun.html and found the switch -S, which includes the path environment variable for searching. So
    C:\>perl -S foo.pl
    Works
Convert arrayref to AoH
3 direct replies — Read more / Contribute
by neilwatson
on Sep 20, 2014 at 09:31

    Greetings,

    I have a default array of hashes. I replace the defaults, if they exist, with an arrayref. Is there a more efficient way to do this?

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Defaults my @t = ( { 'label' => 'kept', 'value' => '0', }, { 'label' => 'notkept', 'value' => '0', }, { 'label' => 'repaired', 'value' => '0', }, ); # Get this from another sub my $a = [ [ 'kept', '1', ], [ 'repaired', '3' ] ]; # Overwrite defaults with arrayref for my $i ( @{ $a } ) { for my $t ( @t ) { if ( $i->[0] eq $t->{label} ) { $t->{value} = $i->[1]; } } } warn Dumper( \$a ); warn Dumper( \@t );

    Produces:

    $VAR1 = \[ [ 'kept', '1' ], [ 'repaired', '3' ] ]; $VAR1 = [ { 'value' => '1', 'label' => 'kept' }, { 'value' => '0', 'label' => 'notkept' }, { 'label' => 'repaired', 'value' => '3' } ];

    Neil Watson
    watson-wilson.ca

compare columns in all possible combination
3 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 20, 2014 at 09:21

    Hi! I am trying to compare columns in all possible combination, I have two files one with all possible combination and other with data

    file2

    1 2 3 4 1 2 3 5 1 2 4 5 1 3 4 5 2 3 4 5

    file1

    A B C D E 0 0 0 + 0 + 0 + + + 0 + + + + 0 0 + + 0 + + + + + 0 + + + + + 0 + + 0

    so far I have tried this, but it seems only first line of file2 is getting parsed to while loop

    open (BC,"file2.txt")||die("cannot open"); open (AB,"file1.txt")||die("cannot open"); @file=<BC>; chomp(@file); foreach $fl(@file) { if($fl=~/(.*?)\s+(.*?)\s+(.*?)\s+(.*)/) { $w=$1-1; $x=$2-1; $y=$3-1; $z=$4-1; } while(<AB>) { @data=split("\t",$_); chomp(@data); push(@col1,$data[$w]); push(@col2,$data[$x]); push(@col3,$data[$y]); push(@col4,$data[$z]); } } for($i=1;$i<@col1;$i++) { if(($col1[$i] eq '+') && ($col2[$i] eq '+') && ($col3[$i] eq +'+')) { $j++; } if(($col1[$i] eq '+') && ($col2[$i] eq '+') && ($col4[$i] e +q '+')) { $k++; } if(($col3[$i] eq '+') && ($col2[$i] eq '+') && ($col4[$i] eq ' ++')) { $l++; } if(($col3[$i] eq '+') && ($col2[$i] eq '+') && ($col4[$i] eq + '+')&&($col1[$i] eq '+') ) { $m++; } print $col1[0],"\t",$col2[0],"\t",$col3[0],"\t\t",$j,"\n"; print $col1[0],"\t",$col2[0],"\t",$col4[0],"\t\t",$k,"\n"; print $col4[0],"\t",$col2[0],"\t",$col3[0],"\t\t",$l,"\n"; print $col1[0],"\t",$col2[0],"\t",$col3[0],"\t",$col4[0],"\t",$m," +\n"; }

    desired output

    A B C 1 A B D 1 D B C 3 A B C D 1 A B C 1 A B E 1 E B C 3 A B C E 1 A B D 1 A B E 1 E B D 3 A B D E 1 A C D 3 A C E 2 E C D 4 A C D E 2 B C D 3 B C E 3 E C D 4 B C D E 3
    thank you
array search on mysql result
4 direct replies — Read more / Contribute
by rups
on Sep 20, 2014 at 06:07

    I have a database which contains mobile numbers. How do I write perl script which get all of numbers into array & check new number already exist or not in that array?

    Create Table:** CREATE TABLE consumeruser ( ConsumerId int(10) NOT NULL AUTO_INCREMENT, ConsumerName varchar(45) DEFAULT NULL, ConsumerMobNo varchar(10) DEFAULT NULL, PRIMARY KEY (ConsumerId) ) ENGINE=InnoDB AUTO_INCREMENT=4494 DEFAULT CHARSET=latin1
    #!/usr/bin/perl -w use strict; use DBI; use warnings qw(all); use Getopt::Long; use Pod::Usage; use Text::CSV_XS; my $username = 'root'; # set your MySQL username my $password = 'xxxx'; # set your MySQL password my $database = 'app'; # set your MySQL database name my $server = 'localhost'; # set your server hostname (probabl +y localhost) my $dbh = DBI->connect("DBI:mysql:$database;host=$server", $u +sername, $password) || die "Could not connect to database: $DBI::errstr"; my $CustomerMobileNumber = 9999999; my @MobileNumbers; my $mobileNumberQuery = "select ConsumerMobNo from consumerus +er"; my $sth = $dbh->prepare($mobileNumberQuery); $sth->execute(); while(my @row = $sth->fetchrow_array()){ push @MobileNumbers, $row; if (present){ 9999999 found in array; } else{ 9999999 not found in array; } }
call_pv on CORE:: sub from XS
1 direct reply — Read more / Contribute
by OlegG
on Sep 20, 2014 at 02:36
    I am trying to do this
    call_pv("CORE::socketpair", G_SCALAR);
    And this works excellent on perl 5.20, but fails on perl 5.14 with message "Undefined subroutine &CORE::socketpair called".

    So, is there a way to make such a thing on older perl?

Add your question
Title:
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.