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.

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.

Post a new question!

User Questions
efficient perl code to count, rank
9 direct replies — Read more / Contribute
by Perl_Noob2021
on Jul 17, 2021 at 13:34
    Need help for possible efficient perl code to count, rank and get the percentage. I have the following code, already running for 6hours and not yet complete. The target filed is a csv (all text) and is about 14m to 14.5m rows, and around 1100 to 1500columns and 62gig size A run of 4hours is acceptable. what it does: - do a count (like a countif in excel) - get the percent (based on 14m rows) - get the rank based on count Appreciate any help.
    $x="Room_reserve.csv"; $in = "D:\\package properties\\${x}.csv"; $in = "D:\\package properties\\${x}.csv"; $out = "D:\\package properties\\output\\${x}_output.csv"; open($fh, '<', $in) or die "Could not open file '$file' $!"; @data = <$fh>; close($fh); %counts; @columns; $first = 1; #counter foreach $dat (@data) { chomp($dat); @rows = split(',',$dat); if ($first == 1) { $first = 0; next; } else { $count = 1; foreach $i (0..$#rows) { if ( exists($columns[$i]{$rows[$i]}) ) { $columns[$i]{$rows[$i]}++; } else { $columns[$i]{$rows[$i]} = int($count); } } } } #output $first = 1; open($fh, '>', $out) or die "Could not open file '$file' $!"; foreach $dat (@data) { chomp($dat); @rows = split(',',$dat); foreach $i (0..$#rows) { if ($i > 6) { #for modifying name if ( $first == 1 ) { $line = join( ",", "Rank_$rows[$i]", "Percent_$rows[$i]", "C +ount_$rows[$i]", $rows[$i]); print $fh "$line,"; if ( $i == $#rows ) { $first = 0; } } else { @dat_val = reverse sort { $a <=> $b } values %{$columns[$i]} +; %ranks = {}; $rank_cnt = 0; foreach $val (@dat_val) { if ( ! exists($ranks{$val}) ) { $rank_cnt++; } $ranks{$val} = $rank_cnt; } $rank = $ranks{$columns[$i]{$rows[$i]}}; $cnt = $columns[$i]{$rows[$i]}; $ave = ($cnt / 14000000) * 100; $line = join( ",", $rank, $ave, $cnt, $rows[$i]); print $fh "$line,"; } } else { print $fh "$rows[$i],"; } } print $fh "\n"; } close($fh);
