Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Clear questions and runnable code
get the best and fastest answer

The Monastery Gates

( #131=superdoc: print w/ replies, xml ) Need Help??

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
Comparing results of math operations
7 direct replies — Read more / Contribute
by stangoesagain
on Apr 16, 2014 at 05:12
    Little real life problem
    #!/usr/bin/perl use strict; use warnings; use 5.010; my $x=0.95*806; my $y=1.3*589; say $x <=> $y; $x == $y ? say 'equal' : say $x.' '.$y; say $x cmp $y; $x eq $y ? say 'equal' : say $x.' '.$y; #now with simpler math $x=2*1.25; $y=5*0.5; say $x <=> $y; $x == $y ? say 'equal' : say $x.' '.$y; say $x cmp $y; $x eq $y ? say 'equal' : say $x.' '.$y;
    -1 765.7 765.7 0 equal 0 equal 0 equal
    Why? What's wrong?
better way to escape escapes
5 direct replies — Read more / Contribute
by RonW
on Apr 15, 2014 at 19:48
    I am processing an input format that can use either @ or \ to introduce inline directives. when a literal @ or \ is in the input, either character can be used to escape either character. IE: @@ or \@ or \\ or @\

    Currently, I replace the escape sequences with place holders, then extract directives, then replace the placeholders with the intended literal occurrences of @ and \ in the string.

    s/(?<![\\\@])[\\\@]\@/\x11/g; s/(?<![\\\@])[\\\@]\\/\x12/g; while (/[\\\@]([_A-Za-z]+)/) { print "Extracted code '$1'\n"; s/[\\\@]$1//; } s/\x11/\@/g; s/\x12/\\/g;

    I'm sure there's a better way, but my search-foo is lacking. And so is my regex-foo. (And there's likely an input file that will break this.)

    (and no, it's not LaTeX, despite the similarities)

Perl Directory file extension stats.
3 direct replies — Read more / Contribute
by sidsinha
on Apr 15, 2014 at 17:32

    I had a question regarding Perl directory statistics. Are there any perl packages that I can use to list the number of unique file extentions in a given directory?

    For ex, it should parse through an input directory (including subdir) and return the number of unique file types (for ex, 200 .bmp , 50 jpg ,20 txt) etc.

    Please guide me . thanks.
Lingua: Transform "wasn't" into "was not"
1 direct reply — Read more / Contribute
by Anonymous Monk
on Apr 15, 2014 at 14:09


    As the title suggests, I'm looking for a module which converts various English shortcuts like we're into we are, I'm into I am, etc...

    I couldn't find any module in CPAN which does this. Maybe I haven't looked for the right name.
    So my question is: did anyone worked with something similar before? What module would you recommend for doing this?


Optimizing a naive clustering algorithm
3 direct replies — Read more / Contribute
by BUU
on Apr 15, 2014 at 12:09

    I'm attempting to implement a basic hierarchical agglomerative clustering algorithm to be used to create an arbitrary number of clusters from an existing dataset. More reading about the general concept can be found at at or your friendly neighborhood google.

    A word about the dataset.

    My data consists of some 1500-5000 "items" each of which contains a set of "words". These words are 5-30 character strings. Each set of words contains no duplicates. There are between 5-100 "words" in a set.

    Some words about the existing code.

    The theoretical complexity of such an algorithm is something like O(cn2d2) but I suspect my implementation is considerably worse since I ran it for over 11 hours and it only managed to consolidate 500 of the 1600 items.

    The "merge" function is obviously very silly, I wrote it without thinking very hard and it doesn't do much. On the other hand I don't think it impacts the performance.

    The vast majority of the time spent is going to be in the max_diff function, which appears to get exponentially slower as the program continues to run.

    The datastructure being produced is necessary, that is it should be a binary tree made of array-refs where each leaf is either another tree or an actual item. (Its necessary because we don't know how many clusters we want to produce).

    Suggestions for optimizations or even different algorithms gratefully received.

Perl formatting beginner
4 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 15, 2014 at 11:23

    Hi all i am new to perl just trying to figure out how I would format my output to look a certain way if possible. I want it be in 3 columns 10 rows Avery 5160 format, but without the use of PostScript. but right now it only show all in one column all the way down.

    #!/usr/bin/perl # Program name: # Purpose: Open disk file. Read and display the records in # the file. Count the number of records in the file. format LABEL = @<<<<<<<<<<<<<<<< $CUSTID @<<<<<<<<<<<<<<<< $fname @<<<<<<<<<<<<<<<< $lname @<<<<<<<<<<<<<<<< $phone @<<<<<<<<<<<<<<<< $address @<<<<<<<<<<<<<<<< $city @<<<<<<<<<<<<<<<< $state @<<<<<<<<<<<<<<<< $zip @<<<<<<<<<<<<<<<< $email . #$topmargin = 0.60; #$poleft = 0.4; $pomiddle = 3.20; $poright = 5.95; $lheight = 1; $row = int($opt{r}) || 1; # chop off any fractional parts and $col = int($opt{c}) || 1; if ($col == 1) { $po = $poleft; } elsif ($col == 2) { $po = $pomiddle; } else { $po = $poright; } # $sp = ($topmargin + ($row - 1)*$lheight); # required terminator for format definition open (FILEIN, "cust.txt") || warn "Could not open passwd file\n"; open (LABEL, ">labels-to-print") || warn "Can't create labels file\n"; while (<FILEIN>) { #print "$_"; ($CUSTID,$fname,$lname,$phone,$address,$city,$state,$zip,$email) += split(/,/,$_); # Or use array: @fields = split(/,/,$_); write(LABEL); # send to output ++$line_count; } print ("File \"passwd\" has $line_count lines. \n"); close (FILEIN); close (LABEL);
Extracting a chapter from text file
4 direct replies — Read more / Contribute
by jwkuo87
on Apr 15, 2014 at 10:28
    Hi everyone. I'm fairly new to Perl and am trying to extract a specific chapter from a text file. In the example below, I would like to retrieve the text from Chapter 2.
    Table of Contents Chapter 1. Introduction Chapter 2. Main Chapter 3. Conclusion ============================== Chapter 1. Introduction This is the introduction preceding Chapter 2. Chapter 2. Main This is the text contained in Chapter 2 and will contain a lot of text + with at least 100 words and probably somewhere around 1000-5000. Chapter 3. Conclusion This is the conclusion.
    The Perl script should extract "This is the text contained in Chapter 2 and will contain a lot of text with at least 100 words and probably somewhere around 1000-5000." from the file and write the output to a new file. Unfortunately, the code below only gives me the first matches, i.e. the text from the table of contents.
    #!/usr/bin/perl -w #use strict; my $startstring='Chapter\s2\.\sMain'; my $endstring='Chapter\s3\.\sConclusion'; { local $/; open (SLURP, "C:\\Text\\1.txt") or die $!; $data = <SLURP>; close SLURP or die $!; { @finds=$data=~m/($startstring.*?$endstring)/ismo; } open my $OUTFILE, ">", "C:\\Text\\Chapter2\\1.txt" or die $!; print $OUTFILE "@finds"; close $OUTFILE; }
    Is there a way to refine my search function so it works as I would like it to? Like including a rule where the startstring must be skipped if the preceding 5 strings contains "Chapter 1. Introduction" and/or the output should contain at least 100 words? Thanks in advance! :)
Delay running Perl execs in Windows
4 direct replies — Read more / Contribute
by Raineer
on Apr 15, 2014 at 10:19

    Hello all,

    I use Perl to write trace and log parsers for my engineering group. They are a Windows crowd and most are not savvy enough to use native Perl.

    I use `pp` to package the scripts into executables which they can run on Windows, and I've had great success doing so.

    My question is - Why do freshly-packed execs have such a long delay the first time they are run in Windows on a given machine? I even see this on my own machine - I pack with `pp`, then launching the file gives me a black screen hang for 30-60 seconds and the program proceeds as desired. Subsequent launches have no delay at all.

    My colleagues see this same behavior as well. It's a nuisance at worst, but I would really like to eliminate this. I can see the behavior on the simplest of programs, at most my typical programs only import Archive::Tar.

    Are there further settings I can use in `pp` to eliminate this delay? I have searched and searched but haven't found a report that matches this behavior, though I can't believe I'm the only one.

    I'm using Strawberry 5.16, but I have seen the same behavior on every distro and version I have used. Thank you.

What are (popular) modules that access/modify @ARGV?
4 direct replies — Read more / Contribute
by frozenwithjoy
on Apr 14, 2014 at 04:55

    I'm working on a project that needs to access @ARGV before anything else does. I figured I'd add a subroutine that checks for potential conflicts and issues a warning if any of the more popular modules that access/modify @ARGV are loaded at the time my module is imported.

    I know that it isn't feasible to list/check all potentially conflicting modules (see @conflicts below). Nevertheless, I'd like to come up with a decent list of popular/commonly used modules that touch @ARGV. What are your favorites?

    This code identifies modules loaded both directly and indirectly (i.e., as dependencies). It works fine and I'm not asking for help with it (but I certainly won't turn away any comments/suggestions):

    use Module::Loaded; sub _check_for_conflicts { my @conflicts = qw(AppConfig Getopt::Args Getopt::Long Getopt::Simple Getopt +::Std); my @loaded; for (@conflicts) { push @loaded, $_ if defined is_loaded($_); } if ( scalar @loaded > 0 ) { print STDERR <<EOF; WARNING: A module that accesses '\@ARGV' has been loaded before Log::Reproducib +le. To avoid potential conflicts, we recommended changing your script such that Log::Reproducible is imported before the following module(s): EOF print STDERR " $_\n" for sort @loaded; print STDERR "\n"; } }

    Thanks all!

RESOLVED: Seeking insight on a specific regular expression
1 direct reply — Read more / Contribute
by BritTim
on Apr 13, 2014 at 23:18

    RESOLVED: The explanation is available in perlre in the section Repeated Patterns Matching a Zero-length Substring.

    Thus Perl allows such constructs, by forcefully breaking the infinite loop. The rules for this are different for lower-level loops given by the greedy quantifiers *+{} , and for higher-level ones like the /g modifier or split() operator. The lower-level loops are interrupted (that is, the loop is broken) when Perl detects that a repeated expression matched a zero-length substring.
    Here Perl is preventing start of string from repeatedly matching in an infinite loop.

    Please accept my apologies for posting this question without first ensuring that I was completely unable to resolve it myself. I shall be more conscientious in future.

AnyEvent and Postgres Listen/Notify
2 direct replies — Read more / Contribute
by xtpu2
on Apr 12, 2014 at 16:13

    I'm trying to make use of the Postgres Listen/Notify feature in my AnyEvent-based program. I tried looking for a ready solution and found AnyEvent::Pg on CPAN, but I've not been able to get the module to work (and the developer does mention that it is in very early development). So I was trying to figure out the best way to set this up with the tools I have.

    Listen/Notify is supported by DBD:Pg, so the solution I came up with is to poll $dbh to see if a notification has been received, like so (posting only the relevant code):

    my $w = AnyEvent->timer ( after => 0, interval => 2, cb => sub { if (my $notify = $dbh->pg_notifies){ # Do something } } );

    This works. However, I'm not sure this is the best way to do it because I'm just not that familiar with event-based programming. I would really appreciate alternate solutions / thoughts from more knowledgeable people...

How to use multithreading in perl?
3 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 12, 2014 at 13:59
    Hi, I am C++ developer and now learning and implementing multithreading in perl. But i found most of the examples jsut tell the basics only. Any real working example solving a pblm will help me a lot. Thanks
New Monk Discussion
To front page or not ? [ANSWERED]
4 direct replies — Read more / Contribute
by Bloodnok
on Apr 10, 2014 at 06:35
    Greetings fellow approvers,

    Pray tell, what are the considerations &/or constraints to be applied when deciding whether, or not, to front page a node ?


    Many thanks to the respondents - it's all much clearer now ... for a Friday :-)

    A user level that continues to overstate my experience :-))
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 surveying the Monastery: (16)
As of 2014-04-16 14:32 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (430 votes), past polls