Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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
File::Find stat question
3 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 23, 2014 at 19:45

    Hi Monks

    Could I seek some help on the following code please?

    I'm trying to Stat the pattern matched directory but am coming up with zero. Any help would be appreciated.

    find(\&all, @allpaths); #Open a file called Paths.bak open (LIST, ">$tempfilepath/Paths.bak") or die "$! error trying to + overwrite"; #Foreach line (path found) $p in the array @allpathlisting foreach my $p (@allpathlisting) { print LIST "$p\n"; foreach my $s (@sfiles) { print LIST " $s\n"; } } close LIST; copy $tempallpathsoldfile, $tempallpathsnewfile or die "$! error t +rying to copyfile"; #Sub routine for File::Find. on all locations sub all { #Unless directory skip. return unless -d; # Don't recurse past folders matching the pattern. $File::Find::prune = 1 if /[IPDLMY]\d{8}$/; #Replace / with \ (my $fn = $File::Find::name) =~ tr#/#\\#; #Replace gpfs_data with nas\rdds $fn =~ s/gpfs_data/nas\\rdds/; #Push into the @allpathlisting array any paths matching the patter +n. push @allpathlisting, $fn if /[IPDLMY]\d{8}$/; # How big is it? my $fsize = (stat($fn))[7]; push @sfiles,$fsize; print "$fsize\n"; #print results to the screen, comment this out in production. print "$fn\n" if /[IPDLMY]\d{8}$/; }
