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: Intermediate:
int2str conversion in perl

 



yasdel
Novice

Sep 8, 2010, 6:18 AM

Post #1 of 6 (1234 views)
int2str conversion in perl Can't Post

Hello Everbody.

How can i generate sequential key names in perl based on a counter value i=1:N ? Do you I have to to use any int2str conversion or you suggest an easier solution?

for i=1:N
{

keyi => [.. ]

}


thanks for you help.

Best Regards,
Yashar


shawnhcorey
Enthusiast


Sep 8, 2010, 7:48 AM

Post #2 of 6 (1229 views)
Re: [yasdel] int2str conversion in perl [In reply to] Can't Post

No, Perl does integer to string conversion automatically. If you want a sequence of numbers:

Code
#!/usr/bin/perl 

use strict;
use warnings;

for my $i ( 1 .. 25 ){
print "$i\n";
}


If you want a sequence of letters:

Code
#!/usr/bin/perl 

use strict;
use warnings;

for my $c ( 'a' .. 'zz' ){
print "$c\n";
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


yasdel
Novice

Sep 8, 2010, 8:03 AM

Post #3 of 6 (1227 views)
Re: [shawnhcorey] int2str conversion in perl [In reply to] Can't Post

Hi. Thanks for your reply.

However my problem is NOT abour printing rather string generation. My problem is mainly how to generate random string names. My idea is to use a counter at each instance & do a int2str conversion in order to obtain the string name.

Infact the strings generated will be assigned for Hash key names like below . If the situation is understandable for you, can I ask you expert guys, provide me with some simple and understandble solution. Many thanks in advance.

My code will look something like this :

Code:

my %hash=(
foreach $e (...)
{

KEY"i" => [$ ...];


}

);



Maybe my hash structure is not correct either.

My Best,
YAS


shawnhcorey
Enthusiast


Sep 8, 2010, 8:13 AM

Post #4 of 6 (1221 views)
Re: [yasdel] int2str conversion in perl [In reply to] Can't Post

I shall repeat myself: you do not need to do an integer-to-string conversion; Perl does it for you automatically.

Is it a requirement that the keys be random? Is being unique enough?

To generate unique keys:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my $key_counter = 0;

sub unique_key {
my $prefix = shift @_ || '';
my $suffix = shift @_ || '';

my $key = join( '', $prefix, $key_counter ++, $suffix );
return $key;
}

for ( 1 .. 10 ){
my $key = unique_key( 'key_' );
print "$key\n";
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


yasdel
Novice

Sep 8, 2010, 9:31 AM

Post #5 of 6 (1219 views)
Re: [shawnhcorey] int2str conversion in perl [In reply to] Can't Post

Dear shawnhcorey,

Thank you very much for your smart response. I think it will make it :)

I would like to explain you why I am using hash of arrays and for that I need to do this. Can you comment if my idea is good? thanks in advance

I want to use Hash becuse me data are text and numbers. I want to place ths data which size is big in a hash (like a table) and then sort the hash (the table) based on one column.

Suppose my data is as below provided in txt file and you want to sort out the data and produce the results in a new text file :

# Input

Code:

#1
name : str1
time : 5
place : desktop/str1.txt

#2
name : str5
time : 10
place : desktop/str5.txt

#3
name : str2
time : 3
place : desktop/str2.txt



After running the code should look like this :

# Output
Code:

#3
name : str2
time : 3
place : desktop/str2.txt

#1
name : str1
time : 5
place : desktop/str1.txt

#2
name : str5
time : 10
place : desktop/str5.txt


This in example and in realirt I have alot of data like #1 #2 #3 ... #1000 so if each group is going to be assigned by a one hash, it is not good to name hashes manullay. We prefer to put it in a loop and let perl generate the key names.

I hope you would realize the situation quite well now.

I look forward to hearing from you.

My Best,
Yas

My Best,
YAS


BillKSmith
Veteran

Sep 8, 2010, 12:29 PM

Post #6 of 6 (1203 views)
Re: [yasdel] int2str conversion in perl [In reply to] Can't Post

A hash of arrays is a very poor choice of data structure for the data that you showed us. The hash that you propose does not "contain" strings or numbers. Rather the values of the hash are references to arrays. The arrays do contain both strings and numbers. (Unlike many other languages, this is not a problem for perl)



It is not clear whether you should use an array of hashes or an array of arrays. Each has advantages. You will not understand what Shawn and I are trying to tell you until you read and understand the related perl documentation. At the command line, type perldoc perlreftut. The tutorial on references will be displayed. In the same way, use the perldoc command to read perldsc (Data Structure Cookbook) and perlref (Perl References and Nested Data Structures).
Good Luck,
Bill

 
 


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

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