skyler has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I'm reading a file ans sub "," with "|" , then I'm trying to strip the last string $var7 to take only certain values out of the string and concatenate the string back and pint it to a file. It is giving some errors like " Global symbol "$n" requires special package" Here is my code. I'll appreciate any help.

#! perl -w use strict; #use File::Find; #use File::Copy cp; open(IN, '<c:/doclist.chr') or die "Couldn't open file, $!"; open(OUT, '>c:/doclist.txt') or die "Couldn't open file, $!"; ## print OUT join '|', split /,/ while <IN>; while(<IN>) { chomp; # Remove the newline my ($var1, $var2, $var3, $var4, $var5, $var6, $var7) = split /,/; my $string1 = $var7; substr($string1, 24) = "H:\"; my $string2 = substr($var7,25,2); my $string3 = substr($var7,-13); my $finstring = $string1.$string2.$string3; ## Find file in sub-directory # find(\&wanted, '', '/bar'); # sub wanted { ... } # find \&wanted, "./"; #sub wanted { # print "$File::Find::name ", (-s $File::Find::name); # print "\n"; #} ## Copy file to another directory # copy("file1","file2"); # copy("Copy.pm",\*STDOUT);' # move("/dev1/fileA","/dev2/fileB"); # $n = FileHandle->new("/a/file","r"); # cp($n,"x");' # print OUT $var1,"|",$var2,"|",$var3,"|",$var4,"|",$var5,"|",$var6, +"|",$finstring,"\n"; } exit; #sub get_line { # prompts for, reads, chomps, and returns a line of input #print $_[0]; #chomp(my $line = <STDIN>); #$line; #} # my $source = &get_line("Which source file? "); # open IN, # $source or die "Can't open '$source' for input: $!"; # my $dest = &get_line("what destination file? "); # die "Won't Overwrite Existing File" # if -e $dest; #optional safety test # open OUT, ">$dest" or die "Can't open '$dest' for output: $!"; # my $pattern = &get_line("What search pattern: "); # my $replace = &get_line("What replacement string: "); #while (<IN>) { # s/$pattern/$replace/g; # print OUT $_; #} ### ##foreach my $file (glob "*.old") { # my $newfile = $file; # $newfile =~ s/\.old$/ .new/; # if (-e $newfile) { # warn "can't rename $file to $newfile: $newfile exists\n"; # } elsif (rename $file, $newfile) { # ## success, do nothing # } else { # warn "rename $file to $newfile failed: $!\n"; # } #} ###

20030217 Edit by Corion: Added formatting

Replies are listed 'Best First'.
Re: string stripping
by Cody Pendant (Prior) on Feb 16, 2003 at 22:21 UTC
    I'm going to be Point Out Red Flags Guy:
    ($var1, $var2, $var3, $var4, $var5, $var6, $var7) = split /,/;
    is the "Why Are You Not Using An Array?" red flag.
    @vars = split /,/;
    --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
Re: string stripping
by allolex (Curate) on Feb 16, 2003 at 20:45 UTC

    Hi, I'm a bit of a beginner myself, but I think that use strict requires that you declare $n with my:

    my $n = FileHandle->new("/a/file","r");

    I think that'll do the trick.

    --

    Allolex
    http://allolex.freeshell.org

Re: string stripping
by jasonk (Parson) on Feb 16, 2003 at 19:47 UTC

    The error message you are getting is telling you that you didn't declare $n, but since you didn't preview your posting, and didn't use code tags, your code is unreadable, so you probably won't get a lot of help with it.