Ask a Question related to PERL Miscellaneous, Design and Development.
-
Go Perl #1
array access problem - newbie question
#-------------code----------#
open (MAER_FILE, "MAER_output.txt");
while (<MAER_FILE>) {
chomp;
my @maer_array = split(/\t/);
print "$maer_array[-1]\n";
}
#----------------------------#
#-----Output----#
0.843444019245762
0.837942476416068
0.836919185707587
0.836919185707587
0.836919185707587
0.836534486876684
#------------------#
Where as i just need the output as 0.836534486876684 which is the last
number of the third column. Can anyone please guide me here.
Thanks in advance,
GP
Go Perl Guest
-
Byte array to string and back - newbie question
Hi, I am trying to implement DES algorithm as described in the Microsoft article at... -
newbie array question
Hi All Im new to php and are getting a bit confused about the sybase_fetch_array function (i think that this is the same as mysql_fetch_array?). ... -
newbie question. how to assign an array to a hash table?
Hi, I have something like: $value = 'A' 'C' 'G'; and I would like store this value on a hash table. my @value = split(' ',$value); print... -
Last number in the array --- access problem --Newbie question
Here is the complete problem: I have two files MAER_FILE and EVAL_FILE MAER_FILE contains ITR Max_error Avg_error Min_error... -
Access/ASP newbie problem!
I've set up an Access db with the following =tables & fields; Categories; catID CatName CatPic SUbCategory subCatID catID subCatName -
Purl Gurl #2
Re: array access problem - newbie question
Go Perl wrote:
(snipped)
> while (<MAER_FILE>)No need for chomp.> chomp;
> my @maer_array = split(/\t/);
> print "$maer_array[-1]\n";> 0.836919185707587
> 0.836919185707587
> 0.836534486876684You previously displayed space delimited entries which> Where as i just need the output as 0.836534486876684 which is the last
> number of the third column.
is confirmed by your split match. No need for an array.
Purl Gurl
--
#!perl
while (<DATA>)
{
<DATA>;
if (EOF)
{
$last_line = <DATA>;
print substr ($last_line, rindex ($last_line, " ") + 1);
}
}
__DATA__
one two three
four five six
seven eight nine
PRINTED RESULTS:
________________
nine
Purl Gurl Guest
-
Steven Kuo #3
Re: array access problem - newbie question
On Tue, 26 Aug 2003, Purl Gurl wrote:
> Go Perl wrote:
>
> (snipped)
>>> > Where as i just need the output as 0.836534486876684 which is the last
> > number of the third column.
> You previously displayed space delimited entries which
> is confirmed by your split match. No need for an array.
>
> Purl Gurl
> --
>
> #!perl
>
> while (<DATA>)
> {
> <DATA>;
> if (EOF)
> {
> $last_line = <DATA>;
> print substr ($last_line, rindex ($last_line, " ") + 1);
> }
> }
>
> __DATA__
> one two three
> four five six
> seven eight nine
>
>
> PRINTED RESULTS:
> ________________
>
> nine
>
You're throwing away to many lines. Check the results when __DATA__
contains four lines:
__DATA__
one two three
four five six
foo bar baz
seven eight nine
PRINTED RESULTS:
_______________
baz
To the OP,
You can either
(1) Loop through the file and save the last line:
my $last_line;
$last_line = $_ while (<DATA>);
print "Parsing last line : $last_line\n";
# do stuff with $last_line
Or (2) loop through the file and check for eof:
while (<DATA>) {
if (eof DATA) {
print "Parsing last line : $_\n";
# do stuff with $_
}
}
Or (3) read the file backwards and process the first line.
use File::ReadBackwards
See [url]http://search.cpan.org/author/URI/File-ReadBackwards-1.00/ReadBackwards.pm[/url]
--
Hope this helps,
Steven
Steven Kuo Guest
-
JS Bangs #4
Re: array access problem - newbie question
Go Perl sikyal:
No one can tell you anything until you show us what the data your working> #-------------code----------#
> open (MAER_FILE, "MAER_output.txt");
> while (<MAER_FILE>) {
> chomp;
> my @maer_array = split(/\t/);
> print "$maer_array[-1]\n";
> }
> #----------------------------#
>
> #-----Output----#
>
> 0.843444019245762
> 0.837942476416068
> 0.836919185707587
> 0.836919185707587
> 0.836919185707587
> 0.836534486876684
>
> #------------------#
> Where as i just need the output as 0.836534486876684 which is the last
> number of the third column. Can anyone please guide me here.
> Thanks in advance,
> GP
from is. Your code doesn't have any compilation problems, so there must be
a logic problem, which we can't work out unless we get the data in
MAER_FILE.
--
Jesse S. Bangs [email]jaspax@u.washington.edu[/email]
[url]http://students.washington.edu/jaspax/[/url]
[url]http://students.washington.edu/jaspax/blog[/url]
Jesus asked them, "Who do you say that I am?"
And they answered, "You are the eschatological manifestation of the ground
of our being, the kerygma in which we find the ultimate meaning of our
interpersonal relationship."
And Jesus said, "What?"
JS Bangs Guest
-
Benjamin Goldberg #5
Re: array access problem - newbie question
Purl Gurl wrote:
[snip]While we can read a line and stuff it into $_, and it's not undef...> while (<DATA>)
> {
Read another line and throw it away.> <DATA>;
Check whether the constant string "EOF" is true. (Which it always> if (EOF)
is.)
Now read another line, and store it into $last_line.> {
> $last_line = <DATA>;
This, at least, does the right thing (if $last_line happens to be the> print substr ($last_line, rindex ($last_line, " ") + 1);
correct line, of course).
What happens if your data contains more than three lines?> }
> }
>
> __DATA__
> one two three
> four five six
> seven eight nine
>
> PRINTED RESULTS:
> ________________
>
> nine
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
Benjamin Goldberg Guest
-
Benjamin Goldberg #6
Re: array access problem - newbie question
Go Perl wrote:
[snip]To get the last number of the third column, try:> Where as i just need the output as 0.836534486876684 which is the last
> number of the third column. Can anyone please guide me here.
> Thanks in advance,
> GP
open (MAER_FILE, 'MAER_output.txt')
or die "could not open 'MAER_output.txt' $!";
my $lastline;
$lastline = $_ while <MAER_FILE>;
print ((split ' ', $lastline)[2]), "\n";
__END__
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
Benjamin Goldberg Guest



Reply With Quote

