Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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
Matching all text after a specific character
1 direct reply — Read more / Contribute
by Anonymous Monk
on Aug 24, 2016 at 09:17

    I have a set of file-names in this format:


    And I want to match the second to last _ and all text after it (_Trimmed_75.SAM). Ordinarily I would use:


    To grab everything after the final _ but being new to Perl i'm not sure how I would do this for the second to last. Any help would be much appreciated!

Hash of Hash of Arrays
2 direct replies — Read more / Contribute
by voltas
on Aug 24, 2016 at 08:04
    I have a working code that reads a log file and produces output like below:
    => [ 'Testing { ', 'JIRA' => 'COM-6789 ', 'Program' => 'Testing ', 'rev' => 'r876391 ', 'Reviewer' => 'Balise Mat ' 'Description' => 'Audited }, { ', 'Program' => 'Testing ', 'rev' => 'r698392 ', 'Reviewer' => 'Chan Joe ', 'JIRA' => 'COM-6789 ' 'Description' => 'SO hwat }, { ', 'JIRA' => 'COM-6789 ', 'Reviewer' => 'Chan Joe ', 'Program' => 'Testing ', 'rev' => 'r327896 ' 'Description' => 'Paid the Due } ], ' => [ 'Development { ', 'JIRA' => 'COM-1234 ', 'Reviewer' => 'John Wick ', 'rev' => 'r345676 ', 'Program' => 'Development ' 'Description' => 'Genral fix }, { ', 'Program' => 'Development ', 'rev' => 'r909276 ', 'Reviewer' => 'None ', 'JIRA' => 'COM-1234 ' 'Description' => 'Updating Received } ],
    I want to print my output like Hash of Hash of Arrays, i.e. take Development as my first hash with the JIRA ID as the values and JIRA ID as the 2nd hash and the associated values. Example :
    'Development { COM-1234 { ', 'JIRA' => 'COM-1234 ', 'Reviewer' => 'John Wick ', 'rev' => 'r345676 ', 'Program' => 'Development ' 'Description' => 'Genral fix }, { ', 'Program' => 'Development ', 'rev' => 'r909276 ', 'Reviewer' => 'None ', 'JIRA' => 'COM-1234 ' 'Description' => 'Updating Received } }, ],
    Code snippet:
    #!/usr/bin/perl use strict; use warnings; use 5.010; use Data::Dumper; my @records = do { local $/ = '------------------------------------------'; <>; }; chomp @records; my %jira; foreach (@records) { next unless /\S/; my %rec = /^(\w+):\s*(.+?)$/mg; push @{$jira{$rec{JIRA}}}, \%rec; } say Dumper \%jira; my %prog foreach (@records) { next unless /\S/; my %rec = /^(\w+):\s*(.+?)$/mg; push @{$jira{$rec{Program}}}, \%rec; } say Dumper \%prog;
Hash searching
1 direct reply — Read more / Contribute
by Oligo
on Aug 24, 2016 at 07:29

    Hello Monks,

    I'm trying to master the art of searching with hashes. I have a file with a list of search terms (one line = one term):

    J00153:42:HC5NCBBXX:6:1101:10896:14959 J00153:42:HC5NCBBXX:6:1101:10896:14959 J00153:42:HC5NCBBXX:6:1101:26616:20709 J00153:42:HC5NCBBXX:6:1101:27549:19935

    ...and a master file I want to search for those terms in (again, one line per record):

    J00153:42:HC5NCBBXX:6:1101:10896:14959 99 gnl|Btau_4.6.1|chr16 + 72729218 1 12M J00153:42:HC5NCBBXX:6:1101:27549:19935 83 gnl|Btau_4.6.1|chr8 + 49556412 1 7M

    I started by opening the query file and reading each line into an array. Then while-ing through the master file, returning matching lines where the array elements match the relevant part of the master file:

    # Open query file and read into array $queryfile = $ARGV[0]; open (QUERYFILE, $queryfile) or die "Cannot open query file\n"; @queries = <QUERYFILE>; close QUERYFILE; # Open main file $mainfile = $ARGV[1]; open (MAINFILE, $mainfile) or die "Cannot open searchable file\n"; # Search through main file while ($inline = <MAINFILE>) { @split = split /\t/, $inline; $ID = $split[0]; if (grep /$ID/, @queries) { print $inline; } else { } } exit;

    This works fine, but the files are huge and the code takes an age to run. So, I tried converting the array to a hash (array elements = hash keys, values all = 1) but I can't seem to get the pattern matching syntax right; the code runs much faster but nothing comes back. So far I have:

    # Open query file and read into array $queryfile = $ARGV[0]; open (QUERYFILE, $queryfile) or die "Cannot open query file\n"; @queries = <QUERYFILE>; close QUERYFILE; # Convert array to hash %hash = map {$_ => 1} @queries; # Open main file $mainfile = $ARGV[1]; open (MAINFILE, $mainfile) or die "Cannot open searchable file\n"; # Search through main file while ($inline = <MAINFILE>) { @split = split /\t/, $inline; $ID = $split[0]; if (defined $hash{$ID}) { print $inline; } else { } } exit;

    Any Perly wisodom greatly appreciated!

DBI - Insert NULL value into DB
3 direct replies — Read more / Contribute
by Yaerox
on Aug 24, 2016 at 05:33
    Got some trouble handling undef/null values from database. (The following code is some kind of pseudocode.) I'm fetching data from DB-1 table test. Table test looks like:
    ID | SOMEVALUE -------------- 1 | NULL 2 | text 1 | NULL
    Now I copy this lines by doing an insert on a second database with the same table called DB-2 table test2:
    while ( ( $id, $value ) = $hStatement->fetrow_array ) { $hStatement2->prepare( INSERT INTO test2 VALUES ( ?, ? ) ); $hStatement2->execute( $id, $value ); }
    The result is fine test2 looks like test table. BUT ... i get uninitialized errors when those undefined values (NULL) are used. I see two ways to solve the problem: 1) Use constants, so i handle all those possible undefs and I concat my string as insert-statement and doing
    $hStatement2->prepare( $sSQL_Statement ); $hStatement2->execute( );
    2) Run this part of the code in a block using "no warnings". I don't like both ways because method 1 needs alot of if-code and statements are proberly less optimized for the database. But suppressing warnings is even worse imo. So I'd like to ask if any of you guys know, how to solve this problem in a good programmers way.
