Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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
Processing JSON with Perl
2 direct replies — Read more / Contribute
by DanielSpaniel
on Feb 17, 2017 at 14:42
    Hi,

    I seem to be having troubles and misunderstandings with processing JSON. My difficulty is likely in large part due to the fact that I don't really seem to understand the variable structures properly.

    So, I have a JSON file, and I've a script which is using the JSON module, and I've decoded the data, etc, and now have a little loop where I want to work my way through the file and print it out. However, although I can print a few bits from the top level of the structure, I'm having problems trying to access anything else. FYI the JSON file is data retrieved from one of the wikimedia sites, but it seems valid.

    The relevant Perl code is immediately below, while the JSON file data, as output by Dumper is below that:

    my $data=decode_json($rawdata); # print Dumper($decoded); for ( @{ $data->{sections} } ) { # This line works; prints the 4 titles (one from each section) print $_->{title}."\n"; # No image info' in file anyway, so no output expected, but not sure i +f code is correct for ( @{ $_->{images} } ) { print $_."\n"; } # Nothing gets printed, but no error (figure it should print something + from top section) print $_{content}->{text}."\n"; # This fails with "Not a hash reference" # for ( @{ $_->{content}->{elements} } ) # { # print $_->{text}."\n"; # } }
    The JSON data, as output by Dumper, is below: (text edited for readability, so it fits nicely)
    $VAR1 = { 'sections' => [ { 'images' => [], 'level' => 1, 'content' => [], 'title' => '10-minute Turkey' }, { 'images' => [], 'level' => 2, 'content' => [ { 'text' => 'Makes 4 servings.', 'type' => 'paragraph' } ], 'title' => 'Description' }, { 'images' => [], 'level' => 2, 'content' => [ { 'elements' => [ { 'elements' => [], 'text' => 'abc' }, { 'elements' => [], 'text' => 'efg' }, { 'elements' => [], 'text' => 'hij' }, ], 'type' => 'list' } ], 'title' => 'Ingredients' }, { 'images' => [], 'level' => 2, 'content' => [ { 'elements' => [ { 'elements' => [], 'text' => 'tuv' }, { 'elements' => [], 'text' => 'wxy' }, { 'elements' => [], 'text' => 'z12' }, ], 'type' => 'list' } ], 'title' => 'Directions' } ] };

    The actual URL I'm retrieving, for testing, is: http://recipes.wikia.com/api/v1/Articles/AsSimpleJson?id=24372

    So, I don't really seem to get how to access anything which is at a lower level, such as {sections}->{content}->{elements}->{text}, or {sections}->{content}->{text}. I'm also a bit confused as to how to distinguish, programatically, between the two bottom sections (i.e. "Directions" and "Ingredients"); for instance, how, if I only wanted "text" fields from the latter, could I distinguish it, in code, from the former (i.e. the third section, titled "Ingredients"?

    Any assistance would be most welcome.

    Thanks!
Strawberry/ActiveState Perl 5.24.1 - Invalid parameter - -R error when invoking perldoc
3 direct replies — Read more / Contribute
by pritesh_ugrankar
on Feb 17, 2017 at 14:11

    Hi

    The following error is noticed when I installed either strawberry perl or activestate perl and invoke perldocs:


    Invalid parameter - -R

    I tried re installing several times, still the same error. I did google but could not find anything affirmative or a workaround. Am I missing something or doing something wrong?

Excel Button to Run Perl
3 direct replies — Read more / Contribute
by rd48sec
on Feb 17, 2017 at 14:01
    Still new to PERL. I have an Excel workbook cell that contains data I want to pass to a PERL script. I want to just push a button in the worksheet that passes the cell value to my PERL script, and run the script. Thank you for any starting point.
sourcing the csh script issue
3 direct replies — Read more / Contribute
by lpl
on Feb 17, 2017 at 10:35
    hi, i have sourcing script source.csh,in perl i sourced by using the system("csh source.csh")but when sourcing is done it was not setting any required variables, but wen i ran a script in exterm using source source.csh the required vairables are available but in perl source is not supporting how to solve this issue plaz help.....
How to read the value of a variable (read from a Excel file converted Into text file) and put then back into Excel column
1 direct reply — Read more / Contribute
by rockyurock
on Feb 17, 2017 at 08:51
    I am able to read the variables from excel and print them in the above program but I want to read these variables(from Variables to read from Excel sheet :) value from Input data (from My input data to search from:) and put them back into excel sheet which I am not able to do. can some one pls help on the same ?
    Variables to read from Excel sheet : isim = usim = sim_invalid = pcscf_discovery_flag signaling_flag ran_type emergency_ind rat pcscf_v4 pcscf_v6 plmn is_ems_support ipv4 ipv6
    My input data to search from: 8077 8116 D type hexa usim = 1, isim = 1 8077 8116 D type hexa usim = 1, isim = 1 pcscf_v4 : num = 2, protocol_type = 0x21, port_num = 0, addr = 10.56.5 +.85 8088 1223 D temp sim_invalid = 0 8099 1223 XX is_ems_support = 1 #88 8099 1224 XX pdn_act_ind->pcscf_discovery_flag = 1 ind->signaling_flag = 1 some text here plmn = 405872 DefaultStatusBarPlmnPlugin: into updateCarrierLabel ipv6 = 36.5.2.4.25.143.200.141.0.0.0.0.32.20.232.161 get_ipv6_prefix,temp ipv4 = 10.167.185.101 _send_ipv4_notify
    MY program:
    #use strict; #use 5.010; use Spreadsheet::Read qw(ReadData); my $book = ReadData ('UE_NW_Parameters.xlsx'); #my @files = grep { -f } (<*.txt>,<*main_log>,<*Project>,<*properties> +); my @files = grep { -f } (<*main_log>); my @rows = Spreadsheet::Read::rows($book->[1]); foreach my $i (1 .. scalar @rows) { foreach my $j (1 .. scalar @{$rows[$i-1]}) { my $temp = "\n$rows[$i-1][$j-1]"; my @keywords = "$temp"; print @keywords; } foreach my $file (@files){ open(my $fh, '<', $file) or die $!; my @content = <$fh>; close($fh); my $l = 0; foreach my $kw (@keywords) { foreach (@content){ $l++; my $search = $kw; if ($search =~ /(\ d+)/) { printf 'Found keyword %s in file %s, line %d:% +s'.$/, $search, $file, $l, $_; last; } } } } }
How to detect non printable characters and non white space characters? [RESOLVED]
3 direct replies — Read more / Contribute
by thanos1983
on Feb 17, 2017 at 04:49

    Hello again Monks,

    Lately I have been bombing the forum with questions but no matter how much I experiment with my code I can not figure out the solution(s) to my problems this is why I keep asking questions over and over.

    To the question, I have a hash of hashes with hundreds values on each hash. I want to use a negative condition on both detecting white space character or non printable character. In such a case I want to delete the element from the hash (if does not contain white space character or non printable character).

    I tried the conditions separately and they work just fine, at least based on my experimentation examples. I need to combine them in a nested if because a value with no spaces it does not mean that it does not contain special characters.

    Based on my research in order to detect non printable characters you can use either this /[^[:print:]]/g or this /[^[:ascii:]]/ regex expression found here (Finding out non ASCII Characters in the text)

    Sample of code:

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my $str = 'a[bdy]dfjaPÑsdafÜ'; my $str_2 = 'WAP'; my $hoh_ref = { hash_1 => { a => 'a[bdy]dfjaPÑsdafÜ', b => 'WAP' }, hash_2 => { c => 'Te st' } }; print Dumper $hoh_ref; foreach my $key (sort keys %{$hoh_ref}) { foreach my $value (keys %{$$hoh_ref{$key}}) { # If not white space character or non printable character remove e +lement if ($$hoh_ref{$key}{$value} !~ /[^[:print:]]/g || $$hoh_ref{$key}{$value} !~ /\s/) { delete $$hoh_ref{$key}{$value}; } elsif ($$hoh_ref{$key}{$value} =~ /[^[:print:]]/g) { while ($$hoh_ref{$key}{$value} =~ /[^[:print:]]/g) { print "Non Printable Characater:\t$&\n"; } } } } print Dumper $hoh_ref; __END__ $VAR1 = { 'hash_2' => { 'c' => 'Te st' }, 'hash_1' => { 'b' => 'WAP', 'a' => 'a[bdy]dfjaPÑsdafÜ' } }; $VAR1 = { 'hash_2' => {}, 'hash_1' => {} };

    Desired output would be:

    $VAR1 = { 'hash_2' => { 'c' => 'Te st' }, 'hash_1' => { 'a' => 'a[bdy]dfjaPÑsdafÜ' } };

    Thanks in advance for your time and effort.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Parse for a name after a keyword search in all the files, given directory as input
7 direct replies — Read more / Contribute
by prash_sri
on Feb 17, 2017 at 02:19
    #!/usr/local/bin/perl $dir = $ARGV[0]; open(HANDLE,$dir) || die "ERROR in opening file"; @allfiles = readdir HANDLE; close(HANDLE); foreach $files (@allfiles) { open(FH,"<",$files)|| die "ERROR in opening file"; foreach $line (<FH>) { $line =~ m/ \^module\ /g ; @next = split(/\s+/,$line); } close(FH); } print "$next[0]\n"

    Trying to write a perl code which browses all the files recursively under a given input directory and do the following: - Parse all the list of module names. - Parse all the instance names inside each of the modules. Kindly let me know where am I going wrong!! And guide me for obtaining the correct output. Thanks

PDF::PDFUnit notContaining
No replies — Read more | Post response
by andreas1234567
on Feb 16, 2017 at 16:51
    Dear Monks

    PDF::PDFUnit let's us verify PDF documents using perl. It seems to do a good job, except for the notContaining function. It seems to always return true (here a rewritten version of 01-load.t). The basic idea is to detect presence of unwanted words, such as the name of the former CEO:

    $ cat t/01-test-mod.t use strict; use utf8; use warnings; use FindBin; use File::Spec; use Test::More; use Test::Exception; use PDF::PDFUnit qw(:skip_on_error); my $resources_dir = File::Spec->catfile($FindBin::Bin, 'resources'); my $pdfReference = "$resources_dir/reference.pdf"; lives_ok { AssertThat ->document($pdfReference) ->hasText() ->containing("Hello") } "containing Hello"; lives_ok { AssertThat ->document($pdfReference) ->hasText() ->notContaining("Hello") } "notContaining Hello"; diag $@->getMessage() if $@; done_testing(); __END__ $ perl t/01-test-mod.t ok 1 - containing Hello ok 2 - notContaining Hello 1..2 $
    Do anyone have experience with this module?

    openjdk-8-jre:amd64 + Ubuntu 16.04.1 LTS + perl v5.22.1

    BR
    Andreas

    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Critical sections; In the Perl interpreter
4 direct replies — Read more / Contribute
by Wiggins
on Feb 16, 2017 at 13:45
    My interest is in critical sections in the Perl interpreter itself.
    I was asked by a friend, why after starting 150 web spider threads, his system would bog down until it seem to stop, but was still running?

    I proposed that each of those threads was a separate process, with each having a full Perl interpreter. But they were all sharing the same .so libraries, and the number of critical sections(CS) that the interpreter had to go through would create single file bottle-neck after bottle-neck.

    Are there CS in the interpreter? Are there certain functional areas that have significantly more CS than others

    It is always better to have seen your target for yourself, rather than depend upon someone else's description.

Can't find a module that is installed
2 direct replies — Read more / Contribute
by Weebo
on Feb 16, 2017 at 13:24
    Forks.pm is giving me problems with MAKER again, you'd think I would become a Perl coder instead of jsut a Perl user by now...

    I tried running MAKER on a new cluster I installed it on and got the following response for every MPI process I created

    Can't locate forks.pm in @INC (you may need to install the forks modul +e) (@INC contains: /Data/Apps/maker/bin/../perl/lib /Data/Apps/maker/ +bin/../lib /Data/Apps/maker/bin/../src/inc/perl/lib /Data/Apps/CEGMA_ +v2.5/lib /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.22.1 /usr/l +ocal/share/perl/5.22.1 /usr/lib/x86_64-linux-gnu/perl5/5.22 /usr/shar +e/perl5 /usr/lib/x86_64-linux-gnu/perl/5.22 /usr/share/perl/5.22 /usr +/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /Data/ +Apps/maker/bin/maker line 42.

    So I went looking

    locate forks.pm /home/seth/.cpan/build/forks-0.36-0/blib/lib/forks.pm /home/seth/.cpan/build/forks-0.36-0/lib/forks.pm /usr/local/lib/x86_64-linux-gnu/perl/5.22.1/forks.pm
    Now, I might be crazy, but /usr/local/lib/x86_64-linux-gnu/perl/5.22.1 seems to be in both. Why can't it find forks.pm?

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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (8)
    As of 2017-02-22 16:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Before electricity was invented, what was the Electric Eel called?






      Results (331 votes). Check out past polls.