CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
/g not working?

 



alberto_zurita
New User

Sep 8, 2014, 4:08 PM

Post #1 of 5 (2745 views)
/g not working? Can't Post

I am using the /g modifier to globally match patterns in the following string:

$data_h = "[electronic resource] [electronic resource] [electronic resource] [electronic resource]"



The objective is to delete the extra [electronic resource] patterns and just keep one [electronic resource].

I wrote the following string replacement:

$data_h =~ s/(\[electronic resource\]\s*)+//g;


The current procedure is only deleting the first pattern. So I get the output:

[electronic resource] [electronic resource] [electronic resource]

Only one [electronic resource] got replaced.

What am I doing wrong? Please help.


(This post was edited by alberto_zurita on Sep 8, 2014, 4:09 PM)


BillKSmith
Veteran

Sep 8, 2014, 9:57 PM

Post #2 of 5 (2731 views)
Re: [alberto_zurita] /g not working? [In reply to] Can't Post

I am not able to duplicate the problem that you report. You should post a complete script that demonstrates it.

By changing your "\s*" to "\s+", I get the desired substitution (Remove all but the last [electronic resource]). Note: I also removed the unnecessary brackets, but that does not change the results.


Code
use strict; 
use warnings;
my $data_h = '[electronic resource] '
. '[electronic resource] '
. '[electronic resource] '
. '[electronic resource]'
;
$data_h =~ s/\[electronic resource\]\s//g;
# Note: Angle brackets are not part of the string.
# They are printed to verify the absence of whitespace at ends of string.
print "data_h contains: >>$data_h<<\n";


OUTPUT:

Code
data_h contains: >>[electronic resource]<<

Good Luck,
Bill


alberto_zurita
New User

Sep 9, 2014, 7:43 AM

Post #3 of 5 (2708 views)
Re: [BillKSmith] /g not working? [In reply to] Can't Post

Thanks for your help Bill.

The string is the result of extracting data from MARC records. I don't know if you are familiar with it. It basically grabs the data off the MARC record into a single string, and process it with PERL.

Printing the result string, it gives: [electronic resource] [electronic resource] [electronic resource] [electronic resource]


Code
 
if($record->field('245')->subfield('h') =~ /[electronic resource]/){
print( "this record contains [electronic resource]: ", $record->field('245')->subfield('h'),"\n");
my $data_h = $record->field('245')->subfield('h');

#$data_h =~ s/\[electronic resource\]\s*//g;
$data_h =~ s/\[electronic resource\]\s+//g;
$record->field('245')->update(h => $data_h);
print( "after update record contains: ", $record->field('245')->subfield('h'),"\n");
}



Output:
perl memoirs.pl memo_recs.mrc
this record contains [electronic resource]: [electronic resource] [electronic resource] [electronic resource] [electronic resource]
after update record contains: [electronic resource] [electronic resource] [electronic resource]


The actual MARC record contains the following:

10$aOn central critical values of the degree four L-functions for GSp(4) :$h[electronic resource] $h[electronic resource] $h[electronic resource] $h[electronic resource] $bthe fundamental lemma /$cMasaaki Furusawa, Joseph A. Shalika.



As you can see, it grabs all the h's data, and placed them into 1 single string "[electronic resource] [electronic resource] [electronic resource] [electronic resource]"

I hope it makes sense.

Thanks.


FishMonger
Veteran / Moderator

Sep 9, 2014, 8:04 AM

Post #4 of 5 (2700 views)
Re: [alberto_zurita] /g not working? [In reply to] Can't Post

test.pl

Code
#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my $data_h = '[electronic resource] [electronic resource] [electronic resource]';
$data_h =~ s/\[electronic resource\]\s+//g;

print Dumper $data_h;


c:\test>test.pl

Quote
$VAR1 = '[electronic resource]';


As you can see, the regex is working as expected.

I'd say that this line isn't doing what you expect.

Code
$record->field('245')->update(h => $data_h);


What output do you get if you change this line:

Code
print( "after update record contains: ", $record->field('245')->subfield('h'),"\n");

to this:

Code
print "after update record contains: ", $data_h, "\n";



alberto_zurita
New User

Sep 9, 2014, 9:03 AM

Post #5 of 5 (2694 views)
Re: [FishMonger] /g not working? [In reply to] Can't Post

Thanks guys:

It looks like it might be a different problem, because even before replacing the string, the value of $data_h is just one [e r]pattern.
I followed your advise FishMonger, and printed this value by itself:


Code
if($record->field('245')->subfield('h') =~ /\[electronic resource\]/g){ 
print( "this record contains [electronic resource]: ", $record->field('245')->subfield('h'),"\n");
my $data_h = $record->field('245')->subfield('h');

#$data_h =~ s/\[electronic resource\]\s*//g;
#$data_h =~ s/\[electronic resource\]//g;
print( "data_h is: ", $data_h,"\n");

}


Output:

perl memoirs.pl memo_recs.mrc
this record contains [electronic resource]: [electronic resource] [electronic resource] [electronic resource] [electronic resource]
data_h is: [electronic resource].

It looks this variable is only being assigned the first or last $h value.

This might mean $record->field('245')->update(h => $data_h) is only updating the first occurrence of [electronic resource] with a blank.

I got a little confused! Smile ?

It looks like it is more of processing the MARC record, than a PERL issue.


(This post was edited by alberto_zurita on Sep 9, 2014, 9:04 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives