Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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
Number guessing game
1 direct reply — Read more / Contribute
by Anonymous Monk
on Nov 25, 2014 at 20:49

    I have everything working except I can not figure out a way to get it to say all of the previous guesses I can only get the last one. I've looked in all of the resource material I have and am missing it. Any help is appreciated

    #!/usr/bin/perl use Modern::Perl; my $target = (int rand 100) + 1; my $number = 99 ; GUESS: foreach my $guess_num (1..$number) { say "($guess_num) Please enter a guess:"; my $guess = <>; chomp $guess; unless ($guess =~ /^\d+$/) { say "This guess is out of range. Guess between 1 and 100!"; redo GUESS; } if ($guess == $target) { say "Congrats, you guessed right!"; last GUESS; } elsif ($guess < $target) { say "Your guess of $guess was too small."; say "your previous guesses:$guess,"; } elsif ($guess > $target) { say "Your guess of $guess was too big."; } if ($guess_num == $number) { say "You have used all your guesses. Please try again."; last GUESS; } }
perl to move outlook emails to another folder
1 direct reply — Read more / Contribute
by john.tm
on Nov 25, 2014 at 20:16
    Hi I have a script that reads thru an email folder and if the subject line starts with 'test' it extracts the mail body to a txt file. I would like it to then move the all emails to another folder. but while the script extracts from all the emails in the folder, i can only get it to move one email, not all of them.
    #!/usr/bin/perl use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; my $filename = 'c:\\test.txt' ; open(FH,"> $filename") or die ("cannot open $filename"); my $outlook = Win32::OLE->new('Outlook.Application') or die "Failed Opening Outlook."; my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->Folders("testmail")->Folders("test");#->Folde +rs; ("Junk Mail")->Folders("Bad"); my $tofolder = $namespace->Folders("testmail")->Folders("testout");#-> +Folders; ("Junk Mail")->Folders("Bad"); my $items = $folder->Items; for my $itemIndex (1..$items->Count) { my $message = $items->item($itemIndex); if( $message->{Subject} =~ /^test /){ print $message->{Subject}."\n"; print FH $message->{Body}; # $message->Move($tofolder); onmnly moves one message } } close(FH);
Downloading a web page over HTTPS?
2 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 25, 2014 at 19:31
    Hello Monks and Monkettes.

    I am attempting to download the following page:

    https://webapp4.asu.edu/catalog/course?s=MAT&n=243&c=DTPHX&t=2144&f=INTRT&r=44843

    so I can run a perl program (via a crontab entry) and monitor the current number of seats available for a specific instructor. I have tried using LWP::Simple's 'get' function, but I am receiving a 302 error. Any ideas would would be most appreciated!

    Thanks for the help,

    Christine
eliminating double quote
1 direct reply — Read more / Contribute
by pragov
on Nov 25, 2014 at 15:02

    Hi All, I use this in CGI

    <code> $PTName =~ s/,/','/g; if ($PTName) { $Where = $Where . "AND E.PT_name IN ANY ($PTName) "; } <code>

    to select multi names pairs from a list box isc01cfa,isc02cfa isc03ifa,isc04ifa

    But the teradata SQL output it gives ..........AND E.PT_name IN ANY ('isc01cfa','isc02cfa'',''isc03ifa','isc04ifa') It puts the two single quotes after the pair and before next pair. I need it to be as AND E.PT_name IN ANY ('isc01cfa','isc02cfa','isc03ifa','isc04ifa') How to modify the CGI?

    Thanks, Pragov

CAM::PDF cleanoutput Error
2 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 25, 2014 at 10:51

    when I save a PDF document with this code:

    print "open the original document \n"; my $pdf1 = CAM::PDF->new($doc1); print "save it to the new location \n"; $pdf1->output($newdoc1);

    I get no errors. However if I use this code:

    print "open the original document \n"; my $pdf1 = CAM::PDF->new($doc1); print "save it to the new location \n"; $pdf1->cleanoutput($newdoc1);

    I get this error:

    Bad request for object 1121 at position 0 in the file Use of uninitialized value in string eq at C:/Dwimperl/perl/site/lib/C +AM/PDF.pm line 4898. Not a ref at C:/Dwimperl/perl/site/lib/CAM/PDF.pm line 5273.

    In case it is helpful, the getPrefs method returns this list

    [ [0] undef, [1] undef, [2] 1, [3] 1, [4] 1, [5] 1 ]

    Doc1 is a PDF document that opens OK in Adobe reader and professional.

