http://www.perlmonks.org?node_id=479

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
Win32::Gui listview column click
No replies — Read more | Post response
by Anonymous Monk
on Aug 18, 2018 at 07:45

    Hello, i am hoping you can help me. I am using the Win32::GUI module and i have a listview added. I am trying to catch a button-click on a column name so i can sort on that column. I am looking through the documentation but i can't find anything. Here is what i have so far:

    use strict; use warnings; use Win32::GUI(); use DBI; my $mw = Win32::GUI::Window->new( -text => "View", -size => [ 550, 500 ], -pos => [ 200, 200 ], ); sub mw_Terminate { return -1 } my $lv = $mw->AddListView( -pos => [ 20, 20 ], -size => [ 400, 400 ], -gridlines => 1 ); $lv->InsertColumn( -item => 0, -text => "PID", -width => 50); $lv->InsertColumn( -item => 1, -text => "Name", -width => 100); $lv->InsertColumn( -item => 2, -text => "Address", -width => 100); $lv->InsertColumn( -item => 3, -text => "Salary", -width => 100); my $dbfile = 'C:/code/lqs.db'; my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","",""); my $stmt = qq(select id,name,address,salary from deliver order by sala +ry desc); my $sth = $dbh->prepare($stmt); $sth->execute; while(my @row = $sth->fetchrow_array()){ $lv->InsertItem(-text => [$row[0],$row[1],$row[2],$row[3]]); } $mw->Show; Win32::GUI::Dialog();
parse json data with underscore symbol
2 direct replies — Read more / Contribute
by amaa11
on Aug 18, 2018 at 02:57
    Dear all,
    when I run the below code,I got this error
    Not a HASH reference at mojo.pl line 112.
    which refer the last line when I print it
    How to fix it please??
    I want to print the sample size (3,117) and other attributes
    Thanks in advance
    =========================
    use strict; use warnings; use JSON qw( decode_json ); my $json = '{ "_embded" : { "stes" : [ { "SampleSize" : "3,117 ", "ge" : false, "gx" : false, "Count" : 1922309, "qualifier" : null, "imputed" : true, "pooled" : false, "Comment" : null, "Id" : "G00854", "full" : false, "Requested" : false, "forms" : [ { "manufacturer" : "ymetrix" } ], "ries" : [ { "te" : "initial", "Individuals" : 4390, "Groups" : [ { "Group" : "European" } ], "Origin" : [ ], "Recruitment" : [ { "majorArea" : "Europe", "region" : "Northern Europe", "countryName" : "U.K." } ] }, { "te" : "epion", "Individuals" : 2698, "Groups" : [ { "Group" : "European" } ],<br> "country" : [ ], "Recruitment" : [ { "majorArea" : "Europe", "region" : "Western Europe", "countryName" : "Germany" } ] }, { "te" : "ation", "Individuals" : 1649, "Groups" : [ { "Group" : "NR" } ], "country" : [ ], "Recruitment" : [ ] } ], "sea" : { "trt" : "risk" }, "Technologies" : [ { "Technology" : "GW" } ], "SampleSize" : "2,698 ", "publicationInfo" : { "Id" : "247", "publicationDate" : "2016-12-01", "publication" : "Psychiat", "title" : "GTW.", "author" : { "fullname" : "Perlis RH", "orcid" : null }, "_links" : { "stes" : { "href" : " ", "teated" : true } } }, "_links" : { "self" : { "href" : "" }, "sd" : { "href" : "", "templated" : true }, "asmary" : { "href" : "" }, "ets" : { "href" : "" }, "ss" : { "href" : "" }, "aons" : { "href" : "" } } } ] }, "_links" : { "self" : { "href" : "" } } }'; my $decoded = decode_json($json); # This is a Perl example of parsing a JSON object. print "SampleSize = " . $decoded->{'_embded'}{'stes'}{'SampleSize'} . +"\n";
[OT] Why does newline in Windows print as having width?
3 direct replies — Read more / Contribute
by Lotus1
on Aug 17, 2018 at 21:48

    This is minor but I find it annoying. When I print 80 characters and then a newline on the Windows command prompt it prints a blank line after the line. The Linux command prompt doesn't do this. My workaround on Windows has been to only print 79 characters per line. My question is why? I don't know how to print a newline from a batch file or I would test it.

    use warnings; use strict; print "-"x79,"\n"; print "1\n"; print "-"x80,"\n"; print "2\n"; print "-"x80; print "3\n"; __DATA__ --redirected to a file in Windows. ---------------------------------------------------------------------- +--------- 1 ---------------------------------------------------------------------- +---------- 2 ---------------------------------------------------------------------- +----------3 --appearance from Windows command line. ---------------------------------------------------------------------- +--------- 1 ---------------------------------------------------------------------- +---------- 2 ---------------------------------------------------------------------- +---------- 3 --appearance from Linux command line. ---------------------------------------------------------------------- +--------- 1 ---------------------------------------------------------------------- +---------- 2 ---------------------------------------------------------------------- +---------- 3
