if else elsif

--_000_D225040F2D0F75448937B83D9527991A0154882EBEdenex1crick et_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

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 great=
ly!

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


#!/usr/bin/perl

use warnings;
use strict;

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

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

my [at] array;

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

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

push [at] array, join "\t", [at] data;
}

[at] array =3D sort {
my [at] aa =3D split /\t/, $a;
my [at] bb =3D split /\t/, $b;
$aa[0] <=3D> $bb[0] or
$aa[1] <=3D> $bb[1] or
$aa[2] <=3D> $bb[2];
} [at] array;

print $out "$_\n" foreach [at] array;
close $out;




#RTD 1 unit =3D 4 chips RUM Field 16 0 - 2^16 - 1 milliseconds








Chris Stinemetz


--_000_D225040F2D0F75448937B83D9527991A0154882EBEdenex1crick et_--
Chris Stinemetz [ Do, 17 März 2011 21:16 ] [ ID #2056744 ]

Re: if else elsif

>>>>> "CS" == Chris Stinemetz <cstinemetz [at] cricketcommunications.com> 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 ------ uri [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Do, 17 März 2011 22:28 ] [ ID #2056749 ]

Re: if else elsif

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 [at] 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 [at] array;
>
> while (<$fh>) {
> next unless /;/;
> chomp;
> my [at] data = ( split /;/ )[31,32,38,39,261];
>
> if ( [at] data[39] =~ /15/)
> {
> 2
> }
> else
> {
> 1
> }
>
> push [at] array, join "\t", [at] 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 [at] data. These are now
accessible as $data[0] through $data[4].

So I presume that by [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Do, 17 März 2011 22:42 ] [ ID #2056752 ]

RE: if else elsif

>-----Original Message-----
>From: Chris Stinemetz [mailto:cstinemetz [at] 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 [at] data in pattern match (m//) at
>./DOband.pl line 19, <$fh> line 485.
>
>
>#!/usr/bin/perl
>
>use warnings;
>use strict;
>
>my $filepath =3D 'C:/temp/PCMD';
>my $outfile =3D 'output.txt';
>
>open my $fh, '<', $filepath or die "ERROR opening $filepath: $!";
>open my $out, '>', $outfile or die "ERROR opening $outfile: $!";
>
>my [at] array;
>
>while (<$fh>) {
> next unless /;/;
> chomp;
> my [at] data =3D ( split /;/ )[31,32,38,39,261];
>
> if ( [at] data[39] =3D~ /15/)
> {
> 2
> }
> else
> {
> 1
> }
You are only pulling 5 elements into [at] data, yet as part of your test, =
you are looking at the 39th element, but you really element 3.

=A0=A0=A0=A0=A0=A0=A0=A0 If you have any questions and/or problems, =
please let me know.
=A0=A0=A0=A0=A0=A0=A0=A0=A0Thanks.
=A0
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 [at] array, join "\t", [at] data;
>}
>
> [at] array =3D sort {
> my [at] aa =3D split /\t/, $a;
> my [at] bb =3D split /\t/, $b;
> $aa[0] <=3D> $bb[0] or
> $aa[1] <=3D> $bb[1] or
> $aa[2] <=3D> $bb[2];
>} [at] array;
>
>print $out "$_\n" foreach [at] array;
>close $out;
>
>
>
>
>#RTD 1 unit =3D 4 chips RUM Field 16 0 - 2^16 - 1 milliseconds
>
>
>
>
>
>
>
>
>Chris Stinemetz


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
David.Wagner [ Do, 17 März 2011 21:24 ] [ ID #2056808 ]

Re: if else elsif

--bcaec51dd8a7e6b7ea049ec1bc62
Content-Type: text/plain; charset=ISO-8859-1

It would help if you could clarify what you expect the following chunk of
code to achieve:

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

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

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

--bcaec51dd8a7e6b7ea049ec1bc62--
Katie T [ Fr, 18 März 2011 14:27 ] [ ID #2056815 ]
Perl » gmane.comp.lang.perl.beginners » if else elsif

Vorheriges Thema: Using perl to automate add user on linux/freebsd systems
Nächstes Thema: Recall: sub routine