CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Array Returns Multiple Values

 



pbhuter
Novice

May 17, 2013, 12:05 PM

Post #1 of 24 (1255 views)
Array Returns Multiple Values Can't Post

I have the following code:

[code]@myarray = ($lines[$count]) =~ m/(\d+)/g);
$time = $myarray[1];[/code]

Which should return a number for myarray[1], but it returns the number I want, followed by a newline, followed by either a one or a zero. The above code works for pulling all other numbers from my textfile, so I don't have any reason to believe it wouldn't work for this particular situation, although my newness to Perl could explain why I don't know what's going on. The line of text I am reading looks something like:

0000000:words words Time: #########

where "#########" is what I want to pull out.


Laurent_R
Veteran / Moderator

May 17, 2013, 2:44 PM

Post #2 of 24 (1250 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

Your regex will certainly not match "######".

Or is "######" standing for numbers? Then you should explain better what you mean.

If it stands for number, then it should work. There is no reason why the /(\d+)/ regex would capture a new line, and it does not, as shown in the debugger session below:


Code
  DB<5> $c = '0000000:words words Time: 3742 ' 

DB<6> @myarray = $c =~ m/(\d+)/g

DB<7> x @myarray
0 0000000
1 3742
DB<8> $c = '0000000:words words Time: 3742\n '

DB<9> @myarray = $c =~ m/(\d+)/g

DB<10> x @myarray
0 0000000
1 3742
DB<11>


Please provide the exact line that is giving you trouble (or another line giving you the same issue), we can't guess it.


pbhuter
Novice

May 17, 2013, 3:29 PM

Post #3 of 24 (1243 views)
Re: [Laurent_R] Array Returns Multiple Values [In reply to] Can't Post

The #s stand for a sequence of nine numbers, I thought that would be clear. Is it possible that there is some hidden numbers? This is a data file generated by a program, and I am not sure how the file is generated, but it looks like just a plain text file. However, when I run the code I posted (which appears to be the same as your post), I get (again, #s represent numbers):

#########
1 (or 0)

for myarray[1]

This is on two lines, and when I try:


Code
$time = $myarray[1]


I get $time = 1 (or 0), which is not what I want. I tried treating myarray as a multidimensional array:


Code
$time = $myarray[1][0]


or


Code
$time = $myarray[1]->[0]


but those produce blanks.


FishMonger
Veteran / Moderator

May 18, 2013, 7:42 AM

Post #4 of 24 (1230 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

Your parens are not matched up. The parens are not needed in this case, so just remove them.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my @lines = '0000000:words words Time: 123456789';

my $count = 0;

my @myarray = $lines[$count] =~ m/(\d+)/g;
print Dumper \@myarray;

my $time = $myarray[1];
print "$time\n";



BillKSmith
Veteran

May 18, 2013, 7:58 AM

Post #5 of 24 (1227 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

The code you posted, is not the code you ran (Parenthesis in the statement with the regex do not balance. ) You would get an error.

I have produced a small working program using as much of your code as possible. It works fine. Would you please modify it to demonstrate the error.


Code
use strict; 
use warnings;
use Data::Dumper qw(Dumper);
my $count = 0;
my @lines = (
'0000000:words words Time: 999999999',
);
my @myarray = $lines[$count] =~ m/(\d+)/g;
my $time = $myarray[1];
print Dumper \@myarray, \$time;


Output:

Code
$VAR1 = [ 
'0000000',
'999999999'
];
$VAR2 = \'999999999';

Good Luck,
Bill


pbhuter
Novice

May 20, 2013, 5:09 AM

Post #6 of 24 (1208 views)
Re: [BillKSmith] Array Returns Multiple Values [In reply to] Can't Post

I ran the code that was posted in replies - still getting:

#########
1

All other times I use this same code in this script, I get what I am looking for, so I don't know why it isn't working in this instance.


BillKSmith
Veteran

May 20, 2013, 5:40 AM

Post #7 of 24 (1206 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

We need to see a complete program that demonstrates the error. Please run the code (exactly as posted) that I posted previously. Verify that it works correctly. Modify it so it fails in the way you describe. Post the modified program. If the original fails, or you are unable to make it fail, show us exactly what you tried. Post complete examples that we can run!
Good Luck,
Bill


pbhuter
Novice

May 20, 2013, 6:08 AM

Post #8 of 24 (1203 views)
Re: [BillKSmith] Array Returns Multiple Values [In reply to] Can't Post

Code:

Code
@myarray = (); 
@myarray = $lines[count] =~ m/(-?\d+\.\d+)/g;
$intX = @myarray[0];
$intY = @myarray[1];
$intZ = @myarray[2];


Input:
000000000: words, words, words -#####.#####, -#####.#####, #####.#####