Authenticate HTTPS website with IE Automation
1 direct reply — Read more / Contribute
by rapid_perl
on Aug 23, 2016 at 23:48
    Hi All, I am trying to automate the authentication of a secured portal for my automation project.I have tried number of ways of doing the same but still struggling with the same. If we do the same with LWP or machanize it is achieved but the content I am getting back in response is the not same as we get in IEautomation content method which is actual HTML content of web page. Can anybody provide me with the solution if they have encountered the same situation earliar. I would prefer if somebody come up with a solution achieved through Win32::IEAutomation package.
Parsing UTF-8 characters ( is changed to )
1 direct reply — Read more / Contribute
by ashesh28
on Aug 23, 2016 at 22:57
    Hi Monks , Am in a bit of confusion here with no clue on what is causing this exception. I have written a perl script which extracts rows of data from SharePoint list using SOAP::Lite Module. The extraction works properly , but in certain scenarios the special character symbols are converted when i open the CSV file which is created by Perl script. Part of Code snippet which extract the mentioned columns and write it into a CSV file. I have already parsing them as UTF-8
    my $element_rowlimit = name( 'rowLimit' => 10000 ); #print $soap->serializer->envelope( 'method' => 'GetListItems', $eleme +nt_listname, $element_query, $element_rowlimit ); my $som = $soap->GetListItems( $element_listname, $element_query, $ele +ment_rowlimit ); my @results = $som->dataof('//GetListItemsResult/listitems/data/row'); my $oc = Text::CSV->new({sep_char => ',', eol => $/ }) or die Text::CSV->error_diag(); open my $of, '>', 'Load_Data.csv' or die $!; binmode $of, ':utf8'; chomp @results; foreach my $data (@results) { my $item = $data->attr; chomp $item; $oc->print($of,[@$item{qw( ows_Job_x0020_ID ows_Justific +ation )}]); } close $of;
    Lets say the Value of Justification column in Sharepoint is as : "RMS Roughness (Rq) is ~3.7 for both wafers." But, when extracted by perl , the comment is changed to following : "RMS Roughness (Rq) is ~3.7 for both wafers."
