Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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
Count of articles Google News
2 direct replies — Read more / Contribute
by madsoeni
on Feb 28, 2015 at 08:15
    Dear all, I am a real newbie in Perl and from a professor in another university I found a perl code able to count the number of articles in google news mentioning a given keyword by day and country. To be clearer, assume that I want count the number of articles mentioning "hello" in UK day by day. I am using WWW::Mechanize and the code I wrote is the following:
    #!/usr/bin/perl -w use WWW::Mechanize; #activate scraper package # waiting time between observations $sleep_per_obs = 5; # www:mechanize agent my $agent = new WWW::Mechanize(onerror => undef); # Safari browser $agent->agent_alias( 'Mac Safari' ); # target file my ($target) = 'data_uk.txt'; print "Data will save to $target \n"; open ($target, '>', $target) or die ("Sorry, couldn't open $target for + writing. \n"); # term to search $term = "hello"; print "Search term is ".$term . ".\n"; for($year=2012;$year<=2014;$year++){ for($month=01;$month<=12;$month++){ for($day=01;$day<=31;$day++){ $url = "$term&hl=en&gl=uk&authuser=0&s +a=X&ei=xXJuUp6tMoLcyQGxp4GgCw&source=lnt&cr=countryUK&tbs=cdr%3A1%2Cc +d_min%3A$month%2F$day%2F$year%2Ccd_max%3A$month%2F$day%2F$year&tbm=nw +s"; # print "URL is ".$url."\n"; $agent->get($url); $content = $agent->content(); $content =~ /(\d+),*(\d*) results/; #assigns results (thousands and hundreds = $1 and $2) to variables my ($results1, $results2) = ($1, $2); if ($results2 eq "") { $combo = $results1; } else { $combo = ($results1*1000+$results2); } if ($combo eq ""){ $combo=0; } print "Number of results $day-$month-$year : $combo \n"; print $target "$day-$month-$year: $combo \n"; sleep 5; } } } close $target;
    However, after a while I get stopped and the script returns 0 results, even though I change the waiting time for each request. Does anyone know where I am wrong? Thank you in advance.
Deleting Chinese characters while reading
3 direct replies — Read more / Contribute
by alexharv074
on Feb 28, 2015 at 04:14
    Hi all I have a finicky little problem in a perl program I've written and have no idea how to go about solving it.
    My program asks the user to enter text in either English or Simplified Chinese via a line that just says
    $response = <STDIN>;
    I have the following statements at the beginning of my program:
    use utf8; use open ':encoding(utf8)'; binmode(STDOUT, ':utf8'); binmode(STDIN, ':utf8');
    (Do I really understand what these lines do? No. :)
    Anyhow, just about everything works the way I want it to except for one little problem: if you type a Chinese character and then try to delete it again using the delete or backspace key, only half of it gets deleted.
    Does anyone know a solution to this?
    The platform is Mac OS X although I'd hoped my app would work on linux too.
What is backslash and number means in string
3 direct replies — Read more / Contribute
by Anonymous Monk
on Feb 27, 2015 at 17:54

    I saw string like this:

    print "\353\257\274\354\243\274\355\231\224!"

    is it hexademical or demical? and is it unicode codepoint? what is difference between \x<number> and \<number> ? sorry for english

OSX File::Find avoid automounted file systems
1 direct reply — Read more / Contribute
by CarolinaPerler
on Feb 27, 2015 at 16:14

    As the title says, 'have an application that runs on several Linux, UNIX, and Windows variants, plus OSX. The application periodically has to search the file systems for certain files or files with certain characteristics.

    The general flow is to surf the file system and stuff the file paths and details into a SQLite DB.

    We need to give our users the option to include remote file systems from the search or include them. Remote meaning just about anything that is not on an internal hard drive (e.g. CD/DVD drive, NFS, etc.).

    On all other platforms, we have found robust ways to avoid NFS mounts and include those tricks in the File::Find preprocessing. However, on OSX the general approach to managing NFS mounts is vi automount/autofs.

    I cannot figure out a straight forward approach to identifying automounted file systems. I'm about to fall back to parsing the /etc/auto_master file and then following the directives therein and any directory includes to identify potential mount points. That will not be trivial.

    The question - is there a more available approach to identifying remote file systems on a Mac?

Cutting last word from a output line
5 direct replies — Read more / Contribute
by Bharath.M.R
on Feb 27, 2015 at 14:05
    Hi Experts, I am new to perl. I want to cut the last word from command line output from one of the command AND assign that to a variable so that i can use it in my rest of the program. Like as in shell variable =`echo $line| awk -F, '{print $NF}'` please suggest.


    . Thanks Karl, I just gave an example how i was achieving in unix shell. Ok i will try to re-phrase the question. I am expecting like below. Assume below is an system command: "snmpwalk.ovpl -c $string $IP sysName" here windows system command "snmpwalk.ovpl -c $string $IP sysName" will give me output like below. sysName.0 : OCTET STRING- (ascii): So as said earlier i am expecting something like below.: my $variable = `system (sysName.0 : OCTET STRING- (ascii): | cut last word` expected $

