Re: creating hash from scalar variable

2007-05-03 Thread Goksie
Matthew J. Avitable wrote: Unf. Got the picture! I'll spend my night in the stockades :) -m Rob Dixon wrote: Matthew J. Avitable wrote: Given the original string ... my $test = 'NAS-IP-Address = 192.168.42.1 ... Acct-Unique-Session-Id = 87d380e1881d226c Timestamp =

Re: creating hash from scalar variable

2007-05-03 Thread David Van Ginneken
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; $/ = \n\n; # Specify the record separator as 2 new lines.. my $fn = 'detail-20070423_1.txt'; open my $fh, '', $fn or die $!; while($fh){ my %test; map { my ($fn,$val) = split(/=/,$_,2); $fn =~ s/^\s*//g; $fn

Re: creating hash from scalar variable

2007-05-03 Thread Dr.Ruud
David Van Ginneken schreef: $fn =~ s/^\s*//g; $fn =~ s/\s*$//g; $val =~ s/^\s*?//g if defined $val; $val =~ s/?\s*//g if defined $val; The g-modifiers and the * quantifiers and the ? are either not right or not necessary. Alternative: s/^\s+//, s/\s+$//

Re: creating hash from scalar variable

2007-04-30 Thread Randal L. Schwartz
Matthew == Matthew J Avitable [EMAIL PROTECTED] writes: Matthew You could also invoke perl 5.8's ability to treat an in-memory string as a Matthew file: You can, but that's rapidly sliding into obfuscation territory. You already have the data... why shove it out as a filehandle and back in

Re: creating hash from scalar variable

2007-04-30 Thread Rob Dixon
Matthew J. Avitable wrote: Given the original string ... my $test = 'NAS-IP-Address = 192.168.42.1 ... Acct-Unique-Session-Id = 87d380e1881d226c Timestamp = 1177282824'; You could also invoke perl 5.8's ability to treat an in-memory string as a file: ## get a filehandle on

Re: creating hash from scalar variable

2007-04-30 Thread Goksie
Rob Dixon wrote: Matthew J. Avitable wrote: Given the original string ... my $test = 'NAS-IP-Address = 192.168.42.1 ... Acct-Unique-Session-Id = 87d380e1881d226c Timestamp = 1177282824'; You could also invoke perl 5.8's ability to treat an in-memory string as a file: ##

Re: creating hash from scalar variable

2007-04-30 Thread Matthew J. Avitable
Unf. Got the picture! I'll spend my night in the stockades :) -m Rob Dixon wrote: Matthew J. Avitable wrote: Given the original string ... my $test = 'NAS-IP-Address = 192.168.42.1 ... Acct-Unique-Session-Id = 87d380e1881d226c Timestamp = 1177282824'; You could also invoke

Re: creating hash from scalar variable

2007-04-29 Thread Rob Coops
Hi there, Your problem here is that perl is kind enough to see the whole scalar as one line. So what you would have to do is break it up in several linse shove them in an array. Then do a foreach on the array to split and drop it in the hash like so: my @array = split( /\n/, $test ); foreach

Re: creating hash from scalar variable

2007-04-29 Thread David Van Ginneken
I think something like this would work for you. my %test; map { my ($fn,$val) = split(/=/,$_,2); $test{$fn}=$val;} split(/\n/, $test); I noticed some of your values had equal signs in them, so in the inside split, I also specified you wanted 2 values so that you receive the full expected value

Re: creating hash from scalar variable

2007-04-29 Thread yaron
Hi, The line -- my %test = my($fname, $fvalu)=split(/=/, $test); Will insert only two elements into %test. Try: my %test = split (/=/,$test); Yaron Kahanovitch - Original Message - From: Goksie [EMAIL PROTECTED] To: Perl Beginners beginners@perl.org Sent: Sunday, April 29, 2007

Re: creating hash from scalar variable

2007-04-29 Thread Rob Dixon
Goksie wrote: hello, Can someone help me correct this code. if i print, it only print the first line. Goksie #!/usr/bin/perl use strict; my $test = 'NAS-IP-Address = 192.168.42.1 Quintum-NAS-Port = 0 0/0/c1dc2a26 NAS-Port-Type = Async User-Name = 192.168.42.8

Re: creating hash from scalar variable

2007-04-29 Thread Martin Barth
Hi, if you're reading a config file to get the string maybe Config::General is handy. HTH Martin On Sun, 29 Apr 2007 14:27:52 +0100 Goksie [EMAIL PROTECTED] wrote: hello, Can someone help me correct this code. if i print, it only print the first line. Goksie #!/usr/bin/perl use

Re: creating hash from scalar variable