Detect piped input
1 direct reply — Read more / Contribute
by igoryonya
on Nov 23, 2014 at 19:35
    I've needed to incorporate a detection of a piped input to my program. Searched the documentation and forums and found and placed the following to my code:
    my $rin = ""; vec($rin, fileno(STDIN), 1) = 1; my $isPiped = select($rout=$rin,"","",1); if($isPiped){ #Do the data processing from pipe }
    I tryed to understand the code, but not really. It works though, when I do:
    some-program|myprogram -parameters
    But, aftera a while, I stumbled on a problem, that on certain cases, it doesn't detect that there is an input, coming from pipe. Maybe, when the system was buisy and the data from pipe was taking a while to come, so, after trials and errors, I came up with the solution:
    my $rin = ""; sleep(5); vec($rin, fileno(STDIN), 1) = 1; sleep(5); my $isPiped = select($rout=$rin,"","",1); if($isPiped){ #Do the data processing from pipe }
    And it works every time, but now it waits for at least a 10 seconds every time. It becomes pretty annoying, after a while. Is there a better, more efficient solution to data coming through a pipe detection?
Regular expression for a comma separated string
6 direct replies — Read more / Contribute
by naderra
on Nov 23, 2014 at 18:22

    Hi,

    I would like to define a regular expression that matches strings consisting of two sub-strings separated by a single comma. Each sub-string may not be empty, and consists exclusively and without repetition of the characters 'A', 'G', 'C' and 'T'. Thus, the pattern should match strings such as:

    A,G
    AG,CT
    TC,CA <- correct, 1st and 2nd sub-strings may have characters in common
    ( as long as these are not repeated within the sub-string ).
    GAT,CGA
    CGAT,TG <- correct, sub-strings may be of different length.
    etc ...

    and should not match:

    ,G <- missing 1st sub-string
    ACGT <- missing comma
    X,A <- incorrect character X
    AA,G <- repetition of character A in 1st substring
    AC,GGC <- repetition of character G in 2nd sub-string
    ATGA,TGG <- repetition in both sub-strings
    ATCXG,AAC <- incorrect character X and repetition in 2nd
    sub-string
    etc ...

    So far I have:

    /^(?=[ACGT]{1,4},[ACGT]{1,4}$)(?!.*(.).*\1.*,)(?!,.*(.).*\1).*$/

    /^(?=[ACGT]{1,4},[ACGT]{1,4}$)(?!.*(.).*\g{1}.*,)(?!,.*(.).*\g{1}).*$/

    also tried joining the capture groups with:

    /^(?=[ACGT]{1,4},[ACGT]{1,4}$)(?!.*(.).*\g{1}.*,.*(.).*\g{2}).*$/

    Now, (?=[ACGT]{1,4},[ACGT]{1,4}$) seems to match the "two sub-strings separated by a single comma" and "consists exclusively of the characters 'A', 'G', 'C' and 'T'" through out the string;

    (?!.*(.).*\1.*,) seems to match "without repetition" up to the comma.

    However, (?!,.*(.).*\1) appears not to be ensuring that it doesn't match a repeated character after the comma.

    I'd greatly appreciate replies with clues and/or patterns that help with the desired matching.

    Using perl v5.18.2

    Thanks in advance

    Robert

Array and Hash
4 direct replies — Read more / Contribute
by waytoperl
on Nov 23, 2014 at 16:58

    Hello,

    The content of DATA are modified  %hash value . Need to include  %hash key before or after  SOME_DATA to respective modified  %hash key

    Don't know how to search modified  value and include respective  key after or before  SOME_DATA

    __HASH__ $VAR1 = { 'Key1' => 'Cat.Mouse.Game.Lion.Tiger.Elephant', 'Key2' => 'Cat.Mouse.Game.Lion.Tiger.Lion', 'Key3' => 'Cat.Mouse.Game.Lion.Tiger.Monkey', };
    __DATA__ Pigeon.Lion.Tiger.Elephant SOME_DATA Pigeon.Lion.Tiger.Lion SOME_DATA Pigeon.Lion.Tiger.Monkey SOME_DATA
    __Example code__ my %hash = () my $file = 'master.txt'; open (my $OUT_FILE, '<', "TEXT_FILE") or die "ERROR"; while (<$OUT_FILE>) { print "$. \n" if /SOME_DATA/; #searching SOME_DATA and printing }
    __OUTPUT__ Pigeon.Lion.Tiger.Elephant SOME_DATA (Key1) Pigeon.Lion.Tiger.Monkey SOME_DATA (Key3)

    UPDATED:

    %hash value "Cat.Mouse.Game" is replaced with "Pigeon" and placed in a text file along with SOME_DATA

    Ignore "Pigeon" and match other characters. If there is a match, then include respective %hash Key before "SOME_DATA"

Useless use of private variable in void context at
2 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 23, 2014 at 16:56

    I have a script that uses "rand" to add one of the four letters (ATCG) to a string:

    #!/bin/perl use warnings; use strict; my $i =0; my $x; my $aa = ""; for ($i;$i<5007;$i++){ $x = int(rand(4)); if ($x == 0) { $aa .= 'A'; } elsif ($ x== 1) { $aa .= 'T'; } elsif ($x == 2) { $aa .= 'C'; } else { $aa .= 'G'; } } open (FILE, '>./test'); print FILE "$aa"; close (FILE);

    It works but also sends : "Useless use of private variable in void context at 8 line 24." What does it mean?

reading value from html to cgi
2 direct replies — Read more / Contribute
by bigup401
on Nov 23, 2014 at 13:27
    $firstname = $cgi->param('firstname'); <input type="text" name="firstname" value="john">

    i have readed name"" how can i read value""?

    don't re-post if ur telling me to use a framework

CSV regex with hash/array program plan
3 direct replies — Read more / Contribute
by campus1plb
on Nov 23, 2014 at 10:01

    Dear all, i'm new to the site (and returning to Perl after quite a long break, but i was never a master of any sort)

    I'm trying to plan out a program and i'd like to sanity check my objectives just to make sure i'm not a: making more work than i need to, and b: planning something stupid.

    Task:
    read a CSV file containing columns for |Degree Subject|Entry requirement|

    The entry requirement field contains strings such as:

    "A minimum of 3 A Levels at ABB for First Year Entry or a minimum of AAB for Second Year Entry. Must include Mathematics and Physics at AB."

    "For First Year Entry a minimum of 3 A Levels at BBB or 4 AS at AABB. For Second Year Entry a minimum of an A in the subject selected for Single Honours plus BB, or AB in the subjects selected for Joint Honours plus a further B."

    "Three A Levels at ABB. AB required in Mathematics and Physics or a B in Design & Technology or a B in Engineering. If applicant presents with B in Physics, Design & Technology or Engineering, Mathematics must be A grade."

    Program plan:
    Read in the CSV file to an array/hash (more on this later)

    Use regular expressions to determine which subjects are required for each degree subject, and create a column specific to EACH subject and mark whether it is present or not

    Write this array/hash to a csv file for output.

    Example output:
    |Degree Subject|Entry requirement* |Grades|Maths|Physics|Engineering|etc etc

    |Chemical Eng |A minimum of 3 A Levels at XXX* for First Year Entry..|ABB |A/B |A/B | |
    *(use s/ in regex to indicate which parts have been "detected" for manual checking)

    Problems/Puzzles:

    1/ Would it be more straighforward to use Text::CSV having created the full matrix of columns manually and then assign values to the relevant fields or check for entries in the dataset and then "create" columns during runtime?

    2/ My gut feeling is to use an array (instead of a hash) for this as by my nature (and as C was my first language many moons ago) it seems nice and orderly. Speed performance isn't a critical issue here.

    3/ for the REGEX component, it's going to be quite complicated, and there are many variants of field here. I'm contemplating doing one of two strategies a) using a first pass to pull out all unique entries and attempt a regex on them, using this as a reference key to then screen the remainder b) doing the regex in one pass N.B there may be as many as 46,000 row entries, but in terms of unique entries it may be more like 5,000 (which is still loads but easier to check over perhaps)

    Delighted for any guidance, even just pointers.

    Best wishes, Phil
Perl script does not work on other directories?
6 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 23, 2014 at 04:26
    Hello guys,

    I have a strange issue with my code that I cannot really understand.I've written a code that searches a given string in all the files in given directory.So far so good.The program works only if I type the directory where the script is.If I choose another directory I get the msg "Couldn't open that file perl_ex1.pl: No such file or directory at ex1.pl line 20, <STDIN> line 2." for example and that file obviously exists in the given directory. Any clarification would be useful.

    Here is my code:
    #!/usr/bin/perl use warnings; use strict; print "Write the directory where you want to search: \n"; my $dir=<STDIN>; chomp $dir; print "Write the string you want to search for: \n"; my $string=<STDIN>; chomp $string; opendir DH, "$dir" or die "Couldn't open that directory $dir: $!"; while($_=readdir(DH)){ next if $_ eq "." or $_ eq ".."; if (-d $_){ print "$_ is a directory\n"; next; } open FILE, "$_" or die "Couldn't open that file $_: $!"; for my $i (<FILE>){ if($i=~/($string)/){ print "I have found |$string| in |$_| file on line:\n$i\n"; last; } } }
Asynchronous HTTP requests
2 direct replies — Read more / Contribute
by perlfan1020
on Nov 23, 2014 at 00:41
    I need to resolve a large number of HTTP requests asynchronously and write the retrieved HTTP pages to files named from the URLs.

    AnyEvent::HTTP seems to be a good choice. However, in the call back as the following: I can't associate the $data to the $url.

    http_get $url, key => value..., $cb->($data, $headers)

    Any ideas? Thanks in advance!
Replacing builtin macro languages with perl -- how difficult?
3 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 23, 2014 at 00:22
    I keep getting involved in projects where a big software package has a hand-rolled macro language that is pathologically deficient. I'd love to replace such languages with Perl and expose the underlying data without needing to copy it or pipe into a separate perl process. Can anyone comment on how difficult this might be? Are there any examples of this being done?

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.
  • 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 contemplating the Monastery: (10)
    As of 2014-11-27 08:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (182 votes), past polls