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
search and replace if pattern found in file
2 direct replies — Read more / Contribute
by myfrndjk
on Jul 31, 2014 at 09:57

    Hi I am here to seek a help from you.I want to search and replace the particular line from text file.The issue is my text file contains "|" between each sentence.When i try to replace the line it ends up printing 3 times instead of 1 time.Thanks

    use strict; use warnings; my $old="welcome|to|chennai"; my $new="Capital of karnataka|is|bangalore"; my $base = (); my @base = (); my $file="c:/users/jeyakuma/desktop/search.txt"; open(BASE, $file) || die("Could not open file!"); @base=<BASE>; close (BASE); foreach $base(@base) { if($base =~ /$old/){ $base =~ s/$old/$new/gi; print ("Replaced!\n"); } open (BASE, ">$file"); print BASE @base; close (BASE); };

    input file contains

    welcome|to|chennai

    wants to replace that with

    Capital of karnataka|is|bangalore

    exp output

    Capital of karnataka|is|bangalore

    current result

    Capital of karnataka|is|bangalore|Capital of karnataka|is|bangalore|Capital of karnataka|is|bangalore
Sorting an Array
5 direct replies — Read more / Contribute
by burgerc
on Jul 31, 2014 at 09:40
    I have an array that stores a hash. I would like to sort the array on one of the fields in the hash. Here is how I create the array
    my @statements = (); for (@{ $data->{'mail-item'} }) { my %entry; $entry{job} = $_->{'mail-jobnum'}; $entry{product} = $_->{'mail-prodnum'}; $entry{statement_date} = $_->{'mail-statement-date'}; push(@statements, {%entry}); }
    How would I sort the @statements array? I would like to sort it on the $entry{statement_date}.

    The ultimate goal here is to display all statements in date orderd.

    Thanks

String processing
2 direct replies — Read more / Contribute
by dovah
on Jul 31, 2014 at 07:05
    Hi! I'd like to ask for help because my code isn't behaving correctly (and I can't figure out why). So, here's my issue. I have to deduce positions in a "simple string" given positions in a "complete string". Here's a minimal exemple of my input file:
    #name complete(cs) len(cs) simple(ss) len(ss) pos(cs) NAME1 A0AAA000AAA00A 14 AAAAAAAA 8 4,6 NAME2 AAAA0AA00000A 13 AAAAAAA 7 7
    Here's my code:
    $ perl -anle ' print "$_ position(cs)" and next if /^#/; printf "%s",$_; for $pos_ss (split ",",$F[5]) { $char = substr($F[1],$pos_ss-1,1); @cs = split //,$F[3]; @cs_idx = grep {$cs[$_] eq $char} 0..$#cs; push @res,++$cs_idx[$pos_ss-1]; } printf "%14s\n", join ",",@res; @res=(); ' file
    And here's my expected output:
    NAME1 A0AAA000AAA00A 14 AAAAAAAA 8 4,9 3,5 NAME2 AAAA0AA00000A 13 AAAAAAA 7 7 6
    In the provided exemple, I have to say that the 4th character (which is the 3rd "A") in the complete string(cs) corresponds to the the 3rd character in the simple string(ss), and so on... Could you please help me formatting/reviewing my code? Thanks in advance for your precious help!!
Perl script is getting stuck for no reason
8 direct replies — Read more / Contribute
by Ankur_kuls
on Jul 31, 2014 at 06:43

    I have a perl script which uses 2 inputs file of size 16gb & 800mb and creates an output file. It creates 4 hashes to store data from 800mb file and then compare and retrieve data from the other 16gb file and finally creates a report Now its working pretty fine but it is getting stuck at random creating the final report of around 500mb. No Idea why it is getting stuck. It doesn't seem a script bug as the 500mb data of report is generating perfectly. Please help me..not pasting the code here as it is too big to put...please let me know if you need further info. Thanks

    :) sorry for the trouble..I am pasting my code here...please help.

the last added element of a hash
4 direct replies — Read more / Contribute
by Lennotoecom
on Jul 31, 2014 at 04:29
    $h{'11c'} = 'C'; $h{'b'} = 'B'; $h{'e22'} = 'E'; $h{'aaaaa'} = 'AAAA'; for (keys %h){ $a = \$h{$_} and $b = $_ if $a < \$h{$_}; } print "$b\n";
    What do you think? Besides obvious.
