Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: trouble with substitution

by ww (Bishop)
on Jan 19, 2012 at 03:27 UTC ( #948662=note: print w/ replies, xml ) Need Help??


in reply to trouble with substitution

"the programme compalins" (sic)
. Yes, it certainly does. Did you try to understand the complaints?
C:\>perl -c F:\_Perl_\pl_test\verbatim.pl Global symbol "$VAR1" requires explicit package name at verbatim.pl li +ne 24. syntax error at verbatim.pl line 25, near ") }" verbatim.pl had compilation errors.

Did you read even the very first section of the posting guidelines (How do I post a question effectively?, inter alia) which tell you'll we'll be better able to help if you tell us (inside code tags) the wording (verbatim!) of what the program says when it "compalins?"

In any case, the first message says you failed to declare $VAR1 before using it.

The second points to the fact that you've written an if clause (a "conditional") without a block of code telling the program what to do when the if returns 'true' (and preferably, also, when it returns 'false'). See "Conditionals" in perldoc perlintro (at your CLI) or perlintro (in your browser).

What you seem to be looking for is something like this:

#!/usr/bin/perl; use strict; use warnings; use 5.012; my ($line, @newarr); my @hashfile = <DATA>; for my $line (@hashfile){ $line =~ tr/\$//; if ( $line =~ /VAR1/ ) { $line =~ s/VAR1/my \%hash/; push @newarr, $line; } else { push @newarr, $line; } } for $_( @newarr) { say $_; } __DATA__ $VAR1 = { '' => '', '362520' => 'Fktn and Name: fukutin[Rattus norvegicus]', '375790' => 'AGRN and Name: agrin[Homo sapiens]', '3339' => 'HSPG2 and Name: heparan sulfate proteoglycan 2[Ho +mo sapiens]', '1428' => 'CRYM and Name: crystallin, mu[Homo sapiens]', }

which produces this output:

$my %hash = { '' => '', '362520' => 'Fktn and Name: fukutin[Rattus norvegicus]', '375790' => 'AGRN and Name: agrin[Homo sapiens]', '3339' => 'HSPG2 and Name: heparan sulfate proteoglycan 2[Ho +mo sapiens]', '1428' => 'CRYM and Name: crystallin, mu[Homo sapiens]', }

Adding the necessary code to write a new file from @newarr (or, to write the new file without resort to that extra varaible/data structure) is left as an exercise.

HOWEVER, the job sequence of which this is a part would probably be less subject to error and easier to maintain, were you to process whatever the source data that's fed into your dumpparse.txt into an appropriate data structure (which may or may not be a hash; more likely, not, from the looks of it, but you haven't told us enough to put much credence in that interpretation) and perform the remaining processing in a single script.

Updated: minor edits for brevity/clarity.


Comment on Re: trouble with substitution
Select or Download Code
Re^2: trouble with substitution
by Interzona (Initiate) on Jan 20, 2012 at 00:31 UTC

    Thanks for reply. I will look through this. Currently it is all a very steep learning curve, so will take while to understand what you have suggested.

    Thanks again

    bye

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://948662]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (15)
As of 2014-09-16 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (26 votes), past polls