Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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
Manipulating tab delimited file
2 direct replies — Read more / Contribute
by andyBio
on Apr 28, 2016 at 21:54

    Hello, wise ones! I am trying to read in a file with content like this:

    It is a tab demilimited file. I want to manipulate the file to have it look like this:
    (i). Sequences(column 0) with length less than 15 or greater than 30 are ommitted (ii). Sequences with counts (Column 1) < 2 are ommitted (iii). the print statement takes uses an argument and the count for the header line. Here is my code:

    #!/usr/bin/perl -w use strict; use warnings; my $species=$ARGV[0]; my $input=$ARGV[1]; my @fields; my $n = 0; open my $tabdata, '<', "$input" or die ("Can't open $input\n"); while (my $line = <$tabdata>) { foreach my $line ($tabdata){ my @fields = split("\t",$line); if(($fields[1] > 2) && (length($fields[0]) > 14 && length($fields[ +0]) < 31)) {print ">$species" . $n++ . "_count=$fields[1]\n$fields[0]\n"} +; } } close ($tabdata);
    Here is the error: a> line 1. Use of uninitialized value $fields[1] in numeric gt (>) at line + 19, <$tabdata> line 2. Use of uninitialized value $fields[1] in numeric gt (>) at line + 19, <$tabdata> line 3. . . . Use of uninitialized value $fields[1] in numeric gt (>) at line + 19, <$tabdata> line 21.

    I'd appreciate some assistance on how to get this to work! Thank you.

Pronouncible TLA's?
3 direct replies — Read more / Contribute
by BrowserUk
on Apr 28, 2016 at 17:36

    Is there a metric by which the pronounceability of of a combination of letters can be judged? And is there anything that Perl can do to measure it?

    Pronounceable by whom is a variable, xkz might be pronounceable in Czech or Polish; but since I am chronically monoligual; let's stick to 'by native English speakers'.

    That still leaves plenty of scope for sounds made by the native English speakers from some parts of the world that are totally unreproducible by those from others; but its a place to start.

    To be useful, the pronunciations would need to be distinct; and changing the pronunciation of known word, to enable a less common spelling to (re)use that sound defeats the purpose.

    Ie. Deciding to pronounce 'are':ah-rey, so that 'aar' can be pronounced as ah doesn't work.

    Also, I think that the Dutch could intone a difference between 'six' and 'syx' and hear the difference; but I'm pretty sure I can't do either.

    For the 'Perl content' and 'what have you got' criteria, this produces all 17,576 3-letter combinations. Can you add code to eliminate the unpronounceable?:

    use Algorithm::Combinatorics qw[ variations_with_repetition ];; $i = variations_with_repetition( [ 'a'..'z' ], 3 );; $" = ''; print "@$_" while $_ = $i->next;; aaa aab aac aad ...

    I thought perhaps that any combination that contained a vowel or y might work:

    $" = ''; $_ = join( '', @$_ ), m[[aeiouy]] and print while $_ = $i->ne +xt;;

    But I don't think any of these would qualify: xxa xxe xxi xxo xxu xxy xza xze xzi xzo xzu xzy.

    And there is a distinguish-ability problem with things like gga gja jga & jja. Again the Dutch might get something like kh-yah & ye-gah for the middle two, but I probably wouldn't be able to pronounce either to their satisfaction.

    The application, albeit a light-hearted speculation rather than yet a serious pursuit, is the Huffman encoded (I really just mean 'short' here) naming of a few (3-7?) thousand items (variables; values) in a phonetically pronounceable way, for clarity of verbal communication, and memorability.

    And why not just use numbers? The hope is that at least some of the more common items could be named in some vaguely mnemonic way -- to aid in the second of the above goals -- in a way that "Item 237' never will.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Suppress 'Can't chdir to' warnings for File::Find
