Re: Neater way to declare variables

2009-01-08 Thread Ron Bergin
On Jan 8, 3:56 am, andrew.tayl...@hmrcaspire.com (Andrew Taylor)
wrote:
> Hello
>
> I have a script that is (at one point) reading through a file.  The file
> is processed line by line and each line split into an array like so:
>
> while (<$TESTFILE>)
>
> {
>
>   my $cur_line=$_;
>
>   chomp ($cur_line);
>
while (my $cur_line = <$TESTFILE>) {
 chomp $cur_line;

>   my @split_line = split( /$t_delim/, $cur_line );
>
> # In a number of places, I have code that looks like the following.
>
>   my $default_type;
>
>   if( $split_line[0] eq "DEFAULT_INPUT" )
>
>   {
>
>     $default_type = "INPUT";
>
>   }
>
>   if ( $split_line[0] eq "DEFAULT_OUTPUT" )
>
>   {
>
>     $default_type = "OUTPUT";
>
>   }
>
snip

>
> This works OK, but I'm trying to avoid declaring variables seperately
> from assigning them (wherever possible), and trying to keep the size of
> the script down without losing human legibility.  
>
> Is there a neater/cleverer way of doing this?
>

my $default_type = $split_line[0] =~ /INPUT/ ? 'INPUT' : 'OUTPUT';


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Neater way to declare variables

2009-01-08 Thread Jenda Krynicky
From: "Taylor, Andrew \(ASPIRE\)" 
> I have a script that is (at one point) reading through a file.  The file
> is processed line by line and each line split into an array like so: 
> 
> while (<$TESTFILE>)
> {
>   my $cur_line=$_;
>   chomp ($cur_line);

while (defined(my $cur_line = <$TESTFILE>)) {
  chomp($cur_line);

>   my $default_type;
>   if( $split_line[0] eq "DEFAULT_INPUT" )
>   {
> $default_type = "INPUT";
>   } 
>   if ( $split_line[0] eq "DEFAULT_OUTPUT" )
>   {
> $default_type = "OUTPUT";
>   }

my $default_type = do {
  if ($split_line[0] eq "DEFAULT_INPUT" ) {
"INPUT"
  } elsif ($split_line[0] eq "DEFAULT_OUTPUT" ) {
"OUTPUT"
  }
};

Jenda
= je...@krynicky.cz === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Neater way to declare variables

2009-01-08 Thread Telemachus
On Thu Jan 08 2009 @ 11:56, Taylor, Andrew (ASPIRE) wrote:
> # In a number of places, I have code that looks like the following.
> 
>  
> 
>   my $default_type;
> 
>  
> 
>   if( $split_line[0] eq "DEFAULT_INPUT" )
> 
>   {
> 
> $default_type = "INPUT";
> 
>   } 

> This works OK, but I'm trying to avoid declaring variables seperately
> from assigning them (wherever possible), and trying to keep the size of
> the script down without losing human legibility.

You can use the statement modifier conditional form, in which case, the if
follows the assignment. Many people find this easier to read for such a simple
test:

my $default_type = "INPUT" if $split_line[0] eq "DEFAULT_INPUT";

See here for more: http://perldoc.perl.org/perlsyn.html#Statement-Modifiers

Hope this helps, T


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Neater way to declare variables

2009-01-08 Thread Mr. Shawn H. Corey
On Thu, 2009-01-08 at 11:56 +, Taylor, Andrew (ASPIRE) wrote:
> This works OK, but I'm trying to avoid declaring variables seperately
> from assigning them (wherever possible), and trying to keep the size
> of
> the script down without losing human legibility.  
> 
> Is there a neater/cleverer way of doing this?
> 

Programming is as much about organization and communication as it is
about coding.  It is possible to write your code too densely to be
easily understood.

However you can shorten some of your code.

while (<$TESTFILE>)
{
  chomp;
  my @split_line = split( /$t_delim/, $_ );


my %Default_types = (
  DEFAULT_INPUT => 'INPUT',
  DEFAULT_OUTPUT => 'OUTPUT',
);

$default_type = $Default_types{$split_line[0]} || undef;


-- 
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication as it is about 
coding.

"It would appear that we have reached the limits of what it is possible to 
achieve with computer technology, although one should be careful with such 
statements, as they tend to sound pretty silly in 5 years."
  --John von Neumann, circa 1950



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Neater way to declare variables

2009-01-08 Thread Taylor, Andrew (ASPIRE)
Hello

 

I have a script that is (at one point) reading through a file.  The file
is processed line by line and each line split into an array like so: 

 

while (<$TESTFILE>)

{

  my $cur_line=$_;

  chomp ($cur_line);



  my @split_line = split( /$t_delim/, $cur_line );

 

 

# In a number of places, I have code that looks like the following.

 

  my $default_type;

 

  if( $split_line[0] eq "DEFAULT_INPUT" )

  {

$default_type = "INPUT";

  } 

 

  if ( $split_line[0] eq "DEFAULT_OUTPUT" )

  {

$default_type = "OUTPUT";

  }

 

# processing continues...

# more stuff here

# blah blah blah

# etc. etc.

 

 

}

 

This works OK, but I'm trying to avoid declaring variables seperately
from assigning them (wherever possible), and trying to keep the size of
the script down without losing human legibility.  

 

Is there a neater/cleverer way of doing this?

 

Cheers

Andy Taylor

 

 

 


Andrew Taylor | Capgemini | Telford

ASPIRE Data Warehouse Support

T : +44 (0)1952 430684 | www.capgemini.com  

 

 


 


Capgemini is a trading name used by the Capgemini Group of companies which 
includes Capgemini UK plc, a company registered in England and Wales (number 
943935) whose registered office is at No. 1 Forge End, Woking, Surrey, GU21 6DB.
This message contains information that may be privileged or confidential and is 
the property of the Capgemini Group. It is intended only for the person to whom 
it is addressed. If you are not the intended recipient, you are not authorized 
to read, print, retain, copy, disseminate, distribute, or use this message or 
any part thereof. If you receive this message in error, please notify the 
sender immediately and delete all copies of this message.