Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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
Matching probem
5 direct replies — Read more / Contribute
by Nansh
on May 29, 2017 at 01:52
    Hi,

    Hello, My file looks like below

    Eg:

    CARS TOYOTO Etios Innova Camry

    +Fortuner Corolla

    *.This is Class cars.

    CARS HYUNDAI Verna i10 i20

    +Eon Xcent

    +Tucson

    *.It is best cars.

    CARS HONDA City Brio Amaze

    +Accord

    *.This is Luxury cars.

    In my above file example under Toyoto what are all the cars are there that will come. same way in Honda and Hyundai as well.

    First i need to match the car companies and put that in an array. Then for each of that car company we need to match the cars and put them in other array

    For that my code is like below bt i am not able to get the correct output please help me to solve this
    #!usr/bin/perl use Data::Dumper qw(Dumper); open(DATA,"/something/something/something") || die("Couldnt open the f +ile \n"); while(<DATA>) { if(/^\s*CARS\s*[A-Z]/) { ($car)=$_=/^\s*CARS\s*(\w+)/; push(@car_list,$car); } } foreach $one_car(@car_list) { open(DATA,"/something/something/something")|| die("Couldnt open the +file \n"); while(<DATA>) { if(/^\s*CARS\b$one_car\b/) { last if/\*\.(\w+)/; $/="*"; $data_next=<DATA>; print"$data_next\n"; $/="\n"; } } }
JSON character encoding
1 direct reply — Read more / Contribute
by Bman70
on May 28, 2017 at 23:35
    Hello! I'm using an API request to get some video descriptions from Youtube. Something like:
    #!/usr/bin/perl use LWP::Simple; use lib 'JSON253/lib/'; use JSON qw( decode_json ); use CGI::Carp qw(fatalsToBrowser set_message warningsToBrowser); use strict; use warnings; my $url = "https://www.googleapis.com/youtube/v3/playlistItems?part=sn +ippet&key=$API_KEY"; my $result = get($url); die "Could not get $url!" unless defined $result;
    Then I decode the JSON response and load what I want into variables:
    $jsonstuff = decode_json($result); $description = $jsonstuff->{items}->{snippet}->{description};
    This all works, except that some video descriptions have weird characters in them. Some are emoticons. Here's an example from a description with an emoticon smiley at the end:
    "Happy with everything at the moment \x{1f60a}."
    Another quirk is that some apostrophes get mangled. Here's another printed description:
    "It’s the fans who make it worthwhile."
    I know it's a character encoding issue.. but is Youtube using a mix of encodings? I once got the apostrophe to print correctly by changing Windows-1252 to UTF-8 in one step. But the emoticons were still mangled. I should add that not all apostrophes are mangled. On some videos they come through fine. I wondered if it had to do with the uploader's text processing method. I've been searching most the day and trying various fixes, even from a number of perlmonks.org threads. But I haven't been able to make it work yet. Thanks
Working with folders in Perl and HTML
5 direct replies — Read more / Contribute
by LinuxNoob
on May 28, 2017 at 20:03
    Hello Gys, I feel so noob when it comes to Linux/Unix world as I'm an MS nerd (bad career choice)... I have NEVER scripted anything on Linux before but today I'm in an uncomfortable situation where I have to learn and develop a web interface in Perl that will just go get the size and the creation date/time a set of folders on a Windows Server (2012 r2)... This seems to be quiet simple for a Perl programmer but it's a complete trouble for a person like me... Can any one of you guys help me out with this? as I'm sure that this won't take a few minutes to be set up for people like you.
Counting matches
4 direct replies — Read more / Contribute
by Nicpetbio23!
on May 28, 2017 at 18:56
    Hi, I am new to perl scripting. Is it possible to create a script the counts the number of times that each string in one array matches a regex containing that string in another array. Thanks!
DBD::CSV - how to I coax it to read BOM prefixed files?
2 direct replies — Read more / Contribute
by ELISHEVA
on May 28, 2017 at 17:06

    I have a bom prefixed file that I'd like DBD::CVS (v. 0.49) to read and query, but when I tried I kept getting the error message Execution ERROR: Missing first row due to EIF - Loose unescaped quote . But there are no unquoted fields and the only thing I can think is happening is that it sees the BOM and thinks the first field of the first row begins without a quote. (For those not familiar with the DBD::CSV documentation "Loose unescaped quotes" are quotes embedded in a field that begins without a quote).

    I'm fairly certain the problem is the BOM because if I remove the BOM, the file can be read and loaded without error

    What's puzzling is that the underlying Text::CSV_XS module is BOM friendly. I've tried a variety of things to no avail, among them:

    • setting f_encoding => 'utf8'
    • setting csv_detect_bom => 1 and detect_bom => 1 in hopes that it would be passed onto the underlying Text::CSV_XS module
    • using an explicit column list (via col_names) and skipping the first line via csv_skip_first_row
    • setting csv_allow_loose_quotes => 1. That suppresses the EIF error message but replaces it with complaints that the column name stored in the first field in the first line doesn't exist - adding to my suspicions that it thinks the BOM is part of the field name

    I had considered just removing the BOM and ignoring the problem, but this isn't a one-off. A data source I don't control is producing these BOM prefixed files. I'd rather not be manually munging files (even with a script), every few months. Furthermore the source, or one of them, is the OECD. They produce a lot of data and I'd like that data to be accessible to other Perl programmers in CSV format, either through the solutions we discuss on this thread or because DBD::CSV has been enhanced to be BOM friendly.

    Many thanks in advance

