Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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
Problem reading Excel File
3 direct replies — Read more / Contribute
by gunther_maier
on Feb 10, 2016 at 05:44
    Dear Monks,
    I would appreciate help with the following problem:


    I download some data via a weblink, which in my web browser I can either save as XLSX-file or open in Excel. In the latter case, the file opens in protected view and I need to click the button "Enable Editing".

    What I want to do:

    I want to download this file and extract some information from it all in a Perl script. I have mastered downloading with the help of LWP::Simple, but cannot find a way to read the information from the Excel file without manually opening it, clicking "Enable Editing" and saving it again. Once this is done, I can read the content without problems with Spreadsheet::XLSX.

    Many thanks
    Gunther Maier

Progress bar in Tk
2 direct replies — Read more / Contribute
by Anonymous Monk
on Feb 10, 2016 at 03:12

    I have the following code as an example but can't seem to figure out how to use it in my code. In my code I read 5,000+ folders in a file and then parse them and write the results of those files to another directory file by file. I'd like to use the progress bar included here in the subroutine in my script but I don't know where to put the call backs to track time. In this example below they just use a loop and count down. I can do that but it won't be an accurate representation to track with I'm doing.

    #!/usr/bin/perl # a nearly trivial script to mount /mnt/camera on # dev/ttyUSB1, create a target dir, and copy pics # from camera to dir. # Note that mount stuff has been excised for brevity. #--------------------------------------------------------------------- $| = 1; use strict; use Tk; use Tk::Pane; use Tk::ProgressBar; my $message = 'Ready'; my $main = MainWindow->new(); $main->configure(-title=>'Camera 2 Disk', -background =>'blue'); $main->geometry('370x200+100+00'); my $label = $main->Label(-text =>"Digital Camera Tool\nCopy Images fro +m Fuji FinePix to Disk", -relief => 'raised', -background =>'#42b4b4') ->pack(-side=>'top', -fill =>'both'); my $button_frame = $main->Frame(-relief=>'raised') ->pack(-side => 'top', -fill => 'x'); my $dump = $button_frame->Button(-text => 'Dump', -command => \&get_files) ->pack(-side =>'left', -anchor => 'w'); my $status_text = 'Ready'; my $status_label = $button_frame->Label( #-textvariable =>\$status_text, -relief => 'raised', -background =>'grey77') ->pack(-side=>'left',-fill=>'both', -expand=>'1'); my $exit = $button_frame->Button(-text => 'Exit', -command => 'exit') ->pack(-side =>'right'); my $status = $main->Scrolled('Pane', -scrollbars => 'se', -relief => ' +flat') ->pack(-side=>'top', -fill=>'both', -expand=>'y'); #$status->Label(textvariable =>\$message, #-relief => 'flat'); #->pack(-side=>'top', -fill =>'both'); my $percent_done; my $position = '0'; my $progress = $main->ProgressBar(-troughcolor => 'grey70', -width => 20, -length => 370, -anchor => 'w', -from => 0, -to => 100, -blocks => 0.1, -colors => [0, 'blue', 100], )->pack(-side=>'left', -fill=>'both'); $progress->value($position); MainLoop(); #--------------------------------------------------------------------- sub get_files { my $i; while ($i <= 10 ) { $percent_done = int(($i/10) * 100); $progress->value($percent_done); $progress->update; $i++; sleep(1); $message = "Loop number $i."; } }
Regex: Asterisk with NO preceding token
1 direct reply — Read more / Contribute
by pedrete
on Feb 10, 2016 at 03:09
    Hi Monks...!!

    i have "silly"? question for you, please....

    in Perl regex... what is the behaviour of an asterisk with no preceding token????

    an example of my doubt:

    .* matches  
    ok so far...



    *  also matches!!!!!!!!!

How to cut the directory path?
3 direct replies — Read more / Contribute
by aca
on Feb 10, 2016 at 01:56
    Hi, I am trying to cut the directory path in the Perl. Here is my code
    my $filespec = "/usr/share/directory/piano_book.mp4";
    How can i cut the first two directories i.e. /usr/share/ and i want only /directory/piano_book.mp4? Any help will be appreciated. Thanks in advance.
XML for a newbie
4 direct replies — Read more / Contribute
by Anonymous Monk
on Feb 10, 2016 at 00:41
    Hiya, I have gone through the documentation for xml::simple, xml::libxml, xml::xpath and my head is still swimming. I want to do is parse the processed request and be able to use C0,C1,C2 as variables. For further understanding, it would be cool if you explain how to add the items to an array in the case of multiple results please? Thank you for your assistance.

    My Code:
    use XML::Simple; $xml = new XML::Simple; my $data = XMLin($xmlresult); print Dumper($data);

    and the output for the dumper is:

    $VAR1 = { 'xmlns:s' => '', 's:Header' => { 'log' => { 'xmlns' => 'http://schemas.solarwinds' } }, 's:Body' => { 'QueryXmlResponse' => { 'QueryXmlResult' => { 'query +Result' => { + 'template' => { + 'resultset' => { + 'column' => { + 'FriendlyName' => + { + + 'ordinal' => '2', + + 'type' => 'String' + } +, + 'AddressMask' => +{ + +'ordinal' => '1', + +'type' => 'String' + }, + 'SubnetAddress' = +> { + + 'type' => 'String', + + 'ordinal' => '0' + +} + } + } + }, + 'data' => { + 'row' => { + 'c2' => 'Extra Subnet', + 'c1' => '', + 'c0' => '' + } + }, + 'xmlns:xsi' => '' + } }, 'xmlns' => 'http://schemas' } } };
reading a proxy address from a file and using in a while statement
2 direct replies — Read more / Contribute
by Anonymous Monk
on Feb 10, 2016 at 00:06

    I have a script where I'm reading I'm reading a line from a file in a folder that contains multiple lines and trying to then read a proxy address from a file and use it to look up the line which is a URL. It looks like I'm getting multiple proxy addressed back and I can't tell if they're being used for the look up.

    Also with my chomp $row I'm trying to return just use lines that start with http. I"m working on that next. Here's my code. I'll continue to trouble shoot but would appreciate any guidance or assistance.

    make_path('Bing/1Parsed/Html3/'); while (my $row = <$FILE>) { open my $fh1, ">", "Bing/1Parsed/Html3/$file.html" or die("Cou +ld not open file. $!"); chomp $row; print "$row\n"; my $xml1 = $row; $fh1->print ($row); #add pause my ( $x, $y ) = ( 2, 5 ); my $result = int( rand( $y - $x + 1 ) ) + +$x; print "pausing "; print $result; print "seconds"; sleep ($result); # create useragent my $ua = LWP::UserAgent->new; $ua->agent('Mozilla/8.0'); # Use this UA/Proxy to fetch something.... $ua->proxy(['http'], 'http://'.$proxy); my $xml2 = get $xml1; #$fh1->print ($xml1); $fh1->print ("\n"); #$fh1->print ($fh1.$xml2); #$fh1->print ($fh1); print $xml2; $fh1->print ($xml2); #close $fh1, ">", "Ask/Parsed/Html/$file.h +tml"; close $fh1; $xml2=1; }}

    Thanks in advance for any assistance.

How to find all the elements in a circular linked list
2 direct replies — Read more / Contribute
by punitpawar
on Feb 09, 2016 at 18:55
    Hello , I wanted to know if there is a way in perl to iterate a circular linked list and capture all the elements in it .
    so if I have a list like 2->4->5->4->6->2->7->8->2 , where after 8 it points back to the head 2 . Is thee a way in which I can iterate through this list and push the elements in an array ?
    I tried something like this below , but this does not seem to work. Any advise on how this can be solved ?
    Consider that the each node in the linked list is having the following data structure
    $head ={ data=>2 next=>undef; ## or the next node to which it is linked to }
    my @elements; my $curr=$head; ## I am trying to make my curr pointer start from t +he head while (1){ last unless ($curr->{next} == $head); # here I am trying to +check that the next element is not head. And if is then break out of +the loop print "data : $curr->{data} \n"; push(@elements,$curr->{data}); $curr = $curr->{next}; }
Accumulate values for each data field
2 direct replies — Read more / Contribute
by JobC
on Feb 09, 2016 at 17:03

    I have some CSV data like this:

    I want to accumulate the values of textbox95 for each value of textbox85.
    This would give a value of 6 to textbox95 when textbox85 eq "456.321.07" for record 1
    It should also eliminate all other records that contain "456.321.07" in textbox85

    Currently, I am reading this in line by line.
    Modifying the data fields as needed.
    I write to a new file the format that can be used at the next step.
    This strips the last two data fields, which help make the lines unique.
    However there is a sequence number in the first field that will be "skipping" numbers after records are removed.

    Ok, I modified my code to remove all of my processing. I am pretty new at perl, so I don't get the _DATA_ thing that GrandFather was mentioning below. (no I haven't taken the time to read the link either). When I run the code against the data above it produces errors. This was developed from the cryptic code NetWallah posted. It is possible and likely the errors are produced by my hack and slash of the original code to make it fit better in this forum.
    **Please Note** This is my second perl script if I am doing something wrong please let me know. Most of my script writing up until this point has been bash and MS-DOS Batch files, with a bit of Pascal and Java just to make it interesting.
    Thanks! jobc


    C:\A>perl test.csv Odd number of elements in anonymous hash at line 14. Reference found where even-sized list expected at line 14.

    My perl code

    #!/usr/bin/perl # use strict; use warnings; use Text::CSV_XS; use File::BOM; use Switch; my @rows; my @strings; my $CT = 0; my $i = 0; my %i = {$i}; my $input = "000"; # read in argument and use the first one as the filename to be read if (! defined $ARGV[0]){ print "Usage: ./ \"Please specify the filename like +\"123456.csv\"\"\n"; exit; } my $FileName = $ARGV[0]; # In a hex editor our source file shows \n or 0x0A or \010 characters +in the data fields. # Our next process wants to have all fields within quotes and separate +d by commas. my $aCSV = Text::CSV_XS->new ({ eol => undef, # \r, \n, or \r\n sep_char => ',', sep => undef, quote_char => '"', quote => '"', escape_char => '"', binary => 1, decode_utf8 => 1, auto_diag => 1, diag_verbose => 1, blank_is_undef => 0, empty_is_undef => 0, allow_whitespace => 1, allow_loose_quotes => 1, allow_loose_escapes => 0, allow_unquoted_escape => 0, always_quote => 1, quote_empty => 0, quote_space => 0, escape_null => 1, quote_binary => 1, keep_meta_info => 0, verbatim => 0, types => undef, callbacks => undef, }); open my $ORIG_CSV, '<:via(File::BOM)', "C:\\A\\$FileName" or die "Can +'t open C:\\A\\$FileName: $!"; open my $MOD_CSV, '>:raw:encoding(iso-8859-1)', "C:\\A\\ModCSV.csv" o +r die "Can't open C:\\A\\ModCSV.csv: $!"; open my $KBan_CSV, '>:raw:encoding(iso-8859-1)', "C:\\A\\kban.csv" or + die "Can't open C:\\A\\kban.csv: $!"; open my $CT_CSV, '>:raw:encoding(iso-8859-1)', "C:\\A\\ct.csv" or die + "Can't open C:\\A\\ct.csv: $!"; while (my $row = $aCSV->getline ($ORIG_CSV)) { if ($row->[0] =~ /\S/ ) { #many regex data modifiers removed from here if ($row->[1] =~ /^\d+/) { $i{$row->[3]}+=$row->[1]; print "$i{$_} $_+\n for sort keys %i"; } # post processing removed push @rows, $row; } close $ORIG_CSV; $aCSV->say ($MOD_CSV, $_) for @rows; close $MOD_CSV or die "Can't close C:\\A\\ModCSV.csv: $!"; close $KBan_CSV or die "Can't close C:\\A\\KBan.csv: $!"; close $CT_CSV or die "Can't close C:\\A\\CT.csv: $!"; }
How to encode apostrophe and quote using XML::Writer?
1 direct reply — Read more / Contribute
by bcurrens
on Feb 09, 2016 at 16:32

    (<, >, &) are encoded by XML::Writer, however (', ") are not. I need to encode both (', ") and I suspect my current solution is not very good. I have essentially made a placeholder for each character, and, after XML::Writer has rendered the XML I'm running a global search replace so I end of with the desired output.

    I should add that if I encode these characters before outputting with XML::Writer I get:

    <root> <str1>one&lt;two</str1> <str2>one&amp;two</str2> <str3>two&gt;one</str3> <str4>Caisse D&amp;quot;Eparge</str4> <str5>Caisse D&amp;apos;Eparge</str5> </root>

    The desired output is:

    <root> <str1>one&lt;two</str1> <str2>one&amp;two</str2> <str3>two&gt;one</str3> <str4>Caisse D&quot;Eparge</str4> <str5>Caisse D&apos;Eparge</str5> </root>


    use strict; use warnings; use Cwd; use XML::Writer; use XML::Writer::String; use HTML::Entities; use utf8; use Modern::Perl; my $str1 = qq(one<two); # &lt; (<) my $str2 = qq(one&two); # &amp; (&) my $str3 = qq(two>one); # &gt; (>) my $str4 = qq(Caisse D"Eparge); # &quot; ("); my $str5 = qq(Caisse D'Eparge); # &apos; ('); $str4 =~ s/\x{22}/\#\#\#doublequote\#\#\#/g; $str5 =~ s/\x{27}/\#\#\#apostrophe\#\#\#/g; say "str4: $str4"; say "str5: $str5"; my $file = cwd() . '/test.xml'; my $BOD = XML::Writer::String->new(); my $Writer = XML::Writer->new( OUTPUT => $BOD, DATA_MODE => 1, DATA_INDENT => 2 ); $Writer->xmlDecl("UTF-8"); $Writer->startTag('root'); $Writer->dataElement('str1', $str1); $Writer->dataElement('str2', $str2); $Writer->dataElement('str3', $str3); $Writer->dataElement('str4', $str4); $Writer->dataElement('str5', $str5); $Writer->endTag('root'); $Writer->end(); my $xml = $BOD->value(); $xml =~ s/\#\#\#doublequote\#\#\#/\&quot\;/g; $xml =~ s/\#\#\#apostrophe\#\#\#/\&apos\;/g; my $fh = new IO::File "> $file"; if (defined $fh) { print $fh "$xml"; $fh->close; } exit;
[OT] Software Architecture - Pipe and Filter
3 direct replies — Read more / Contribute
by trippledubs
on Feb 09, 2016 at 15:40

    Hi Monks,

    Is "Pipe and Filter" a real thing? We are learning about it in school, but I have my doubts. It is mostly hand wavy and any questions I ask are usually answered by 'depends on implementation'. The concept seems sufficiently vague that it could mean just chaining a bunch of Unix operations together using proc1(stdout) -> stdin(proc2) OR a legitimate software pattern in use today that achieves some higher degree of concurrency OR anything else OR nothing.

    The assignment leads me down the road of Java Spliterators and/or streams, but I am wondering if there is a Perl equivalent.

    I think it is originally from this 3.5 star 2 cent book Software Architecture: Perspectives on an Emerging Discipline. ISBN 9780131829572

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 contemplating the Monastery: (5)
    As of 2016-02-13 12:01 GMT
    Find Nodes?
      Voting Booth?

      How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

      Results (429 votes), past polls