Re: if else elsif

2011-03-18 Thread Katie T
It would help if you could clarify what you expect the following chunk of
code to achieve:

   my @data = ( split /;/ )[31,32,38,39,261];

   if (@data[39] =~ /15/)
   {
   2
   }
   else
   {
   1
   }

-- 
CoderStack
http://www.coderstack.co.uk/php-developer-jobs
The Software Developer Job Board


RE: if else elsif

2011-03-17 Thread Wagner, David --- Senior Programmer Analyst --- CFS
>-Original Message-
>From: Chris Stinemetz [mailto:cstinem...@cricketcommunications.com]
>Sent: Thursday, March 17, 2011 14:16
>To: beginners
>Subject: if else elsif
>
>I am trying to return new values based on if else. I understand the idea of
>using if else, but I am not sure I have placed in the right place.
>I was assuming I would want to insert it before the array is sorted.
>Thank you in advance. This mailing list is helping me understand perl
>greatly!
>
>I am getting the following error:
>Use of uninitialized value within @data in pattern match (m//) at
>./DOband.pl line 19, <$fh> line 485.
>
>
>#!/usr/bin/perl
>
>use warnings;
>use strict;
>
>my $filepath = 'C:/temp/PCMD';
>my $outfile  = 'output.txt';
>
>open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";
>open my $out, '>', $outfile or die "ERROR opening $outfile: $!";
>
>my @array;
>
>while (<$fh>) {
>next unless /;/;
>chomp;
>my @data = ( split /;/ )[31,32,38,39,261];
>
>if (@data[39] =~ /15/)
>{
>2
>}
>else
>{
>1
>}
  You are only pulling 5 elements into @data, yet as part of your test, you are 
looking at the 39th element, but you really element 3.

 If you have any questions and/or problems, please let me know. 
 Thanks. 
 
Wags ;) 
David R. Wagner 
Senior Programmer Analyst 
FedEx Services 
1.719.484.2097 Tel 
1.719.484.2419 Fax 
1.408.623.5963 Cell
http://Fedex.com/us

>
>push @array, join "\t", @data;
>}
>
>@array = sort {
>  my @aa = split /\t/, $a;
>  my @bb = split /\t/, $b;
>  $aa[0] <=> $bb[0] or
>  $aa[1] <=> $bb[1] or
>  $aa[2] <=> $bb[2];
>} @array;
>
>print $out "$_\n" foreach @array;
>close $out;
>
>
>
>
>#RTD 1 unit = 4 chips RUM Field 16 0 - 2^16 - 1 milliseconds
>
>
>
>
>
>
>
>
>Chris Stinemetz


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




Re: if else elsif

2011-03-17 Thread Rob Dixon

On 17/03/2011 20:16, Chris Stinemetz wrote:


I am trying to return new values based on if else. I understand the
idea of using if else, but I am not sure I have placed in the right
place. I was assuming I would want to insert it before the array is
sorted. Thank you in advance. This mailing list is helping me
understand perl greatly!

I am getting the following error:
Use of uninitialized value within @data in pattern match (m//) at
./DOband.pl line 19,<$fh> line 485. >

#!/usr/bin/perl

use warnings;
use strict;

my $filepath = 'C:/temp/PCMD';
my $outfile  = 'output.txt';

open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";
open my $out, '>', $outfile or die "ERROR opening $outfile: $!";

my @array;

while (<$fh>) {
 next unless /;/;
 chomp;
 my @data = ( split /;/ )[31,32,38,39,261];

 if (@data[39] =~ /15/)
 {
 2
 }
 else
 {
 1
 }

 push @array, join "\t", @data;
}


split /;/ is splits $_ into a list of many (hopefully at least 262)
fields. This is indexed with [31,32,38,39,261], which selects a 'slice'
of five elements and assigns them to the array @data. These are now
accessible as $data[0] through $data[4].

So I presume that by @data[39] you mean $data[3]?

It is unclear what you want the if statement to do; perhaps change the
value of this element if it happens to be 15? If you are comparing
numbers then it is wrong to use regular expressions (/15/ will match
anything containing those two characters, like 'XXX15XXX'). A simple ==
comparison will suffice.

So to change the fourth selected field to 1, or to 2 if it happens to be
15, you would write

  if ($data[3] == 15) {
$data[3] = 2;
  }
  else {
$data[3] = 1;
  }

I hope that is a satisfactory answer.

Rob

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




Re: if else elsif

2011-03-17 Thread Uri Guttman
> "CS" == Chris Stinemetz  writes:

  CS> I am trying to return new values based on if else. I understand
  CS> the idea of using if else, but I am not sure I have placed in the
  CS> right place.  I was assuming I would want to insert it before the
  CS> array is sorted.  Thank you in advance. This mailing list is
  CS> helping me understand perl greatly!

if/else DOES NOT return a value. where did you even get that idea? if
you want to choose between two values, use the ?: ternary operator. read
about it in perldoc perlop. if you need help with it, ask again here.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

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