Aldebaran has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks
I'm fumbling with another regex and have been weighing the time it costs to write a proper perlmonks post as opposed to digging myself in further with errant attempts and wish I had given up earlier. I'm combining two lists:
$ cat name1.txt 0. Amber BYU 1. Kim BGSU 2. Kim Washington $ cat harm1.txt 0. J 1. B F K 2. A I J $
In the first list, I'd like the second word of the name to be rendered as only a first initial. I'm completely mystified by the omission of the second letter. In the second list, I want the numbers omitted, and the regex fails on the first item of the list, whether it is 0 or 54. I hope that I have enough to illustrate my intent. What follows are caller, callee, and output.
#!/usr/bin/perl -w use strict; use 5.010; use lib "template_stuff"; use steps1; say "enter basename for file"; my $word = <>; chomp $word; # main data structure my %vars = ( name => 'name1.txt', harm => 'harm1.txt', diff => 'diff1.txt', word => $word .'.rtf', ); my $rvars = \%vars; my $return = pop_texts( $rvars ); say "returned was \n $$return"; my $return2 = format_texts( $rvars, $return ); __END__
sub pop_texts { use strict; use 5.010; use File::Slurp; my ($rvars) = shift; my %vars = %$rvars; my @name = read_file( $vars{name} ); my @harm = read_file( $vars{harm} ); for (@name) { s/\s+$/ /; my $int = s/^(\d+\.)(\s+)(\w+)(\s+)(\w)(.)/$3$4$5/; say "int is $int"; say "six was $6"; } for (@harm) { my $int = s/(^\d+\.)(\s+)(\w)(.)/was harmed by $3$4/; say "schmint is $int"; } my $text1 = ''; for my $i ( 0 .. $#name ) { $text1 = $text1 . $name[$i] . $harm[$i] . "\n"; } my $reftext = \$text1; return $reftext; }
$ perl hears1.pl enter basename for file rt7 int is 1 six was Y int is 1 six was G int is 1 six was a schmint is schmint is 1 schmint is 1 returned was Amber BU 0. J Kim BSU was harmed by B F K Kim Wshington was harmed by A I J
Thx for your comment.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: combining lists along with a regex
by Athanasius (Archbishop) on May 09, 2015 at 03:12 UTC | |
by Aldebaran (Curate) on May 09, 2015 at 06:05 UTC | |
by Athanasius (Archbishop) on May 09, 2015 at 08:19 UTC | |
by Aldebaran (Curate) on May 10, 2015 at 01:37 UTC | |
by Athanasius (Archbishop) on May 10, 2015 at 02:54 UTC | |
|
Re: combining lists along with a regex
by CountZero (Bishop) on May 09, 2015 at 10:25 UTC |