I often have requests to comment out BLOCKS of text, such as "comment out everything between and including each cat/^cat block. Example:
mouse
cat
1
2
3
^cat
deer
eel
cat
furbaby
elk
^cat
dog
fish
the result I want is
mouse
#cat
#1
#2
#3
#^cat
deer
eel
#cat
#furbaby
#elk
#^cat
dog
fish
This can be in an array (one element per line) or a scalar. Usually I just loop thru the list and set a flag when hit cat, and unset it after leaving ^cat, and add the # if flagged. Which is like how I would do this in C. Every time I solve a problem in my brain in C, I always think to myself surely Perl has a better approach!.. Fugly code:
my $comment=0;
for (@a) {
$comment++ if /^cat\b/;
$_ = '#'.$_ if $comment;
$comment-- if /^\#\^cat\b/;
}
Can you suggest a brief solution with a single no-looping regex, OR a map for an array? Or maybe a clever mapped split?
Much thanks and happy Day of the Earth-Rodent-Forecaster!
|
I've noticed that some modules, which do exist, are sometimes shown as "not found" on metacpan.org. If you reload the page it will kinda alternate between found and not found. Here is one such module:
Astro::Montenbruck
Some of the modules in that distribution are always found, like Astro::Montenbruck::Ephemeris
Others kinda alternate between found and not found, like
Astro::Montenbruck::CoCo
What's going on at metacpan?
|
This is a simple script which sorts TV show episodes into the right folders, it comes down to this basic example:
### I run this from the command line:
/usr/bin/perl mover.pl The.Rookie.S05E14.720p.HEVC.x265-MeGusta[eztv.r
+e].mkv
The script dies here:
unless ( -e $ARGV[0] ) {
die("$ARGV[0] doesn't exist");
}
But:
- The file does exist: it shows up if I do ls The.Rookie.S05E14.720p.HEVC.x265-MeGusta[eztv.re].mkv
- The script is operating in the right folder, I double checked
If I skip the file-exists test and go right to moving the file to the correct folder, I can do this:
say "move command: 'mv $ARGV[0] ./r/'";
system("mv $ARGV[0] ./r/");
The mv command in the system() call fails with "No such file or directory", but the exact same command, if I copy and paste it from the perl script's output and hit enter, works just fine.
Is it the brackets in the filename? I tried escaping them and it make no difference. The only wrinkle I can think of is that this is happening on MacOS, but the files in question are on a share from a Linux NAS, could that be causing issues?
|
Hey, Monks, I've never done this before, besides referencing from a database in sql to perl. I have a file that will have missing data that I am parsing, I want to populate the State based off the zip codes, so I created spreadsheet with like 80000 zip codes and they state abbr. Could you please give me some guidance on using one file to reference zip codes and based on those to update the state on another external file.
|
Dear Monks, I'm passing an octal file mode to a sub like sub(0666) but
in the sub I'm seeing it as 438, and 0777 becomes 511. Quoting the mode
causes it to lose the leading zero so that sub(q(0666)) becomes 666. I
can put the leading 0 back and continue but that feels messy. Why is
0666 changing to 438? Thank you!
|
I keep running into an uninitialized error that doesnt make sense to me, I should be writing the state in column 6, but I keep getting Use of uninitialized value $t6 in string eq at Project1_6.pl line 48, <FH> line 2551.
Use of uninitialized value $t6 in string eq at Project1_6.pl line 42, <FH> line 2552.
Used perl c- it runs, says syntax is okay
I tried declaring $t in line 35, "$worksheet->write($rowCount1+1, $_, my $t$_);"
Perl C- said that was okay, but I get "syntax error at Project1_6.pl line 37, near "$t[""
use Excel::Writer::XLSX;
use strict;
use warnings;
my $rowCount = 0;
my $filename = "Output2022.xlsx";
my $workbook = Excel::Writer::XLSX->new( $filename );
open(FH, "<", "SRC185.xlsx" ) or die;
my $worksheet = $workbook->add_worksheet('List');
$worksheet->write(0, 0, "source_id" );
$worksheet->write(0, 1, "first_name" );
$worksheet->write(0, 2, "middle" );
$worksheet->write(0, 3, "last_name" );
$worksheet->write(0, 4, "address1" );
$worksheet->write(0, 5, "city");
$worksheet->write(0, 6, "state");
$worksheet->write(0, 7, "postal_code");
$worksheet->write(0, 8, "phone_number");
$worksheet->write(0, 9, "address3");
$worksheet->write(0, 10,"province");
$worksheet->write(0, 11, "email");
my $rowCount1 = 0;
my $t=0;
my @z = 90005;
while (<FH>){
chomp;
my @t=(',',$_);
if(defined($t[8])){
my $Count=0;
$worksheet->write($rowCount1+1, $_, $t[$_]);
$Count++;
} elsif($t[6] eq "CA" && $t[7] eq !defined) {
$worksheet->write($rowCount1+1, 7, $z[7]);
}
$rowCount1++;
}
$workbook->close();
close(FH);
|
Can a lady or gentleman please help me find the correct resource. I am looking for information that I can use in an app I'm building.
It will be run on a LAN behind a firewall. It would be far more convenient if the user could be logged in automatically if they are already logged in to the network it is running on. So, I'm seeking information on accessing a network users's userid and login status. I'm not looking for specific code, just guidance to the proper keywords or terms that I need to research to achieve this end goal, if it is even possible.
I don't mind doing the legwork, I just don't know what the specific legwork is that I must do right now.
Peace,
|
perl -MExtUtils::Installed -le '$_=ExtUtils::Installed->new->modules;p
+rint'
1650
perl -le '@_=grep/Module/,split/\n/,`perldoc -uT perllocal`;print scal
+ar@_'
1472
perl -MExtUtils::Installed -le '@_=ExtUtils::Installed->new->modules;p
+rint for sort{lc$a cmp lc$b}@_' >mod.ext
perl -e '@_=grep/Module/,`perldoc -uT perllocal`;for(@_){s/.*?\|([^>]+
+)>/$1/}print for sort{lc$a cmp lc$b} @_' >mod.loc
diff -y mod.ext mod.loc
diff -y mod.ext mod.loc|grep '<'|wc -l
232
diff -y mod.ext mod.loc|grep '>'|wc -l
54
|
I've created a helper function for my own purposes and thought it would be useful to others. So CPAN seems a sensible place to put it so others can use it if they want to...
It's function is simple - to go to the homepage of a website and return an array of URI's within that site, being careful not to stray outside it, that use the http or https scheme. It ignores things that aren't plain text or that it cannot parse such as PDFs or CSS files but includes Javascript files as links (thing like window.open or document.location.href) might be lurking there. It deliberately doesn't try to follow the action attribute of a form as that is probably meaningless without the form data.
As the Monastery has taught be that all published modules should have tests, I want to do it probably and provide those tests...
But, given that there is only one function and it makes HTTP requests, what should I test?
The obvious (to me) test is that it returns the right number of URIs from a website. But that number will likely change over time, so I cannot hardcode the 'right' answer into the tests. So beyond the necessary dependencies and their versions, I'd like some ideas of what should be in the tests, please.
In case you're interested, this came about from wanting to automate producing and installing sitemap files.
|
I want to study more on SIGNAL. But after half hour digging, there's nothing useful.
I am looking some full documentation that explains
$SIG{__DIE__} will fire when die() happen
$SIG{INT} will fire when Ctrl-C pressed
...
Is that anything like this exists?
|