Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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
Recommendations for perl modules to work on data sets ?
4 direct replies — Read more / Contribute
by raghuprasad241
on Oct 21, 2016 at 11:06
    Hello monks,

    I am trying to find modules for some advanced data mining projects in perl. In python they have iterator module with functions like pairwise etc. that does the task for me. However I need to do this perl. So trying to avoid reinventing the wheel. I am going to describe a sample problem so that it will give you an idea for any recommendations you may provide. I have a data set like below...

    10/01/2016 99.71 10/02/2016 99.53 10/03/2016 100.10 10/04/2016 100.96 10/05/2016 100.99 10/06/2016 101.38 10/07/2016 100.74 10/08/2016 100.70 10/09/2016 99.88 10/10/2016 97.62 10/11/2016 97.55 10/12/2016 99.12
    Now I need to find 5 consecutive records either increasing or decreasing by value next to date field. For e.g. I need the following 2 result sets in my output.
    10/02/2016 99.53 10/03/2016 100.10 10/04/2016 100.96 10/05/2016 100.99 10/06/2016 101.38 10/07/2016 100.74 10/08/2016 100.70 10/09/2016 99.88 10/10/2016 97.62 10/11/2016 97.55
    Please note that I am looking for exactly 5 consecutive records with either increasing or decreasing patterns, any other number of consecutive records with those patterns should not qualify.

    I know this is a little advanced for me to write from scratch given my perl experience. Appreciate any inputs on the modules that I may use to accomplish this task or atleast any ideas to write the code efficiently given there are high volumes of records in each file.

    Again I am not expecting one of you to write code for me although I would not complain about it :-)

    Thanks! Monk
How to concatenate utf8 safely?
1 direct reply — Read more / Contribute
by gregor42
on Oct 21, 2016 at 10:22

    I am humbled and seeking help.

    This concerns data containing names so getting it Right is important.

    It is likely that I am fundamentally missing something when it comes to safely concatenating strings.

    A hand-rolled point solution sometimes works as intended and others times results in the dreaded:

    Wide character in syswrite

    I assume that the problem is my code and not the data coming in since one can usually depend on people to get their own names right.. But then i18n characters are tricksy, like Hobbits...

    sub jibe { my($s,$t) = @_; my $r = join('', (is_utf8($s)?$s:decode('utf8',$s)), (is_utf8($t)?$t +:decode('utf8',$t))); return $r; }

    To give it context, let's say that we are creating common name from given name plus surname: (Anglo-centric, I know...)

    my $cn = jibe(jibe($givenname," "),$sn);

    Thank you in advance for any nudges in the right direction that anyone might provide.

    Wait! This isn't a Parachute, this is a Backpack!
Get latest file created or modified & matching part of file name :
1 direct reply — Read more / Contribute
by rahulme81
on Oct 21, 2016 at 09:49

    Hello Monks

    I have a directory with a bunch of files. I'm trying to find the latest file created or modified by time in a directory and using the following pattern

    opendir(my $DIRH, $DIR) or die "Error opening $DIR: $!"; my @files = map { [ stat "$DIR/$_", $_ ] } grep( ! /^\.\.?$/, readdir( $DIRH ) ); #This find me +all files not have dot in directory #How this grep can be accommodate for my regular expre +ssion ????? closedir($DIRH); sub latestFile { $b->[0]->ctime <=> $a->[0]->ctime } my @latest_files = sort latestFile @files; my @latest = @{$latest_files[0]}; my $name = pop(@latest); print "Latest file created fro $name\n";

    This is giving the latest file in the directory, but not the file which i need as per my regular expression.

    I need to parse the file and do something, which eventually I am able to achieve

    Facing difficulty with file name pattern match and get the latest file