2 direct replies — Read more / Contribute
by mabossert
on Apr 28, 2016 at 12:03

    I am writing some prototype code for a larger application that needs to search all directories from a particular starting point and return all directories that contain certain types of files. The function works fine excepts for producing warnings related to not being able to chdir to directories for which the user who runs the application does not have sufficient permissions to read.

    Perhaps I am just being pickier than I should, but the application needs to traverse directories that belong to the user and also ones that do not belong to the user, but for which the user has sufficient permissions to read. When I run the test code, everything works as expected, but produces warnings. They are expected, but I would like to suppress them or "tell" File::Find to skip any directories that the user does not have permissions to access. On the other hand, maybe there is a way to tell File::Find to first test if it can read the directory and then move on if it cannot?

    Here is my current code. Any suggestions would be greatly appreciated.

    #!/usr/bin/env perl use strict; use warnings; use 5.016; use Carp qw(cluck carp croak); use JSON qw( encode_json ); use File::Find; use Data::Dumper; my %seen; my @files; find ({ wanted => \&wanted }, '/mnt/lustre'); say Dumper(\@files); sub wanted { if($File::Find::name =~ /\.nt$|^dbQuads$|^$|^string_table_ +chars.index$|^string_table_chars$/ && !exists $seen{$File::Find::dir} +) { my $user = (getpwuid ((stat $File::Find::dir)[4]))[0]; my $name = $1 if $File::Find::dir =~ /\/([^\/]+)$/; #=+ Would like to know how big the directory contents are my $raw_size = 0; my $db_size = 0; my $built = 0; find(sub { if(-f $_ && $_ =~ /\.nt$/) { $raw_size += -s $_; } elsif(-f $_ && $_ =~ /^dbQuads$|^string_table_chars.index$|^stri +ng_table_chars$/) { $db_size += -s $_; $built = 1; } },$File::Find::dir); my %temp = ( owner => $user, raw_size => $raw_size, db_size => $db_size, name => $name, path => $File::Find::dir, built => $built ); push @files, \%temp; $seen{$File::Find::dir} = 1; } }
Obtaining combinations of hash keys and values
2 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 28, 2016 at 10:00

    I have a short script which splits a DNA-sequence (imported by BioPerl) by a user-provided pattern and subsequently populates a hash with modified versions of the resulting fragments along with a unique ID. Each fragment produced by split appears in the hash twice - in the original form and in a reverse complement form (F and R respectively).

    #!/usr/bin/perl use strict; use warnings; use Bio::SeqIO; my %sequences; my $seqio = Bio::SeqIO->new(-file => $ARGV[0]); my $enz = $ARGV[1]; while(my $seqobj = $seqio->next_seq) { my $id = $seqobj->display_id; my $seq = $seqobj->seq; $sequences{$id} = $seq; } my @fragments; for my $value (values %sequences) { @fragments = split(/$enz/, $value); } my $Lfill = "TT"; my $Rfill = "AA"; my $ID = 0; my %bins; foreach my $RF (@fragments) { $ID++; $RF = $Lfill.$RF.$Rfill; $bins{$ID."F"} = $RF; (my $rev = $RF) =~ tr/ACGT/TGCA/; $bins{$ID."R"} = reverse($rev); }

    Example Input:


    Fragments Generated:


    I now want to obtain unique combinations (of two) whilst excluding a fragment from combining with its own reverse cognate. Using the example above, it would produce:


    But would not produce 1F1R or 2F2R. As shown above, both the keys of the involved fragments are combined as well as the values - and stored in a new hash.

    I've tried using the CPAN modules Algorithm::Combinatorics and Math::Combinatorics but can't seem to adapt these to fit this task.

    Does anybody have any code snippets, examples or suggestions that could help achieve this? If it helps: i'm very new to Perl.