Output:
$intX = -#####.#####
$intY = -#####.#####
$intZ = #####.#####

So the above works.

Next,

Code:

Code
@myarray = (); 
@myarray = $lines[count] = m/(\d+)/g;
$time = @myarray[1];


Input:
000000000: words, words, words Time: #########

Output:
$time =
#########
1

So the above does not work.


Laurent_R
Veteran / Moderator

May 20, 2013, 7:50 AM

Post #9 of 24 (1198 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

What is:


Code
$lines[count]


supposed to be? Especially, what is "count" supposed to be?

If you had the

Code
use strict; 
use warnings;

pragmas turned on, the "count" bareword would not be allowed. And they might pickup some other errors in your code.


pbhuter
Novice

May 20, 2013, 8:02 AM

Post #10 of 24 (1196 views)
Re: [Laurent_R] Array Returns Multiple Values [In reply to] Can't Post

Sorry:


Code
$lines[$count]


I am retyping to code from another system.


BillKSmith
Veteran

May 20, 2013, 8:24 AM

Post #11 of 24 (1191 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

Every line of your failing example is the same as one in my working example. The difference must be in the part you are not showing us. Post complete programs that we can run and debug.
Good Luck,
Bill


pbhuter
Novice

May 20, 2013, 8:42 AM

Post #12 of 24 (1189 views)
Re: [BillKSmith] Array Returns Multiple Values [In reply to] Can't Post

This is as close as I can get to "full script":


Code
@lines = <FILE>; 
$linesSize = @lines;
for (my $count = 1; $count <= $linesSize; $count++)
{
...
@myarray = ();
@myarray = $lines[$count] =~ m/(\d+)/g;
$ID = $myarray[1];

$count = $count + 1;

@myarray = ();
@myarray = $lines[$count] =~ m/(\d+)/g;
$Time = $myarray[1];

$outString = join " ", $ID, $Time;
}


Input:

Quote
...
000000000: words, wordsID: 1234
000000000: words, wordsTime: 1234567


Output:
$Time =

Quote
1234567
1


$outString =

Quote
1234 1


As you can see, I get my first variable (ID) correctly, and it starts to pull the second variable (Time), but then adds a newline and a 1 (or a 0, later in the running). The newline and 1 are NOT visible in the input file.


BillKSmith
Veteran

May 20, 2013, 10:32 AM

Post #13 of 24 (1170 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

Much better, but I still cannot duplicate the problem. All we need for a complete script is the print statements that display the bad results.

It now seems that you intend to merge data from two lines of input to create one line of output. Your for statement repeats this pattern for all the data in the file.

One problem is that $count starts at one. This ignores the first line of data which is in $line[0] and later reads one element past the end of @line (a likely source for your strange characters.).
Good Luck,
Bill


hwnd
User

May 20, 2013, 10:37 AM

Post #14 of 24 (1170 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

Your input string has changed several times in this forum topic, so which one is it?

Is the line in the file without spaces after ' : ' ?


Quote
0000000:words words Time:1234567



Or does the line in the file have ongoing 'words' followed by spaces and commas?


Quote
000000000: words, words, words, wordsTime: 1234567



or


Quote
000000000: words, wordsID: 1234
000000000: words, wordsTime: 1234567



(This post was edited by hwnd on May 20, 2013, 10:41 AM)


pbhuter
Novice

May 20, 2013, 11:42 AM

Post #15 of 24 (1156 views)
Re: [hwnd] Array Returns Multiple Values [In reply to] Can't Post

All lines in my input file start with:


Quote
000000000:


This is followed by some words:


Quote
words, words, words


The "words" may include some identifier, such as "ID" or "Time", but that shouldn't matter because I'm not looking for words. After the words is another number:


Quote
1234...


The number is varying in length, but that is what I want, not a newline followed by something else.


Laurent_R
Veteran / Moderator

May 20, 2013, 2:56 PM

Post #16 of 24 (1134 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

