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:
Constructing a record...positioning question

 



S_Shrum
User

Nov 4, 2001, 12:44 AM

Post #1 of 3 (605 views)
Constructing a record...positioning question Can't Post

This goes hand in hand with my db_random.pl post earlier....

I am attempting to generate 1 record...really a line of text with delimiters.

What I want is to be able to create a semi-blank record. I say semi-blank because it will only have 1 value in it. Something like:

|||||||123||||

...where the '123' is in the 7th row (starting from 0).

Now here's the catch...it's not always going to be the 7th row.

I need a way to construct this semi-blank row while at the same time passing it a delimiter (called $delimiter), a value (called $random_number), and a field number (called $in_column).

Any help is greatly apprechiated.

Sean Shrum
http://sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat


rGeoffrey
User / Moderator

Nov 7, 2001, 11:45 AM

Post #2 of 3 (595 views)
Re: Constructing a record...positioning question [In reply to] Can't Post

This will do it...


Code
#!/usr/local/bin/perl 

sub generate
{
my ($delimiter, $random_number, $in_column, $column_count) = @_;
$column_count ||= 10;

my @array = map { '' } (0..$column_count);
$array[$in_column] = $random_number;

return (join ($delimiter, @array));
}

my $sample_data = 'me';

print join ("\n",
&generate ('|', $sample_data, 3, 10),
&generate ('|', $sample_data, 4, 10),
&generate ('|', $sample_data, 5),
''
);

I added a fourth parameter to tell how many columns the row will have, so we know how many $delimiters to use. But as you will probably always use the same number, it is optional and the function will use the default value if it is not given one.



S_Shrum
User

Nov 7, 2001, 10:47 PM

Post #3 of 3 (589 views)
Re: Constructing a record...positioning question [In reply to] Can't Post

Thanks...I will take a look at it...in the meantime, this is what I came up with:


Code
for $j (0..$#{$data[0]}) {  
$new_record = $new_record . $input->param('delimiter') unless ( $j == 0);
if ( $j == $in_column ) { $new_record = $new_record . $random_number; }
}

I'm still running through the various scenarios in my head but so far this works all the time.

Thanks for the response.

Sean Shrum
sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat

 
 


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

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