2007-04-29 Thread Rodrick Brown
On 4/29/07, Goksie [EMAIL PROTECTED] wrote: hello, Can someone help me correct this code. if i print, it only print the first line. Goksie #!/usr/bin/perl use strict; my $test = 'NAS-IP-Address = 192.168.42.1 Quintum-NAS-Port = 0 0/0/c1dc2a26 NAS-Port-Type = Async

Re: creating hash from scalar variable

2007-04-29 Thread Rob Dixon
Rodrick Brown wrote: use Data::Dumper; my %h; map { $h{$_-[0]}=$_-[1] } map { [ split/=/,$_ ] } split/\n/,$test; print Dumper(\%h); Or, more intelligibly, my %h; foreach (split /\n/, $test) { my ($key, $val) = split /=/; $h{$key} = $val; } Rob -- To unsubscribe, e-mail: [EMAIL

Re: creating hash from scalar variable

2007-04-29 Thread Matthew J. Avitable
Given the original string ... my $test = 'NAS-IP-Address = 192.168.42.1 ... Acct-Unique-Session-Id = 87d380e1881d226c Timestamp = 1177282824'; You could also invoke perl 5.8's ability to treat an in-memory string as a file: ## get a filehandle on $test open(my $fh, '', \$test)

Re: Creating hash with multiple keys for an array

2004-08-08 Thread Gunnar Hjalmarsson
Edward Wijaya wrote: my @AoH = ( { values = ['AGCAG','AGCCG','AGCCGGGCG','AGCCAGGAG'] }, { values = ['AGCGGAGCG','AGCCGAGGG','AGCGGAGGG'] }, ); for ( 0..$#AoH ) { $AoH[$_]-{ic} = compute_ic( @{ $AoH[$_]-{values} } ); } print Dumper @AoH; Thanks Gunnar, I managed to

Re: Creating hash with multiple keys for an array

2004-08-08 Thread Edward WIJAYA
What have you done to find out? perldoc -f sort perldoc -q sort an array I think you also need to read up on data structures: perldoc perldsc Gunnar, It works! I can't express enough my gratitude for your help and patience. I apologize for having trouble you this far. Actually I

Re: Creating hash with multiple keys for an array

2004-08-07 Thread Gunnar Hjalmarsson
Edward Wijaya wrote: Thanks so much for your reply Gunnar, The purpose is as follows. For example these lines: AGCAG,AGCCG,AGCCGGGCG,AGCCAGGAG 15.188721875540 AGCGGAGCG,AGCCGAGGG,AGCGGAGGG 16.163408331891 \_/ \_/ @Array1

Re: Creating hash with multiple keys for an array

2004-08-07 Thread Edward WIJAYA
Thanks so much for your reply Gunnar. However there is a bit complication. I just realize that hash table can only return the values of unique key. Please try to execute the code below along with the attached file, and the target answer below for clarity, (the current code return the deviated

Re: Creating hash with multiple keys for an array

2004-08-07 Thread Gunnar Hjalmarsson
Edward Wijaya wrote: I just realize that hash table can only return the values of unique key. And the compute_ic() function computes numbers that are not unique, so they can't be hash keys, I see. You have a new problem. We don't know much about the bigger picture here. Would possibly an array of

Re: Creating hash with multiple keys for an array

2004-08-07 Thread Edward WIJAYA
my @AoH = ( { values = ['AGCAG','AGCCG','AGCCGGGCG','AGCCAGGAG'] }, { values = ['AGCGGAGCG','AGCCGAGGG','AGCGGAGGG'] }, ); for ( 0..$#AoH ) { $AoH[$_]-{ic} = compute_ic( @{ $AoH[$_]-{values} } ); } print Dumper @AoH; Thanks Gunnar, I managed to construct the Array of Hashes

Re: Creating hash with multiple keys for an array

2004-08-06 Thread Gunnar Hjalmarsson
Edward Wijaya wrote: Is there anyway in Perl to create hash with multiple key for an array? Yes. my %HoA = ( key1 = [ @array1 ] ); $HoA{key2} = $HoA{key1}; The purpose is as follows. For example these lines: AGCAG,AGCCG,AGCCGGGCG,AGCCAGGAG 15.188721875540

Re: Creating hash with multiple keys for an array

2004-08-06 Thread Edward WIJAYA
Thanks so much for your reply Gunnar, The purpose is as follows. For example these lines: AGCAG,AGCCG,AGCCGGGCG,AGCCAGGAG 15.188721875540 AGCGGAGCG,AGCCGAGGG,AGCGGAGGG 16.163408331891 \_/ \_/ @Array1

RE: creating hash from name/value found in file.

2003-01-29 Thread Timothy Johnson
Why not something like this? my %hash; while(SM_FILE){ chomp $_;#remove the newline my($key,$value) = split(/,/,$_); #split by commas or whatever $hash{$key} = $value;#assign the value here } foreach(sort keys %hash){ print $key = $value\n; }

RE: creating hash from name/value found in file.

2003-01-29 Thread Toby Stuart
-Original Message- From: Jamie Risk [mailto:[EMAIL PROTECTED]] Sent: Thursday, January 30, 2003 9:32 AM To: [EMAIL PROTECTED] Subject: creating hash from name/value found in file. Okay, it's 5:25pm and I started programming PERL shortly after downloading the 5.8.0 tarballs

Re: creating hash from name/value found in file.

2003-01-29 Thread Jamie Risk
Thanks, Timothy, I almost had it before I decided to look back at the list. My only concern was the creation of data space for each new key, I guess it's a non issue. ALTHOUGH, if I have repetitions of keys, code below would obliterate previous key data, would it not? I guess I'm looking at a hash

RE: creating hash from name/value found in file.

2003-01-29 Thread Timothy Johnson
PROTECTED]] Sent: Wednesday, January 29, 2003 3:03 PM To: [EMAIL PROTECTED] Subject: Re: creating hash from name/value found in file. Thanks, Timothy, I almost had it before I decided to look back at the list. My only concern was the creation of data space for each new key, I guess it's a non issue

Re: creating hash from name/value found in file.

2003-01-29 Thread John W. Krahn
Jamie Risk wrote: Okay, it's 5:25pm and I started programming PERL shortly after downloading the 5.8.0 tarballs at 2:30pm. I'd like to create a hash from a text file that has name/value pairs, one per line. So far, I'm only capable of reading it in as a list - this PERL stuff really seems

Re: creating hash from name/value found in file.

2003-01-29 Thread Jamie Risk
Original Message- From: Jamie Risk [mailto:[EMAIL PROTECTED]] Sent: Wednesday, January 29, 2003 3:03 PM To: [EMAIL PROTECTED] Subject: Re: creating hash from name/value found in file. Thanks, Timothy, I almost had it before I decided to look back at the list. My only concern was the

Re: Creating hash?

2002-10-01 Thread Sudarshan Raghavan
On Mon, 30 Sep 2002, Paul and Joann Van Dalen wrote: Hi all, Given an input file that has the following records: 123ABCXX112Zz 123DEFXX113Z 123EEFXX112 Zz 444cccvvbfdc 444

RE: Creating hash?

2002-10-01 Thread Nikola Janceski
open(FILE, yourfile) or die can't open $!; my(%LINES); while(FILE){ my($key) = split; $LINES{$key) = $_ unless exists $LINES{$key); } close FILE; # %LINES now has key value pairs of '123' and '444' as the keys and the value is the first occurence in the file. -Original

Re: Creating hash?

2002-10-01 Thread Robin Cragg
Hi Paul, Ihashes would need a little bit of work to work with this, as you will have multiple entries with the same key, eg you have three lines beginning 123. if you have all you lines in an array, you could do this: foreach $line (sort @arry) { die Invalid input: $line\n unless ($line

RE: Creating hash?

2002-10-01 Thread Kipp, James
and just so you don't try to load any blank lines into the hash and get an error, throw in a check for blank lines open (INPUTFILE, $your_input_file) or die Error opening $your_input_file: $!\n; my %uniq_hash; while (INPUTFILE) { next if /^$/; ## SKIP BLANKS (my

RE: Creating hash?

2002-10-01 Thread Sudarshan Raghavan
On Tue, 1 Oct 2002, Kipp, James wrote: and just so you don't try to load any blank lines into the hash and get an error, throw in a check for blank lines open (INPUTFILE, $your_input_file) or die Error opening $your_input_file: $!\n; my %uniq_hash; while (INPUTFILE) {

RE: Creating hash?

2002-10-01 Thread Kipp, James
next if /^$/; ## SKIP BLANKS should have done that check, thanks for the correction Actually Nikola's version saves us from needing this check by throwing in the unless line :-) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]

Re: Creating hash?

2002-10-01 Thread david
Paul And Joann Van Dalen wrote: Hi all, Given an input file that has the following records: 123ABCXX112Zz 123DEFXX113Z 123EEFXX112 Zz 444cccvvbfdc 444CCdvvbfd

Re: Creating hash?

2002-10-01 Thread John W. Krahn
Paul And Joann Van Dalen wrote: Hi all, Given an input file that has the following records: 123ABCXX112Zz 123DEFXX113Z 123EEFXX112 Zz 444cccvvbfdc 444CCdvvbfd