Stopping excessive periods in email
7 direct replies — Read more / Contribute
by htmanning
on Jul 16, 2021 at 14:48
    Monks,

    I'm trying to stop bogus Russian hackers who use bogus email addresses. I'm trying to detect more than 2 dots in an email string. For example, test.test@gmail.com is fine, but test.test.test.test@gmail.com is not. The following seems to work, but I wonder if someone can validate what I have. I first detect if there are two at signs that signal they are trying to bcc others, and then I look for a comma, and then the periods. Is this right?

    if ($sender =~ /\@.*\@|,|\..*\..*\.|\n/i) {
    Thanks.
Loading browser cookies into WWW::Mechanize
2 direct replies — Read more / Contribute
by bakiperl
on Jul 16, 2021 at 11:58
    Folks,
    I need help with WWW::Mechanize to automate a login to a page that uses Duo-Authentication. I am able to load the credentials and login but I need to silently pass the second check using the information that is saved in the cookies by the browser (Chrome or any other browser if necessary). Once this is achieved I should be able to things I was able to do before the implementation of the Duo-Authentication.

    This call
    $cookie_jar->load($cookie_file);
    returns this warning: "Decrypted cookies is not set up at..."
    Here is what I have so far.
    #!/usr/bin/perl -w use strict; use Data::Dumper; use WWW::Mechanize; use HTTP::Cookies::Chrome; my $username = '**********'; my $passwd = '*********'; my $cookie_file = 'C:/Users/username/AppData/Local/Google/Chrome/User +Data/Default/Cookies'; #chrome cookies path on windows my $cookie_jar = HTTP::Cookies::Chrome->new( # chrome_safe_storage_password => '***", # I don't know what t +o do here file => $cookie_file, # autosave => 1, # ignore_discard => 0, ); $cookie_jar->load($cookie_file); my $mech = WWW::Mechanize->new( cookie_jar => $cookie_jar, ); #$mech->cookie_jar->scan(sub { print Dumper \@_ }); #print $cookie_jar->as_string; my $uri = URI->new( "https://www.*****.com" ); $mech->get( $uri ); unless ($mech->success) { my $mesg = $mech->response->status_line; print $mesg; goto END; } $mech->set_fields( username => $username, password => $passwd, ); $mech -> submit(); sleep(2); $mech->success() or die "Can't fetch the Requested page"; my @form = ( "formname" ); $mech->form_with_fields( @form ); my $value ='******'; eval {$mech->select('fieldName', $value)}; if ($@ and $@ =~ /Illegal value/) { print "This page has not been loaded..."; goto END; } END :
total Noob Comma/Raku Questions here?
1 direct reply — Read more / Contribute
by misterperl
on Jul 16, 2021 at 11:43
    Hello friends,

    A Damian talk persuaded me to look at developing on Raku in Comma IDE. Is that entirely outside of the Blessed Perlmonks scope? Is there a separate (but equal) community to ask questions about those? I did diligently search for those terms here, and came up with a nearly NULL set. So I believe Raku may have its own forum?

    Blessed Be!

Extra backlash getting added to string
2 direct replies — Read more / Contribute
by newperldeveloper
on Jul 15, 2021 at 12:57
    I am trying to replace the underscore with backslash underscore. However it is adding an extra backslash to the string.
    my $file="Bob_Smith"; $file =~ s/_/\\_/g;
    Bob_Smith becomes Bob\\_Smith.
Getting the shell's version of working directory, without PWD's help
5 direct replies — Read more / Contribute
by perlancar
on Jul 15, 2021 at 05:37

    I'm wondering if it's possible to get/recreate the version of working directory that is the same as the bash version given via PWD environment variable, without using the PWD (I'm thinking no). With Cwd's getcwd() and friends, symbolic links are resolved. I want absolute path of working directory but with symbolic links not resolved like the version bash gives me in PWD, which might have some symlinks in its elements.

    Illustration:

    % pwd /home/foo % mkdir dir1 % ln -s dir1 sym1 % cd sym1 % echo $PWD /home/foo/sym1 % perl -MCwd=getcwd -E'say getcwd' /home/foo/dir1

    UPDATE: Correct the title and wording of what I am asking.

Using form return value as subroutine name
3 direct replies — Read more / Contribute
by phirun
on Jul 14, 2021 at 23:20

    I'd like to use the return value from an HTML form as the name of a destination subroutine.

    What is the most elegant solution for this?

Mojo Template, passing dynamic index to array.
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 13, 2021 at 14:45
    Hi Monks!!

    I am trying to pass a dynamic value to print a value out of an array using Mojo::Template, getting syntax errors, is there a way to do that?

    The code works if its like this:
    <% $local->{ place }[ 0 ]{ street } %>

    I'd like to pass the index number dynamic using the example below:

    Code snipped from the .ep template file:
    <p> Location: </br> <% $local->{ place }[ %> <%= $number %> <% ]{ street } %> </p>

    <%= $number %>, can be 0,1,2,3...


Get element of array not looping.
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 13, 2021 at 13:53
    Hi Monks,

    Whats the way to manually access the firt element of this arrays without looping?

    Test Code:

    #!/usr/bin/env perl use strict; use warnings; my $DATA = { "facts": [ { "name": "A", "type": "Normal" }, { "name": "B", "type": "Broken" } ], }; print $DATA->{ facts }->{ name }->[0]; # A print $DATA->{ facts }->{ type }->[0]; # Normal exit;
    Thanks for looking!
finding the varaible quantity and static quantity from comma separated data and values
1 direct reply — Read more / Contribute
by ankityad
on Jul 13, 2021 at 04:17
    Hi, I am solving a problem where i have dumped the data into a text file with comma separated registers(flip-flops) and their values. I want to check which registers can have multiple values and which are having constant values throughout. The sample values can be shown below: register1, 000111 register2, 010101 register1, 000111 register2, 011011 register4, 100000 register4, 100000 register5, 000000 register1, 000111 Now, I want to get the separate list of registers which are variable and there values and registers which have only constant values. Please help , how can I proceed with this problem?

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":