.cgi?action params
2 direct replies — Read more / Contribute
by tultalk
on May 28, 2017 at 08:37

    Hi:

    Reluctant to come here for fear of being ruthlessly attacked. Just kidding. Very helpful.

    my GetLink = 'one&reaction=two'; query string xxx.cgi?action=' + GetLink;

    Want to recover the query params in:

    my $query = new CGI; my $action = lc ($query->param('action')); ....... elsif ($action eq "one"){ my ($string1, $string1 ) = @_; my ($string2, $string2 ) = @_; warn("string2 = '$string2'");

    or

    elsif ($action eq "one"){ my $query = CGI->new(); my $string2 = $query->param('reaction'); warn("string2 = '$string2'");

    Tried several other pieces of code. None work.

writing two files (different in length) to one output
4 direct replies — Read more / Contribute
by ic23oluk
on May 28, 2017 at 06:51

    Hi, I'm new to Perl, I'd like to create a script that reads two input files and writes their contents to one output file in a way that the first line of input 1 is followed by the first line of input 2 and so forth. If one input file is shorter than the other, it should return to the first line, which is the difficulty to me. check my code out:

    #! /usr/bin/perl use strict; use warnings; my $input1=<>; my $input2=<>; my $output = "output.txt"; # open (READ1, $input1) || die "Cannot open $input1: $!.\n"; chomp ($input1); open (READ2, $input2) || die "Cannot open $input2: $!.\n"; chomp ($input2); open (WRITE, ">$output") || die "Cannot open $output: $!.\n"; while (my $line= <READ1>.<READ2>){ chomp ($line); print WRITE "$line\n"; } close (WRITE); close (READ2); close (READ1); print "done\n";
reading files to different output files.
2 direct replies — Read more / Contribute
by ic23oluk
on May 28, 2017 at 06:37

    hello, I'm new to Perl! I'd like to write a script that obtains a list of files from the command line, read the files, and write their content to different output files. Here's my code:

    #! /usr/bin/perl use strict; use warnings; my $output = "output.txt"; open (WRITE, ">$output") || die $!; while (<>){ chomp $_; print WRITE "$_\n"; } print "done\n";

    How can I automatically create as much output files as needed without assigning them by myself, so that it works with any number of input files? Thanks in advance!

Storing the log file name as key/value in hash
2 direct replies — Read more / Contribute
by Magnolia25
on May 28, 2017 at 02:39
    Hi. I need a help on parsing the file name and save the file names contents into a hash as key/value. Below are the representation of my log file looks stored in an array
    ULTRIX.CW18.72.0.3.IP-HOST1.log DEC_DECSTATION.CW180.72.0.3.IP_HOST_AL.log DEC_DECSTATION_ADDR.CW180.72.0.3.IP_HOST_al2.log FOR_VISITORS23_HOST.HL617.253.1101.2.IP_HOST_hostinfo.log FOR_VISITORS24_HOST.HL617.253.1101.2.IP_HOST_hostinfo2.log FOR_VISITORS25_HOST.HL617.253.1101.2.IP_HOST_webform3.log
    File name format : <String(alphanumeric,_)>.<UniqueIdentifier(alphanumeric,.)>.<String(alphanumeric,_)>.log

    for example for below set of files

    DEC_DECSTATION.CW180.72.0.3.IP_HOST_AL.log DEC_DECSTATION_ADDR.CW180.72.0.3.IP_HOST_al2.log
    I need CW180.72.0.3 as my key, and DEC_DECSTATION , DEC_DECSTATION_ADDR as my values.

    specifically I want to store the result as below and write them later to a file as key and value.

    $HASH1 = { 'CW18.72.0.3' => ['ULTRIX'], 'CW180.72.0.3' => ['DEC_DECSTATION','DEC_DECSTATION_ADDR'], 'HL617.253.1101.2' => ['FOR_VISITORS23_HOST','FOR_VISITORS24_HOST','FO +R_VISITORS25_HOST'] };
    Please help on this. Thanks.
Is there a "standard" way to link to other modules in POD for a CPAN module?
4 direct replies — Read more / Contribute
by atcroft
on May 27, 2017 at 14:33

    Earlier this week, I was looking for information on a module on search.cpan.org. While reading through the module's documentation, there were several related modules mentioned, but the links referred to the module on metacpan.org.

    Curious, I viewed the module's POD, and found they had used L<text|url> POD linking syntax. When I later viewed the POD of a few other modules that referred (linked) to other modules in a way I expected, I found those modules using the L<module> / L<module/section> / L<text|module> / L<text|module/section> syntax.

    To me, this violated the Principle of Least Astonishment (Surprise), but I can see a scenario where the author may not have noticed the issue, such as if (for instance) they use metacpan.org as their preferred interface for finding modules on CPAN. I considered contacting the module's author regarding the linking, but before I potentially made (more of) a fool of myself (than normal) by doing so (and because I am still (at best) a novice with respect to POD), I wanted a "second opinion" (as it were). So, my question is:

    Is there a "standard" manner for linking to other modules within CPAN I should recommend, or should I just ignore it as being my issue, not an actual problem worthy of disturbing the author?

    My thanks in advance for your shared wisdom and consideration.


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 lurking in the Monastery: (5)
    As of 2017-05-29 12:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?