Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
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
Error DBI::SQLite file not existent
1 direct reply — Read more / Contribute
by IB2017
on Aug 19, 2018 at 13:41

    Hello monks

    I am trying to improve the error handling of my application that uses SQLite. I have noticed that the following script is not producing an error, no matter what $database is (empty, a non existing database name, etc.). However, if $database does not exist I would have expected to see an error. This forces me to first check for the existence of $database. Is this a feature?

    use DBI; use strict; my $driver = "SQLite"; my $database = ""; my $dsn = "DBI:$driver:dbname=$database"; my $userid = ""; my $password = ""; my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr; print "Opened database successfully\n";
Help with parsing command line to make more readable
2 direct replies — Read more / Contribute
by proxie
on Aug 19, 2018 at 13:24
    Hi folks. I work at a company where I don't normally do scripting. I tend to use Perl for like 3 days for some one off scripting need, and then promptly forget everything I learned. I was hoping someone could do this reg exp faster. Say I have a an automatically dumped command line like:
    execute test_number_1 -tex -tex_args -sub_args +debug_dir=./ -sub_args + +debug_dir=./ -constraint parity_en,random_en -sub_args '"' ruck=1 ' +"' -constraint dual_en -sub_args -cd -sub_args 2596.slow -sub_args te +st -seed 1 -tex_args- -opt 1 -tag 2
    Since it's difficult to parse what's really going on when it's all a single line, I instead would like to create a script that would take this command line as input and effectively print out something like the below in a kind of tree format:
    execute test_number_1 -tex -tex_args -sub_args +debug_dir=./ -sub_args +debug_dir=./ -constraint parity_en,random_en -sub_args '"' ruck=1 '"' -constraint dual_en -sub_args -cd -sub_args 2596.slow -sub_args test -seed 1 -tex_args- -opt 1 -tag
    Can someone help me get started on the most efficient reg exps to do this? There are certain anchors I can see in which to key off of in terms of knowing when to tab, but it's not consistent. For instance, can't always use the "-" reliably to know when to tab over.
Handling errors on perl
3 direct replies — Read more / Contribute
by limner
on Aug 19, 2018 at 08:24
    Hi i've a small problem about this instruction:

     push @tbl, [ @$_ ] for $table->rows

    when i launch the my program, occasionally i receive this message:

    Can't call method "rows" on an undefined value at download_and_parse_v3.pl line 81.

    So, because i want the program goes on even on this error, i tried this in replace of the above instruction:

     push @tbl, [ @$_ ] for $table->rows or warn " ERRORE $hname $chin ";

    where $hname and $chin are variables in the program that can help me understood the reason of the error.
    At this point i thought that i had the above phrase instead the "Can't call method rows..."
    but i alwais receive the same message. Where is my mistake? Thanks
Remove all duplicates after regex capture
3 direct replies — Read more / Contribute
by Maire
on Aug 19, 2018 at 05:45

    Hello, Monks. I'm hoping that you can help me with what is probably quite a simple problem, but which is completely stumping me!

    I'm trying to work with text files which are fairly disorganized in terms of structure, and which have been stored in a hash. What I want from each file is to extract the line of text which begins "title:#" and ends with a "#" and store this text in a scalar to be used later.

    The problem arises because, within some of the text files, there are multiple lines which begin and end with "title#" and "#" respectively. What distinguishes the "titles" I want is that they only ever appear in each text file once, whereas the "titles" I do not want appear at least twice (but sometimes three or four times) in the same text file.

    So this is the basic script that I am using, which prints out all the titles

    use warnings; use strict; my %mycorpus = ( a => "<blah blah blah blah title:#this is text I want 1# blah blah blah", b => "blah title:#this is text I do not want# blah title:#this is text I want 2# blah title:#this is text I do not want# blah", c => "blah blah title:#this is text I want 3# title:#this is text I do not want# title:#this is text I do not want# title:#this is text I do not want# blah", ); foreach my $filename (sort keys %mycorpus) { my $titles = ''; while ($mycorpus{$filename} =~ /title:#(.*?)#/g){ $titles = $1; print "$titles \n"; } }

    The script above, obviously, prints out all 8 captured lines which begin with "title", but my desired output is:
    this is text I want 1 this is text I want 2 this is text I want 3
Module::Metadata case sensitivity
2 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 18, 2018 at 23:00
    perl -MModule::Metadata -MData::Dumper -le "print(Dumper(Module::Metad +ata->new_from_module('CGI')))" perl -MModule::Metadata -MData::Dumper -le "print(Dumper(Module::Metad +ata->new_from_module('cgi')))"
    In the second example searching for "cgi" returns the metadata for "CGI" but both module and filename keys contain "cgi" and are not valid as a package or file for the "CGI" distribution?
Win32::Gui listview column click
2 direct replies — Read more / Contribute
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?
4 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
5 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
4 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/){ ... }

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 contemplating the Monastery: (5)
    As of 2018-08-19 22:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Asked to put a square peg in a round hole, I would:









      Results (188 votes). Check out past polls.

      Notices?