I just tried your program with just one change in the loop (start #count at 0, not 1, and change the loop stop condition similarly):


Code
for (my $count = 0; $count < $linesSize; $count++)


and adding a print after the join.

The exact code that I ran is as follows:


Code
@lines = <DATA>;  
$linesSize = @lines;
for (my $count = 0; $count < $linesSize; $count++)
{

@myarray = ();
@myarray = $lines[$count] =~ m/(\d+)/g;
$ID = $myarray[1];

$count = $count + 1;

@myarray = ();
@myarray = $lines[$count] =~ m/(\d+)/g;
$Time = $myarray[1];

$outString = join " ", $ID, $Time;
print $outString, "\n";
}

__DATA__
000000000: words, wordsID: 1234
000000000: words, wordsTime: 1234567


(I would not write a program this way, but I wanted to stay as close as possible to your code, except for the necessary changes.)

Then running it, I obtain:


Code
$ perl mult.pl 
1234 1234567


which is presumably what you want.

Try my changes on your program and tell us what you get.

But if it is still wrong, then the problem comes mosty probably from the input data, and I guess you'll have to insert in a post a test file that actually exhibits the problem you are facing.


hwnd
User

May 20, 2013, 5:07 PM

Post #17 of 24 (1119 views)
Re: [Laurent_R] Array Returns Multiple Values [In reply to] Can't Post

You precisely said the number on the end is what you want and it varys in length. From your code, you're joining each line's end numbers together with " ". I've adapted my code to the way you write it, so if that is the case try this and see if it works out for you.

File input:


Code
000000000: words, wordsID: 1234  
000000000: words, wordsTime: 1234567
000000000: words, words, words, words, wordsID: 5678
000000000: words, words, words, words, wordsTime: 9999999



Code:


Code
@lines = <DATA>; 
my @wanted;

for my $i ( 0..$#lines ) {
my @data = split /\s+/, $lines[$i];
push ( @wanted, $data[$#data] );
}

$outString = join " ", @wanted;
print $outString, "\n";



Output:


Code
1234 1234567 5678 9999999



pbhuter
Novice

May 21, 2013, 6:36 AM

Post #18 of 24 (1102 views)
Re: [hwnd] Array Returns Multiple Values [In reply to] Can't Post

Using:


Code
my @data = split /\s+/, $lines[$count]; 
push ( @myarray, $data[$#data] );
$time = $myarray[0];


All I get is that phantom 1 or 0 (that doesn't show up in the text file).

Trying:


Code
push ( @myarray, $data[$#data - 1] );


I end up with something about ten lines above where I should be (and not a number).

Thanks for all the help so far, I feel we are making progress.


hwnd
User

May 21, 2013, 6:51 AM

Post #19 of 24 (1100 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

Can you post your entire script code you are using? The code I wrote above worked fine for me so It seems something else is the problem or your input file is different than you say.


(This post was edited by hwnd on May 21, 2013, 7:51 AM)


pbhuter
Novice

May 21, 2013, 7:29 AM

Post #20 of 24 (1090 views)
Re: [hwnd] Array Returns Multiple Values [In reply to] Can't Post

Before I go about posting my entire script (only because it is on a different computer not connected to the Internet), is there a way to see if there are hidden characters in my input file? That way I can see if there is something that is being captured at the end of that line that I can't see.

Thanks.


hwnd
User

May 21, 2013, 7:53 AM

Post #21 of 24 (1081 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

Try this:


Code
myfile.txt  


000000000: words, wordsID: 1234
000000000: words, wordsTime: 1234567



Code
 my @wanted;   

open my $fh, '<', 'myfile.txt' or die "failed: $!";

while ( my $line = <$fh> ) {
chomp $line;
my ($time) = $line =~ /\s.+?(\d+)/g;
$time =~ s/\r\n$//g;
push @wanted, $time;
}

my $outString = join " ", @wanted;
print $outString, "\n";




Code
__OUTPUT__ 
1234 1234567



(This post was edited by hwnd on May 21, 2013, 7:59 AM)


FishMonger
Veteran / Moderator

May 21, 2013, 8:03 AM

Post #22 of 24 (1074 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post


Quote
is there a way to see if there are hidden characters in my input file?


Yes, use the od utility/command.
http://unixhelp.ed.ac.uk/CGI/man-cgi?od

If you're on Windows, you can install the CoreUtils for Windows.
http://gnuwin32.sourceforge.net/packages/coreutils.htm


BillKSmith
Veteran

May 21, 2013, 8:26 AM

Post #23 of 24 (1070 views)
Re: [pbhuter] Array Returns Multiple Values [In reply to] Can't Post

The best way to check you data file for unexpected whitespace or non-printing characters is to examine it with a hex editor. Next best thing is your text editor. (It probably has a mode to display non-printing characters). So far three of us have posted code that "works" with made-up data. Try all of them with real data. Even if they are not exactly what you want, they could validate your data. If the data is bad, fix a sample of it and try again. Let us know what happened.

I Think that it much more likely that the problem is in your code rather than your data. It may eventually be necessary to post your entire script, but not yet. Much better to add print statements to the script that you already posted. This will show us (and you!) exactly what is failing and when.
Good Luck,
Bill


pbhuter
Novice

May 21, 2013, 9:16 AM

Post #24 of 24 (1058 views)
Re: [BillKSmith] Array Returns Multiple Values [In reply to] Can't Post

Printing things out did the trick. I had an earlier statement that captured data using a string in the line, and that string was repeated, causing me to capture a different line for my "time".

Thanks for the help.


(This post was edited by pbhuter on May 21, 2013, 9:16 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives