Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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
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$|^graph.info$|^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:

    >example AAGTAGCATCGATTTATAGCATCGACTAGTAAGCTTAGCTACGATCAGCTACGATCGAGCGACTACGTAG +C

    Fragments Generated:

    1F => TTAAGTAGCATCGATTTATAGCATCGACTAGTAA 1R => TTACTAGTCGATGCTATAAATCGATGCTACTTAA 2F => TTAGCTACGATCAGCTACGATCGAGCGACTACGTAGCAA 2R => TTGCTACGTAGTCGCTCGATCGTAGCTGATCGTAGCTAA

    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:

    1F2F => TTAAGTAGCATCGATTTATAGCATCGACTAGTAATTAGCTACGATCAGCTACGATCGAGCGA +CTACGTAGCAA 1F2R => TTAAGTAGCATCGATTTATAGCATCGACTAGTAATTGCTACGTAGTCGCTCGATCGTAGCT +GATCGTAGCTAA 1R2F => TTACTAGTCGATGCTATAAATCGATGCTACTTAATTAGCTACGATCAGCTACGATCGAGCGA +CTACGTAGCAA 1R2R => TTACTAGTCGATGCTATAAATCGATGCTACTTAATTGCTACGTAGTCGCTCGATCGTAGCTG +ATCGTAGCTAA

    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 :

    First:

    //*[@id="parameters"]/div/div[4]/div[2]/div/input

    Second:

    //*[@id="program_logs"]/div/div[4]/div[2]/div/input

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

    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.

    UPDATED:

    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/DynaLoader.pm
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/mysql.so' for module DBD::mysql: /home/jeffer36/perl5/lib/perl5/x86_64-linux/auto/DBD/mysql/mysql.so: undefined symbol: PL_perl_destruct_level at /usr/lib64/perl5/DynaLoader.pm 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/buyit.pl line 284</P.

    Line 284 in buyit.pl 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?

Print keys corresponding to duplicate values
5 direct replies — Read more / Contribute
by amar712
on Apr 27, 2016 at 18:01
    I have a hash %hash = { 'a' => 1, 'b' => 2, 'c' => '1', 'd' => 3, 'e' => '5' } How can I return the keys with the same value. In the above example I want a,c.
The process cannot access the file because it is being used by another process?[Problem disappeared!]
6 direct replies — Read more / Contribute
by BrowserUk
on Apr 27, 2016 at 17:44

    Update!

    Before I posted, I tried a bunch of things and ran it at least a dozen times getting the exact same failure. I the cut the script of the top of the data, renamed the file .dat and stuck the script into a new file of the old name. I changed <DATA> to <> and supplied the data file on the command line and it worked first time.

    I've just recreated the all in one script; and now it runs perfectly. I have no explanation for the error or the cure; but I suspect 1nickt called it.

    Thanks for your help guys.


    I'm getting this error when I try to run the following code:

    C:\Motor>parseAns.pl The process cannot access the file because it is being used by another + process.

    Another process is accessing the DATA pseudo-handle?

    The data is large 2.4 million, but I'm sure I've processed much larger datasets from <DATA> before; and I've commented out the storing of the data to the array, so it isn't running out of memory. Cluebats anyone?

    #! perl -slw use strict; my @data; my $i = 0; #$data[ $i++ ] = [ split ' ' ] ++$i while <DATA>; print $i; __DATA__ -69.282032302755084 40.000000000000014 0 -1 -69.123493781255831 39.908467741935496 -1.4443382565142906e-006 -1 -68.748013135538145 40.911009397420145 0 -1 -68.964955259756593 39.816935483870985 -2.990721348858345e-006 -1 -68.370049396495517 40.298149116372635 -6.3944096502804299e-006 -1 -68.202015544462682 41.814890597403163 0 -1 ... 2.4 million lines omitted.

    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.

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!
  • 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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (11)
    As of 2016-04-28 20:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      :nehw tseb si esrever ni gnitirW







      Results (435 votes). Check out past polls.