Module for form validation and resubmission
1 direct reply — Read more / Contribute
by Dallaylaen
on Oct 21, 2016 at 07:33

    Hello, dear esteemed monks!

    tl;dr: I'm thinking of a module that (1) compiles a set of validation rules once and (2) for each hashref given to it later, returns another object containing valid data, errors, AND initial input for processing and/or resubmission.

    Is there such a module already? If not, should I roll my own? If yes, does the API described below seem sensible?

    Now the whole story. Some time ago there was a discussion here at Perlmonks pointing out that it would be nice to make create button act as preview if post content was edited. Can't find proofs now, but the idea impressed me much.

    Recently I started looking for a form validator. My initial idea was to build a permanent validator object once and keep throwing inputs at it later. And I found a module with almost the interface I imagined - Validator::LIVR.

    However, after trying to actually implement form validation & resubmission, I found myself juggling 3 hashrefs (valid data, errors, raw user input for reentry). This was not very convenient, so I decided to pack them into one object, adding is_valid() method on top. And I found a module on CPAN with almost the interface I imagined (but without the "compile once" part) - Data::CGIForm.

    So I crossed these two and got roughly the following API:

    # initialization # the hash describes regexps, requiredness, and other checks # per input key my $validator = My::Class->new ( { ... } ); # later when processing request my $form = $validator->validate( { get => "params" } ); if ($form->is_valid) { do_something( $form->data ); redirect( "/somewhere" ); } else { show_form_again( display_errors => $form->error, input_defaults => + $form->raw ); };

    Also error content (and thus is_valid return value) can be modified, just like in Data::CGIForm:

    if (!load_user($form->data->{user})) { $form->error( user => "No such user in database" ); };

    Unfortunately, the Data::CGIForm has a fatal flaw: error() without arguments acts like my is_valid(), while separate errors() method returns all errors as hash. Error/errors is prone to typoes in my opinion, but maybe I should shut up my ego and stay compatible to an existing API instead?

    Thank you

Automating site having iframes using Selenium
2 direct replies — Read more / Contribute
by 9mohit2
on Oct 21, 2016 at 07:28

    Hi can someone please help me with some documentation for working with site having iframes using selenium in Perl. I am using Selenium::Chrome module. But I am not able to get elements inside the iframes also not getting syntax to switch back to default page content.

write to Disk instead of RAM without using modules
3 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 21, 2016 at 07:04
    I am trying to compare multiple files simultaneously for similar pattern search. I have multiple files with each file of size 500MB approximately. I have written a script which works well when I run with some of my files but when I increase the number of files, script hangs. I think it is due to memory limitation. Is there any other way of storing hash in Disk rather than RAM without using any of perl module.
First word
2 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 21, 2016 at 07:00

    I would like to get the first word from each row of my @AoA[][] and push it into the array @kmers. Can anybody help me get there? This is what I've done so far. Thanks in advance everybody!

    my(@kmers); my($x) = 0; my($y) = 0; for ($x=0, $x<7, $x++){ for ($y=0, $y<$AoA[$x], $y++){ my($firstword) = ''; $firstword = $AoA[$x][$y] =~ /^(\w+)/; push @kmers, $firstword; print $firstword."\n"; } }
Filter array of hashes for specific keys
1 direct reply — Read more / Contribute
by ddominnik
on Oct 21, 2016 at 05:35

    Say I have an array of hashes like this:

    @response = [ { 'name' => 'foo', 'id' => 'bar', 'self' => 'baz' }, { 'name' => 'foo2', 'id' => 'bar2', 'self' => 'baz2' }, ]

    Now I have a subroutine called filter which I call like this:

    my $responseref = \@response; my $filtered_response = filter ($responseref, 'name', 'id');

    How would the subroutine need to look to get an array reference as return value of which the array would look like:

    @response = [ { 'name' => 'foo', 'id' => 'bar', }, { 'name' => 'foo2', 'id' => 'bar2', }, ]

    Ideally it should work without installing any additional modules
    It should also be pretty generic, it should for example be able take a different amount of fields to filter through every time

crawl a full webpage one time
2 direct replies — Read more / Contribute
by liuweichuan
on Oct 21, 2016 at 03:56

    using Perl, how to get a full web page (include ajax web) once like phantomjs ? And what kind of brower useragent can do this?

Batch FIle Operations
4 direct replies — Read more / Contribute
by ankit.tayal560
on Oct 21, 2016 at 01:29

    Hi Monks, This is not a query related to some specific code/script but in a general scenario. suppose if I am having a script in which I have defined several (3-5) paths. I want to run this script through a batch file so I make a batch file.

    perl @pause

    Now if I want to pass the paths required through batch file only what should I do how can I pass paths through batch file directly into my script. any help or suggestions is appreciated.

    Thanks in advance

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 about the Monastery: (5)
    As of 2016-10-21 22:06 GMT
    Find Nodes?
      Voting Booth?
      How many different varieties (color, size, etc) of socks do you have in your sock drawer?

      Results (291 votes). Check out past polls.