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

Re^3: Problem with split using a | separator

by Marshall (Canon)
on Jan 26, 2009 at 03:19 UTC ( #738862=note: print w/replies, xml ) Need Help??


in reply to Re^2: Problem with split using a | separator
in thread Problem with split using a | seperator

if ( $#ARGV < 0 ) { print "Usage: $0 [In File][Out File]\n"; exit(1); } my $emtocin = $ARGV[0]; my $emtocout = $ARGV[1] || 'cmpemtocout.txt';
Since you open to style suggestions, the above could be written:
die "Usage: $0 [In File][Out File]\n" if @ARGV <1; my ($emtocin, $emtocout)= @ARGV; $emtocout ||= 'cmpemtocout.txt';
In Perl you can multiple lvalues! In general using a subscript is not a good idea in Perl. @ARGV is more clear than $#ARGV and you will use the @array syntax LOTS! An "off by one error" is one of the if not, the most common error in programming. A big advantage of Perl is that it greatly reduces this chance. Think in term of number of things in the list, not in terms of last index in list.

Perl has a ||= operator that is often overlooked because it doesn't exist in most languages. Here an undef evaluates to "false", so this sets $emtocout to the default value if not already defined, which evidently is what you want.

Also be aware that "die" prints different things depending upon whether you put a "\n" at the end of the string or not. You get executing path and line number if you leave off the "\n". In your opens, would be able to tell which line had the problem.

You can also apply the list slice operator to the list to begin with instead of waiting until the print (why save something you don't need?).

my @fld = ( split(/\|/, $record ) )[0, 3..8]; #and print could look like this: print FDOUT join("\n",@fld),"\n"; #you don't need $outrecord
Not trying to hyper-critical, just helpful. EDIT:
print FDOUT join("\n",@fld),"\n"; #should have course been, print FDOUT join('|',@fld),"\n";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2022-10-04 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (17 votes). Check out past polls.

    Notices?