Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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
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

    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 for testing purposes in the directory C:\MyDir:
    print "bar\n";

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

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


    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 );


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

    Neil Watson

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


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


    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?
flip flop operator and if statement
3 direct replies — Read more / Contribute
by iThunder
on Sep 19, 2014 at 22:32


    Can someone please help that how value of p is coming out as 1 through 17.

    My understanding is p should be equal to $. (line number) and it should be printed from 3 to 19 rather 1 to 17.

    Thanks in advance!

    #!/usr/bin/perl ##use warnings; while(<>){ if($p=3..1) { print "p is $p\n"; } }

    When i run the same code on a file that has fifteen lines, it returns following

    p is 1

    p is 2

    p is 3

    p is 4

    p is 5

    p is 6

    p is 7

    p is 8

    p is 9

    p is 10

    p is 11

    p is 12

    p is 13

    p is 14

    p is 15

    p is 16

    p is 17

    Also what is difference when i have $p=3..1 vs ($p=3)..1 in if statement above

How to convert negative and positive floating points to binary and vice versa
2 direct replies — Read more / Contribute
by thanos1983
on Sep 19, 2014 at 22:32

    Hello Monks,

    I have managed to convert positive and negative decimal numbers to binary and vice versa.

    Sample of code:

    I am trying to figure out how to convert positive and negative floating points. For example if I had a number -0.000008 how could I convert the number in binary and back to floating.

    I have read on the pack documentation that: F A Perl internal floating-point value (NV) in native format

    When I am modifying on my code the "c" to "F" I get the following error:

    So I assume I am going terribly wrong some where. What I am missing?

    Thank you in advance for your time and effort.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
School assignment
3 direct replies — Read more / Contribute
by mb92
on Sep 19, 2014 at 20:07

    I need to write a perl script that searches for a file from standard input and then opens the file along with line numbers to the left of every line.. Been sitting for hours but haven't got it to work. Tips?

    I managed to get it work with the help of: system("find -type f -iname '$filename' -print -exec cat -n {} +"); but that is not pure perl code.
Faster indexing an array
5 direct replies — Read more / Contribute
by wollmers
on Sep 19, 2014 at 16:48

    Dear monks,

    as this part takes ~15% of an fast alignment algorithm, it would be nice to have a faster way.

    my $Y = [ qw( a b a) ]; my $YPos; my $index; for ( $index = 0 ; $index <= $#$Y ; $index++ ) { push ( @{ $YPos->{$Y->[$index]} }, $index ); } # now $YPos should contain my $result = { a => [ 0, 2 ], b => [ 1 ], };

    TIA (Thanks In Advance)

    Helmut Wollmersdorfer

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 taking refuge in the Monastery: (3)
    As of 2014-09-21 06:44 GMT
    Find Nodes?
      Voting Booth?

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

      Results (167 votes), past polls