Create based on repeating data
3 direct replies — Read more / Contribute
by Perl-Thornton
on Aug 23, 2016 at 20:36

    Hello all. I have a file that looks like this:

    Hostname1 Cisco Chassis Serial Number xyz123 Interface Gig0/0/31 Hostname2 Juniper Chassis Serial Number abc123 Interface Gi-0/0/31
    The repeating pattern seems obvious. Each row starts with either text that starts at the beginning of the row (i.e. hostname which could be the key to a hash), or the row starts with white space or tabs and then some text which could be the values associated with the key of the hash (i.e. serial number, interface, etc..). I have been working on solving this to parse through files with a Perl program but I've failed so far. I was hoping the Perl community might have suggestions to help me grasp what seems like should be a simple concept.
inspecting Perl call stack from inside a XS subroutine?
1 direct reply — Read more / Contribute
by bubnikv
on Aug 23, 2016 at 18:47
    Dear Perl Monks. I am taking part on a bigger Perl/C++ project. I managed to compile the Perl interpreter and everything related with Visual Studio C++ on Windows and I can now debug the XS/C++ part wonderfully. Now I would love to inspect the Perl call stack calling the XS routine, when I break in the C++ code. Is there any way to inspect the Perl internal structures using the C++ debugger to get the Perl call stack? Or maybe I can execute some Perl routine from inside the XS C++ code to get the Perl call stack? Thanks, Vojtech
Win32 OLE Word combining docs
1 direct reply — Read more / Contribute
by cormanaz
on Aug 23, 2016 at 18:04
    Good day bros. I need to create a script to take multiple word documents and combine them into one. I've done a little Word automation for accessing text but never for writing it. I have looked at nodes here like this one about writing stuff to Word, but what I want to do it take the text unmodified with formatting etc. from a source file and add it to the end of a destination file using code like:
    #!/usr/bin/perl -w use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; my $word = Win32::OLE->new('Word.Application') or die "Failure - word. + \n"; my $outputdoc = $word->Documents->Add; my @inputdocs = qw( foo.doc bar.doc); foreach my $f (@inputdocs) { my $inputdoc = $word->Documents->Open({FileName => "c:\\$f"}); my $inputparagraphs = $inputdoc->Paragraphs; foreach my $par (@$inputparagraphs) { #how to add to end of $outputdoc? } $inputdoc->close; } $outputdoc->SaveAs({FileName => 'c:\\combined.doc'});

    So I guess my question is, is there a way to take a paragraph from the source doc and push it (with original formatting) onto the end of the paragraphs of the destination doc? The VBA reference for Word shows that there is a Paragraphs.Add method, but it seems that just adds a paragraph without putting anything in it. Any pointers appreciated.

Parsing with XML::Simple
3 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 23, 2016 at 14:11
    Hi Monks!

    I am trying to get the value of a node that has an attribute, can not get it to work, here is a sample code on how I am trying:
    #!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; use Data::Dump 'pp'; #my $content = $response->decoded_content(); my $content = qq|<?xml version=\'1.0\' encoding=\'UTF-8\'?><soap:Envel +ope xmlns:soap=""> <soap:Header><ais:requestID xmlns:ais="http://com"> {7f000001-47db2420000001569783cdb88136} </ais:requestID> </soap:Header> <soap:Body> <soap:Fault> <faultcode>soap:Server</faultcode> <faultstring> check expression: </faultstring> <faultactor>CheckTwo</faultactor> <detail> <longDescription xmlns="http://com"> Exception occurred </longDescription> <MaxCode xmlns="http://com/Server">-2225</MaxCode> </detail> </soap:Fault> </soap:Body> </soap:Envelope>|; #my $parser = new XML::Simple; my $parser= XML::Simple->new( KeepRoot => 1, KeyAttr => [], ForceArray + => [qw( MaxCode )] ); my $result = $data->{'soap:Body'}->{'soap:Fault'}->{'detail'}->{'Error +Code'}; print $result; # I neet to print this::: -2225

    Thanks for looking!

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 having an uproarious good time at the Monastery: (13)
    As of 2016-08-24 14:25 GMT
    Find Nodes?
      Voting Booth?
      The best thing I ever won in a lottery was:

      Results (346 votes). Check out past polls.