Indent, anonymous sub, or lexical sub for module private code?
6 direct replies — Read more / Contribute
by 1s44c
on Jul 31, 2014 at 03:26

    Monks,

    I have a subroutine in a module that takes a data structure and runs the exact same page of code on two inputs from that structure. What's the recommended way to reuse that code?

    The code already has a bunch of loops and ifs so I'm hesitant to just wrap it in a another loop as it will disappear off the right of my screen. I don't really want a global sub that will only get called exactly twice. I think I want a lexical sub but they are experimental and others might want to run my code on perl <5.18. Is an anonymous sub the right thing here? Copy and paste just seems wrong.

    What's the best way to reuse my chunk of code?

    EDIT: Changed the title to something more descriptive.

printing the entire row with particular given last column criteria
3 direct replies — Read more / Contribute
by deepakshyl
on Jul 31, 2014 at 01:31
    INPUT
    orf00007					PHAGE_Prochl_MED4_213_NC_020845-gi|472340344|ref|YP_007673870.1|			  7665 8618	0.210897481636936
    orf00007					PHAGE_Prochl_MED4_213_NC_020845-gi|472340344|ref|YP_007673870.1|			  7665 8618	0.210897481636936
    orf00007					PHAGE_Prochl_P_HM2_NC_015284-gi|326783200|ref|YP_004323597.1|			  7665 8618	0.207761175236097
    orf00015					PHAGE_Megavi_lba_NC_020232-gi|448825467|ref|YP_007418398.1|			  11594 13510	0.278721920668058
    orf00015					PHAGE_Acanth_moumouvirus_NC_020104-gi|441432357|ref|YP_007354399.1|			  11594 13510	0.278721920668058
    
    THE SCRIPT I HAD IMPLEMENTED 
    
    
    use feature qw/say/; use Math::Trig; open FILE,"out02.txt"; my @file= <FILE>; close FILE; my $aa = 0; for (my $i =$aa; $i <=17822; $i++) { if(($file[$i] >= 0.210)){ open (OUTFILE,'>>out_t10-t10.txt'); print OUTFILE $file[$i]; } else{} }
    NOTE: 1) I need to take the last column as the analysing criteria to print the entire row(the float value, eg:0.210897481636936) 2) for example if the user input value is '0.210' we have to print the rows having >= values ,the expected output is OUTPUT orf00007 PHAGE_Prochl_MED4_213_NC_020845-gi|472340344|ref|YP_007673870.1| 7665 8618 0.210897481636936 orf00007 PHAGE_Prochl_MED4_213_NC_020845-gi|472340344|ref|YP_007673870.1| 7665 8618 0.210897481636936 orf00015 PHAGE_Megavi_lba_NC_020232-gi|448825467|ref|YP_007418398.1| 11594 13510 0.278721920668058 orf00015 PHAGE_Acanth_moumouvirus_NC_020104-gi|441432357|ref|YP_007354399.1| 11594 13510 0.278721920668058
Using Perl to access notes/memos on my mobile
3 direct replies — Read more / Contribute
by LanX
on Jul 30, 2014 at 20:23
    Hi

    Not sure if this is off-topic or even "over-topic" cause it involves plenty of non-perl technology.

    I'd like to be able to create, edit, update and delete text-files on my mobile device and to keep them synchronized with a folder on my desktop computer.

    I'm not sure about the easiest way to achieve this:

    Here some brainstorming:

    • using an email client and storing the text as drafts and using Perl to synchronise the folder
    • synchronize text-memos with google-drive, and access that web-folder via Perl
    • trying to figure out where text-memos are stored in the mobiles file system and trying to synchronize that folder directly
    • trying to find a browser-app which can access local html-files╣ and allows to change and safe them somehow
    • using some third party app
    • something I didn't think of yet ...
    Suggestions welcome! :)

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    ╣) this would be handy cause I could implement some JS logic in DHTML too, but the standard browser doesn't even support the file:// protocol

XML::Twig and file encoding
1 direct reply — Read more / Contribute
by slugger415
on Jul 30, 2014 at 13:33

    Hello, another XML::Twig question. My original files are UTF8 encoded, but after I run my script they are ANSI encoded. Special characters such as German small sharp s ( ß when encoded) still appear in the output, but don't display properly in my XML editor.

    I've set keep_encoding, which does save the file in UTF8, but produces some odd character strings and spacing.

    Any thoughts on this? thanks as always, Scott

Keeping browser active while computing in Perl/CGI
2 direct replies — Read more / Contribute
by vit
on Jul 30, 2014 at 10:26
    Dear Monks
    I am using Perl/CGI for my web application. It performs computation for 20 minutes or so. The browser does not keep session for such a long time and turns to some dummy page.
    Is there anything to keep it on my page till the end of computations?

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.