Fall through loop
3 direct replies — Read more / Contribute
by b4swine
on Apr 28, 2016 at 09:33

    helloed monks,

    Often I need to distinguish between a loop that ran to completion, and one that was aborted due to a last statement. For example, if I want to check if every element in a list is less than 0.5, I might write

    my @list = (rand(1), rand(1), rand(1)); my $less=1; for (@list) { $less=0, last if $_>=0.5; }

    At the end of this, $less will be true if all the members of the list were small, and false otherwise. I understand that this could have been done via grep in clever ways, but this is just an example. If we are doing a loop, is there a cleaner way to distinguish running a loop to completion from aborting via a last. If not, it would be a nice feature to have a system variable (or some better way), where one could write

    for (@list) { last if $_ < 0.5; } my $less = $loop_ran_to_completion;

    After thinking about it some more, here is an ugly looking solution, but without any temporary variables:

    use Data::Dump; @list = (rand(1), rand(1), rand(1)); dd \@list; OUT: { ANY: { for (@list) { next ANY if $_ > 0.5; } # if all elements are small we come here print "All Small\n"; next OUT } # if any element is big we come here print "Some Big\n"; }
Xpath with Selenium
1 direct reply — Read more / Contribute
by Chaoui05
on Apr 28, 2016 at 08:24
    Hello Monks !! I have this following issue: i would like to get 2 input from my web page.But i have the same xpath for both. I used for that Developper Tools. I can get the first but not the second. And i wrote that in my code :

    for first:

    $elem = $driver->find_element('//input[1][@class="dx-texteditor-input"]')->send_keys("Live");

    for second:

    $elem = $driver->find_element('//input[2][@class="dx-texteditor-input"]')->send_keys("Live");

    And with Inspector chrome,i have thoses xpath :





    I Think i have to use 'id' but i don't know how

    Thanks !!
Restrict file search within current filessystem using wanted subroutine
2 direct replies — Read more / Contribute
by madparu
on Apr 28, 2016 at 07:41

    I found the following code in Perl Monks to search for top 10 largest files in a filesystem. But it searches in all sub directories of different filesystem. Example., I have /var and /var/log filesystems, my requirement is to search top 10 files under /var but the code searches through /var/log too. is there a way to restrict the search with teh current filesystem alone with this script using File:find and warning subroutine?

    use File::Find; File::Find::find( { wanted => sub { return unless -f; my $s = -s _; return if $min < $s && @z > 10; push @z, [ $File::Find::name, $s ]; @z = sort { $b->[1] <=> $a->[1] } @z; pop @z if @z > 10; $min = $z[-1]->[1]; } }, shift || '.' ); for (@z) { print $_->[0], " ", $_->[1], "\n"; }
What is -w?
2 direct replies — Read more / Contribute
by htmanning
on Apr 28, 2016 at 03:04
    Sorry for the dumb question. I have a script like many others, but unless I add a -w at the top it returns an Internal Server Error. I find nothing special about this script. It searches a database and returns some html. #!/usr/bin/perl -w Without that -w it won't run. What is that switch?
How to convert a string like this: "\321\201\321\202\321\200\321\203\320\272\321\202\321\203\321\200\320\260" to a proper utf8?
1 direct reply — Read more / Contribute
by igoryonya
on Apr 28, 2016 at 01:50

    It contains this string: "структура".

    I am lost, can't find how to do it.
    I guess, I have to use pack or regex, but I don't know where to start. I don't even know, what this encoding is called, although, I saw it before many times.


    I've figured it out. I realized that those 3 digit sequences are not decimal, but octal, so I've tried:
    my $string = "\321\201\321\202\321\200\321\203\320\272\321\202\321\203 +\321\200\320\260"; $string =~ s/\\([[:digit:]]{3})/sprintf "%c", oct($1)/ge; print $string;
    And it Worked! :)
Error Message - PL_perl_destruct_level at /usr/lib64/perl5/
3 direct replies — Read more / Contribute
by NorCal12
on Apr 28, 2016 at 00:45

    I am new to this forum and certainly not a Perl expert.

    I have a website that is an auction for the commercial fishing industry. It has been up and running since 2011. It is currently located on a shared hosting site and I am in the process of moving it to a VPS site on another company's server. I have move all the files and database to the new location and I have been testing everything before I have the DNS pointed to the new location. For the most part everything looks good. I have an archive section, where a user can look at tables of past sales. These are generated from data stored in a mysql database. The display of past sales works fine.

    However, when I test a new sale I get an error when that sale is being inserted into the database. For speed in testing I have been using a "buy-it-now" feature rather than have to wait for an auction to end.

    The code begins as below:

    #!/usr/bin/perlml BEGIN { my $base_module_dir = (-d '/home/jeffer36/perl' ? '/home/jeffer36/per +l' : ( getpwuid($>) )[7] . '/perl/'); unshift @INC, map { $base_module_dir . $_ } @INC; unshift @INC, '/home/jeffer36/perl5/lib/perl5','/home/jeffer36/perl5/ +lib/perl5/x86_64-linux','/home/jeffer36/perl5/lib/perl5/x86_64-linux/ +Bundle'; } use POSIX qw(strftime); use File::Copy; use strict; use CGI; use CGI::Session; use CGI::Carp qw(fatalsToBrowser); use File::CounterFile; use Data::Dumper; use DBI; #use DBD::mysql;

    When the sale is being inserted into the database this is the error message:

    install_driver(mysql) failed: Can't load '/home/jeffer36/perl5/lib/perl5/x86_64-linux/auto/DBD/mysql/' for module DBD::mysql: /home/jeffer36/perl5/lib/perl5/x86_64-linux/auto/DBD/mysql/ undefined symbol: PL_perl_destruct_level at /usr/lib64/perl5/ line 200, <BUYERFILE> line 88. at (eval 16) line 3 Compilation failed in require at (eval 16) line 3, <BUYERFILE> line 88. Perhaps a required shared library or dll isn't installed where expected at ../auction/ line 284</P.

    Line 284 in is:

    my $dbh = DBI->connect("DBI:mysql:$db:$server", $userid, $passwd);

    I have search the web for information on this error message and have come up empty. Does anyone have suggestions on how to correct the problem?

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 pondering the Monastery: (3)
    As of 2016-04-29 05:40 GMT
    Find Nodes?
      Voting Booth?
      :nehw tseb si esrever ni gnitirW

      Results (438 votes). Check out past polls.