File size discrepancy
2 direct replies — Read more / Contribute
by wrkrbeee
on Nov 25, 2014 at 10:47
    Hi everyone, I downloaded a file from the SEC.GOV website, then used Perl's test operator "-s" to determine the size of the file, and received a result of 6,324,458 bytes. The same file is posted on the SEC's website with a size of 6,255,650 bytes. Why is my file size slightly larger? Tags? Other formatting? You can see the SEC's file at: http://www.sec.gov/Archives/edgar/data/6201/000000620109000009/0000006201-09-000009-index.htm Thanks!! Rick
Can't install Text::CSV:Encode in strawberry perl
1 direct reply — Read more / Contribute
by Skeeve
on Nov 25, 2014 at 08:59
    Hi! I'm trying to get Text::CSV::Encoded installed on strawberry perl but it fails and I have no clue how to fix it. Can anyone please have a look and give a hint?
    CPAN: CPAN::SQLite loaded ok (v0.204) Database was generated on Tue, 25 Nov 2014 13:16:47 GMT Running install for module 'Text::CSV::Encoded' CPAN: Digest::SHA loaded ok (v5.92) CPAN: Compress::Zlib loaded ok (v2.064) Checksum for C:\Users\skeeve\Desktop\Strawberry\cpan\sources\authors\i +d\M\MA\MAKAMAKA\Text-CSV-Encoded-0.22.tar.gz ok CPAN: Archive::Tar loaded ok (v2.02) CPAN: File::Temp loaded ok (v0.2304) CPAN: YAML::XS loaded ok (v0.52) CPAN: Parse::CPAN::Meta loaded ok (v1.4414) CPAN: CPAN::Meta loaded ok (v2.142060) CPAN: Module::CoreList loaded ok (v5.20140914) Configuring M/MA/MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz with Makefile.P +L Checking if your kit is complete... Looks good Generating a dmake-style Makefile Writing Makefile for Text::CSV::Encoded Writing MYMETA.yml and MYMETA.json MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz C:\Users\skeeve\Desktop\Strawberry\perl\bin\perl.exe Makefile.PL -- +OK Running make for M/MA/MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz cp lib/Text/CSV/Encoded/Coder/Base.pm blib\lib\Text\CSV\Encoded\Coder\ +Base.pm cp lib/Text/CSV/Encoded/Coder/Encode.pm blib\lib\Text\CSV\Encoded\Code +r\Encode.pm cp lib/Text/CSV/Encoded.pm blib\lib\Text\CSV\Encoded.pm cp lib/Text/CSV/Encoded/Coder/EncodeGuess.pm blib\lib\Text\CSV\Encoded +\Coder\EncodeGuess.pm MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz C:\Users\skeeve\Desktop\Strawberry\c\bin\dmake.exe -- OK Running make test C:\Users\skeeve\Desktop\Strawberry\perl\bin\perl.exe "-MExtUtils::Comm +and::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test +_harness(0, 'blib\lib', 'blib\arch')" t/*.t t/00_pod.t ................... skipped: Test::Pod 1.00 required for te +sting POD t/01_use.t ................... ok t/02_undef.t ................. ok t/03_attr_err.t .............. ok t/pp_01_base.t ............... ok t/pp_02_code.t ............... ok t/pp_03_file.t ............... 1/20 Cannot decode string with wide cha +racters at C:/Users/skeeve/Desktop/Strawberry/perl/site/lib/Encode.pm + line 241, <$fh2> line 2. Compilation failed in require at t/pp_03_file.t line 14. # Looks like you planned 20 tests but ran 12. # Looks like your test exited with 25 just after 12. t/pp_03_file.t ............... Dubious, test returned 25 (wstat 6400, +0x1900) Failed 8/20 subtests t/pp_04_uni_but_utf8.t ....... ok t/pp_05_guess.t .............. ok t/pp_06_coder_class.t ........ ok t/pp_07_coder_check_value.t .. ok t/xs_01_base.t ............... ok t/xs_02_code.t ............... ok t/xs_03_file.t ............... 1/20 Cannot decode string with wide cha +racters at C:/Users/skeeve/Desktop/Strawberry/perl/site/lib/Encode.pm + line 241, <$fh2> line 2. Compilation failed in require at t/xs_03_file.t line 16. # Looks like you planned 20 tests but ran 12. # Looks like your test exited with 25 just after 12. t/xs_03_file.t ............... Dubious, test returned 25 (wstat 6400, +0x1900) Failed 8/20 subtests Test Summary Report ------------------- t/pp_03_file.t (Wstat: 6400 Tests: 12 Failed: 0) Non-zero exit status: 25 Parse errors: Bad plan. You planned 20 tests but ran 12. t/xs_03_file.t (Wstat: 6400 Tests: 12 Failed: 0) Non-zero exit status: 25 Parse errors: Bad plan. You planned 20 tests but ran 12. Files=14, Tests=235, 3 wallclock secs ( 0.08 usr + 0.03 sys = 0.11 +CPU) Result: FAIL Failed 2/14 test programs. 0/235 subtests failed. dmake.exe: Error code 153, while making 'test_dynamic' MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz C:\Users\skeeve\Desktop\Strawberry\c\bin\dmake.exe test -- NOT OK //hint// to see the cpan-testers results for installing this module, t +ry: reports MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz Stopping: 'install' failed for 'Text::CSV::Encoded'.
    I'm currently using Strawberry Perl Sep 2014 / 5.20.1.1 / 32bit P.S. I also opened an issue on github https://github.com/makamaka/Text-CSV-Encoded/issues/2 and just now noticed there are already replies

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Perl structure to Javacript-ready JSON
4 direct replies — Read more / Contribute
by Jeppe
on Nov 25, 2014 at 08:21
    Esteemed monks, I'm essentially trying to escape the contents of a JSON structure.
    my $hashref = { abc => "abc'abc" };
    This should become
    {"abc":"abc\'abc"}
    , and it must work for blessed references as well. I'm currently using the JSON module, but I'm not married to it. Ideally, this would have been resolved by a hook in the JSON module - but that does not seem to be available. What other options do I have?
Avoid recursive replacement using regex
4 direct replies — Read more / Contribute
by anirudhkumar_r
on Nov 25, 2014 at 06:21

    Hi Monks,
    How are you doing today? I am in a little trouble and need your help with regex.
    I am replacing some strings in JavaScript. However, I believe the same regex will work for both JavaScript and Perl.

    With my method of replacing, I am getting into the problem of recursive replacement.
    I want to do the following.
    1. Replace &amp; with &
    2. Replace &amp;lt; with &lt;
    3. Replace &amp;gt; with &gt;
    4. Replace &lt; with <
    5. Replace &gt; with >

    I tried to include the following code snippets in my JS code.

    new_json_string = new_json_string.replace(/&amp;/g, '&'); new_json_string = new_json_string.replace(/&lt;/g, '<'); new_json_string = new_json_string.replace(/&gt;/g, '>');
    But the problem with the above code snippet is, both text segments like [&lt;] and [&amp;lt;] will be replaced ultimately with [<] symbol. Is it possible to include all the three replacements in a single statement to avoid recursive replacement?
    Your help is highly appreciated, Thank you very much in advance.

Regex hyphen
4 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 25, 2014 at 05:31

    Dear Monks

    I need to match entire words in strings. Words may or may not include an hyphen. Why is the following not working?

    $mystring =~/\b($query)\b/i

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 scrutinizing the Monastery: (9)
    As of 2014-11-26 03:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (162 votes), past polls