-e not working Perl 5.008008
3 direct replies — Read more / Contribute
on Feb 27, 2015 at 13:00
    My Perl script works fine on my local win7 computer using Apache24 Perl 5.016003 when using the -e file exists operator to test the existence of a regular filename such as "cero.png" and accented filename such as "número.png". However, when I upload that Perl script to my SiteGround host server running Perl 5.008008, the same script will work for a regular filename such as "cero.png", but not for any accented filename such as "número.png". Any suggestions. Here's part of that code, testing if a filename with a png file extention exists in my directory. $c="$dimg/$esp.$epng"; if (-e $c) { ...
Using indexing for faster lookup in large file
7 direct replies — Read more / Contribute
by anli_
on Feb 27, 2015 at 12:55
    Hi everybody.

    I'm trying to get a grip on indexing and how it works. My current problem is that I would like to make a simple text lookup in a very large file.
    My query would be something like '3005698' and the database I want to search has the following structure:

    3005696;Homininae;Homo;Homo sapiens;
    3005698;9606;Homininae;Homo;Homo sapiens;
    3005690;90371;Enterobacteriaceae;Salmonella;Salmonella enterica
    3005700;9606;Homininae;Homo;Homo sapiens;

    The output I would like would be something like: Homininae,Homo,Homo sapiens

    One way would be to use bash grep and do a search like:

    grep "^3005698;" database.txt
    Then I could parse the output to make it pretty.

    Using perl, they way I would normally do it would be to generate a hash of the database and then do my lookups from that, like so

    open IN, '<', "/path/to/database.txt"; my %hash; while (<IN>) { my ($first,@array) = split(/;/, $_); @{ $hash{$first} } = @array; } close IN; print $hash{'3005698'}[1] . " "; print $hash{'3005698'}[2] . " "; print $hash{'3005698'}[3] . "\n";
    The problem I have with this is that the database is around 30Gb, so it would be a very slow and memory consuming process. So my question is, can I somehow index the database, so I know where in the file the query '3005698' resides, to speed up this process.
ip_match doesn't seem to work
3 direct replies — Read more / Contribute
by mwhiting
on Feb 27, 2015 at 10:54

    This is something of a ressurection of a question from this past month.

    I am using Session Id's and have started trying to use -ip_match so that you can't cut and paste an url with the session id embedded in it from one computer to the next. But since I have added -ip_match I don't get any different results when accessing the same script from computers with different IP's. I modified my Use statement to be:

    use CGI::Session qw/-ip-match/;

    instead of my usual use cgi::Session; line. I also tried adding

    $session::IP_MATCH = 1;

    to the mix, after the creation of the session object, and still I see no difference in the results on browsers with different ip's. The different computers still access the session info just fine and pass the authentication using info in the session variable.

    What am I missing here in the use of -ip_match ? Your help would be greatly appreciated!   :)

perl bar graph
2 direct replies — Read more / Contribute
by poss
on Feb 27, 2015 at 10:34

    I have a perl script using CGI to create a stacked Bar graph. I am plotting Build Times and Kitting times per platform. my data plot looks like.
    @data2 = ('Windows', 'Linux', 'Solaris', 150,250,350, 100,200,300);
    However if I want to reverse the way it is displayed, the plotting works ok, but the values do not follow the bar. So reversing the above data points to
    @data2 = ('Windows', 'Linux', 'Solaris', 100,200,300, 150,250,350);
    The values remain the same. I have cumaulate => 1 and show_values=> 1. How do I get the values to follow the bars? Here is my graphing points.
    my $graph = new GD::Graph::bars(900,600);
    $graph->set( x_label => $x_Label,
    x_label_position => 1/4,
    x_label_vertical => 1,
    y_label => 'Minutes',
    y_max_value => 300,
    y_tick_number => 10,
    title => $Graph_Title,
    bar_width => 35,
    cumulate => 1,
    dclrs => 'lblue', 'cyan',
    bgclr => 'white',
    fgclr => 'lred',
    values_vertical => 0,
    valuesclr => 'black',
    accentclr => 'red',
    shadowclr => '#F7E7CE',
    shadow_depth => 0,
    legendclr => 'blue',
    show_values => 1);

    I hope I provided enough information. This is driving me nuts! Thanks

How can I add a -id attribute to a perl cgi start_Tr ?
1 direct reply — Read more / Contribute
by misterperl
on Feb 27, 2015 at 10:28
    According to the CGI docs, the start_Tr tag generator is supposed to support *event* and *global* HTML attributes, as well as the 5-6 normal <tr> ones. One of the globals is id=. I tried
    $cgi->start_Tr( -id => "thisID" )
    and that yielded <tr> only- the attribute was ignored. Even common <tr> attributes aren't working like valign.
    $cgi->start_Tr( -valign => "top" )
    also yields simply <tr>.

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!
  • 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?

    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 rifling through the Monastery: (6)
    As of 2015-03-03 05:09 GMT
    Find Nodes?
      Voting Booth?

      When putting a smiley right before a closing parenthesis, do you:

      Results (64 votes), past polls