Make CSV list from values in AoH
4 direct replies — Read more / Contribute
by tel2
on Aug 17, 2018 at 18:35
    Dearest Monks,

    Let's say I've got an array of hashes like this:

    @AoH = ( { name => Adam, age => 0 }, { name => Bob, age => 10 }, { name => Cat, age => 20 } );
    and I want to produce a 'quoted' comma separated list (a bit like CSV) of the names, i.e.:
    'Adam','Bob','Cat'
    can anyone offer more concise/efficient/elegant ways of doing it than this kind of approach, which does work, and is pretty concise, but ain't pretty:
    $csv .= ",'$AoH[$_]{name}'" for (0..$#AoH); $csv =~ s/^,//;
    Order of names doesn't matter.

    Update: If there are no names (i.e. @AoH is empty), the output should be:
    ''
    i.e. 2 single quotes, but my solution above doesn't meet that requirement (I've replied to Rolf below with some which do, though).

    Thanks.
    tel2

Match multiple number
3 direct replies — Read more / Contribute
by IB2017
on Aug 17, 2018 at 14:37

    Dear monks

    How can I write the following regex so that it matches ad infinitum all numbers that are x=x+5? In my regex I stopped at 25.

    if ($Number=~/0|5|10|15|20|25/){ ... }
How to efficiently search for list of strings in multiple files?
4 direct replies — Read more / Contribute
by stray_tachyon
on Aug 17, 2018 at 12:11

    Hi Perl masters, I am trying to lookup the existence of a list of unions in a bunch of collective agreements. I was told my codes aren't efficient because they are going through an array one by one. I try to use these two lines

    my ( $first_match ) = grep { $line =~ m/\Q$_\E/ } keys %InputData_Empl +oyersHash; print $search and last SEARCH if $first_match ne "";
    (modified from https://www.perlmonks.org/?node_id=1183136) but they don't work. Thank you very much

    Here's my working code:

    read_in_data_union(); process_files ($FileFolder); sub read_in_data_union { open(my $fh, '<:crlf', $DataFile_Unions) or die "Could not open file '$DataFile_Unions' $!"; chomp(@InputData_Unions = <$fh>); } sub process_files { my $path = shift; opendir (DIR, $path) or die "Unable to open $path: $!"; my @files = grep { /.pdf.txt/ } readdir (DIR); closedir (DIR); @files = map { $path . '/' . $_ } @files; foreach my $file (@files) { open (my $txtfile, $file) or die "error opening $file\n"; print $FileHandle "$file"; LookForUnion: { print $FileHandle "\t"; while (my $line=<$txtfile>) { foreach (@InputData_Unions) { if ($line =~ /\Q$_/i) { print $FileHandle "$_"; last LookForUnion; } } } } } }
    My union.txt file sample:
    Sarnia Employees' Bargaining Association Sarnia Municipal Administrative Employees' Assn. Sarnia Police Association Sarnia Professional Fire Fighters'
    and the collective agreements are just converted text files from PDFs Sample:
    McMaster University (The "Employer") and Service Employees International Union, Local 2 BGPWU ("SEIU") Representing Hospitality Services Staff ________________________________________ COLLECTIVE AGREEMENT

Proper way to split a line with the "," delimiter
4 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 17, 2018 at 11:40
    Hi Monks,
    I thought it was obvious enough, but apparently I am doing something wrong here:
    Say you have the line:
    my $string='nick,1,2,,56,88';

    I want to capture all elements, including the missing one (between 2 and 56).But if I do this:
    my @array = split(/\,/, $string);

    it returns 4 and not 5 elements in the array.What am I missing here?
    Thanks!
substitute space to 0
5 direct replies — Read more / Contribute
by yueli711
on Aug 17, 2018 at 11:31

    Hello, I just want to substitute the space to 0. Spaces were separated by tab. Thanks in advance! For example

    1 2 2 5 4 4 4 4 4 3 4 4 1 1 5 6 4 change to 1 2 0 0 2 0 5 0 0 0 4 4 4 4 0 0 4 0 0 0 3 0 0 0 4 4 0 0 1 0 0 0 0 0 1 5 6 0 4 0 0 0 0 0
    #use strict #use warning open(IN1, "tmp01") || die "Cannot open this file"; @lines = <IN1>; $i = 0; for (@lines) { $lines[$i]=~s/\s\s+/\t0\t/g; $thislines[$i]=$lines[$i]; print $thislines[$i]; $i++; } open(OUT, ">tmp02") || die "Cannot open this file"; for $thisline(@thislines){ print OUT $thisline;} close(OUT); close(IN1);
Tab completion of path from prompt from within Perl script
2 direct replies — Read more / Contribute
by nysus
on Aug 17, 2018 at 11:05

    I have a perl script that asks the user to input a path on the local machine. Looking for a module that implements path tab completion on a Mac. I found Term::BashTab but it doesn't work on a Mac. Anyone know of any modules?

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Help publishing my DB migration project
2 direct replies — Read more / Contribute
by juankpro
on Aug 16, 2018 at 19:56

    I started learning Perl a couple of months ago and started a project which I call Migrate so I can learn as much Perl as possible.

    The project allows managing DB schema changes (migrations) using Perl syntax for creating and removing tables, indexes, columns and constraints instead of plain SQL.

    The current implementation is using DBI and DBD::SQLite and there is also an implementation for Informix, but I made it in such a way other RDMS implementation can be added as separate libraries easily.

    I need the following:

    • Advice on how can I get a Perl partner to help me finish the tool. Specially the tests suite.
    • Get someone to review my code so I can be confident I'm in the right path
    • Advice on what are the next steps or what I'm still missing to get t publish my project in CPAN
    My project is posted here: https://github.com/juank-pa/Perl-Migrate There is already general documentation on the README file as well as an API in the repo Wiki. Please guide me. Thanks in advance

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.