Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print to screen but blank to file!?!

Status
Not open for further replies.

jggrossm

IS-IT--Management
Joined
Feb 8, 2007
Messages
5
Location
US
Hi all. I'm new at perl. Just using it to strip some fields of a comma-delimited file. This code works when printing to the screen, but not to a file. Any help would be much appreciated.

#!/usr/bin/perl -w

#STRIPS last 5 fields off a comma-delimited file

#OPENS FILE
open (DI, ">DIAMOND20070126.txt") or die "I couldn't get at it";

# GRABS LINE
for $line(<DI>) {

# TOKENIZES LINE
@tokenized = split(/,/,$line);

# POPS END FIELDS OFF
for $i (17 .. 21){
pop(@tokenized);
}

#PUSHES REMAINING FIELDS INTO NEW ARRAY
@joined = join(',',@tokenized);
push(@trimmed,@joined);

}
#WRITES NEW ARRAY TO FILE
for $i (1 .. 20) {
print DI "@trimmed[$i]\n";
}
close DI;
 
the main problem is you opened the file in write/create mode '>'.

This should do what you want:

Code:
[gray]#!/usr/bin/perl  -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@trimeed[/blue] = [red]([/red][red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] [red]([/red]IN, [red]"[/red][purple]<DIAMOND20070126.txt[/purple][red]"[/red][red])[/red] or  [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]I couldn't get at it: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<IN>[red])[/red] [red]{[/red]
   [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@trimeed[/blue], [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url] [red]'[/red][purple],[/purple][red]'[/red], [red]([/red][url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple],[/purple][red]/[/red],[blue]$_[/blue][red])[/red][red])[/red][red][[/red][fuchsia]0..16[/fuchsia][red]][/red][red];[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] IN[red];[/red]
[black][b]open[/b][/black] [red]([/red]OUT, [red]"[/red][purple]>DIAMOND20070126.txt[/purple][red]"[/red][red])[/red] or  [black][b]die[/b][/black] [red]"[/red][purple]I couldn't get at it: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] OUT [red]"[/red][purple][blue]$_[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red] [olive][b]for[/b][/olive] [blue]@trimeed[/blue][red];[/red]
[black][b]close[/b][/black] OUT[red];[/red]

Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Wow - your code worked. Thanks! But I'd still like to understand why mine didn't. I tried switching '>' to '<' and got:
Filehandle DI opened only for input at stripsDI_extraFieds.pl line 31, <DI> line 35.
 
you can only read '<' or write '>' to a filehandle using those operators, but not both at the same time. There is '+<' for read/write access but it's buggy and seems to be platform dependent so I never use it. You could use perls in-place editor for this task. Untested code:

Code:
[gray]#!/usr/bin/perl  -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[red]{[/red]
   [url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]@ARGV[/blue] = [red]([/red][red]'[/red][purple]DIAMOND20070126.txt[/purple][red]'[/red][red])[/red][red];[/red]
   [black][b]local[/b][/black] [blue]$^I[/blue] = [red]'[/red][purple].bac[/purple][red]'[/red][red];[/red]
   [olive][b]while[/b][/olive] [red]([/red]<>[red])[/red] [red]{[/red]
      [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
      [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url] [red]'[/red][purple],[/purple][red]'[/red], [red]([/red][url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple],[/purple][red]/[/red],[blue]$_[/blue][red])[/red][red])[/red][red][[/red][fuchsia]0..16[/fuchsia][red]][/red],[red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
   [red]}[/red]
[red]}[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Whoa - your signature clearly rings true.

Okay, take pity on me and explain one more part to me. Where's the pop?

 
don't need to use pop(). I used an array slice that only returns the parts of the array you want to begin with:

Code:
(split(/,/,$_))[b][0..16][/b]

that does what you did here:

Code:
    @tokenized = split(/,/,$line);

    # POPS END FIELDS OFF
    for $i (17 .. 21){
        pop(@tokenized);
    }

but without using a temporary variable - @tokenized - to hold the data. Instead it's sent directly to the join() function to be reassembled "on the fly".


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top