simple array question

I can get the following to work correctly:

*********************************
$extra = (key,value,key,value)

%group = ($extra);
while(($key,$value) = each(%group)) {
     print "value:$value  key:$key.\n";
}
****************************************

it prints out:
*********************************************
value:  Key:key,value,key,value
*********************************************

I works if I set %group equal to "('key', 'value', 'key', 'value');"

but I cannot do that because the value for $extra is pulled from the following text file:

*****************************
#array|extra
1|key,value,key,value
2|key,value,key,value
*****************************

I can change how it is in the text file though if needed.
thanks.
derek4716Asked:
Who is Participating?
 
ahoffmannConnect With a Mentor Commented:
while(($key,$value) = each(%groups)) {print "KEY: $key VAL $value.\n";}
0
 
TintinCommented:
You would find it easier to have a single key/value in the text file, ie:

1|key,value
2|key,value

then you can simply do

open FILE,"file.txt" or die "Can not open file.txt $!\n";

while (<FILE>) {
  m/\d+|(\w+),(\w+)/;
  $groups{$1}=$2;
}
0
 
samriCommented:
How about;

%extra = (key1,1,key2,2);

foreach $key (keys %extra) {
  print "Key: $key\t Val: $extra{$key}\n";
}
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
derek4716Author Commented:
None on those scripts do what I aksed.  I stated before the the value for %extra was being pulled from a text file and I gave an example of the textfile.  

TinTin,
   I can't limit it to just one key,value per line...some lines may have up to six pairs.

Is there even a way to do what I want?
0
 
ahoffmannCommented:
open F,"<file";
foreach $l (<F>){
  ($d,$p)=split(/\|/,$l);
  push(@groups,split(",",$p));
}
%groups=@groups;
close F;
0
 
derek4716Author Commented:
ahoffmann,
   I can't get your script to do anything.?
0
 
samriCommented:
very rude code, but should work (still taking perl-101) :)

-- infile.txt
#array|extra
1|key1,value1,key2,value2
2|key3,value3,key4,value4
3|key5,value5
4|key6,value6,key7,value7,key8,value8,key9,value9
--

-- test.pl
#!c:/perl/bin/perl

open(IN,"infile.txt");

while(<IN>) {
  chop;
  next if (/^\#/);      # skip if found comment
  my ($a,$b)=split(/\|/);
  @tmparr=split(/\,/,$b);
  foreach (@tmparr) {
#    print "$_\n";
    push (@tmparr2,$_);
  }
#  print "== $b\n";
}
close(IN);

while (<@tmparr2>) {
  $key=shift(@tmparr2);
  $val=shift(@tmparr2);
  $extra{$key}=$val if ($val);
#  print "KEY $key VAL $val\n";
}
--

0
 
derek4716Author Commented:
I had to edit your code a little to get it to work, here is what I did, I have only one problem left:
******************************
open(IN,"infile.txt");

while(<IN>) {
 chop;
 my ($a,$b)=split(/\|/);
 @tmparr=split(/\,/,$b);
 foreach (@tmparr) {
   push (@tmparr2,$_);
 }
}
close(IN);

while (<@tmparr2>) {
  $key=shift(@tmparr2);
  $val=shift(@tmparr2);
  $extra{$key}=$val if ($val);
  print "KEY $key VAL $val\n";
}
**************************************

When it prints out the the page it prints the following(using just row 1 of the file for testing):
****
KEY key1 VAL value1 KEY key2 VAL value2 KEY VAL KEY VA
****

As you can see it prints out a couple extra KEY and VAL's.

Any ideas why?
Thanks so far, though:)
0
 
TintinCommented:
derek4716.

First you said "I can change how it is in the text file though if needed."

then you said "I can't limit it to just one key,value per line...some lines may have up to six pairs."

which statement is correct?

Also you said ahoffman's script to do anything.  What do you mean?  It works perfectly for me.  I assume you added a print to it to see the output?
0
 
ahoffmannCommented:
my script works perfect formewith your example
what exactly is the problem?
just through in some print commands ...
0
 
ahoffmannCommented:
also keep in mind that my suggestion work with any number of key value pairs per line,
as long as there is a even, not odd, number of key-value pairs
0
 
derek4716Author Commented:
TinTin what I meant was that I can change how the pair are in the text file, for example, I can replace the comma's with anything needed or use quotes if need be.

ahoffmann
I will retry your script, I was probably doing something wrong.
0
 
derek4716Author Commented:
ahoffmann, Here is the code that I tried with yours, mabye you can see what I am doing wrong.
*****************************
open F,"<test.dat";
foreach $l (<F>){
 ($d,$p)=split(/\|/,$l);
 push(@groups,split(",",$p));
}
%groups=@groups;
close F;
while(($key,$value) = each(%group)) {
     print "KEY: $key VAL $value.\n";
}
****************************
0
 
derek4716Author Commented:
thanks, figures I did something stupid :)
0
 
derek4716Author Commented:
one more quick question:

what if in my file I added another column such as:

******
#Id|array|extra
1234|1|key,value,key,value
3241|2|key,value,key,value
******

How do have the code go to the third column now?
0
 
TintinCommented:
And the typo of 'groups' is a classic example of where

use strict;

and

use warnings;

would have come to the rescue.
0
 
derek4716Author Commented:
100 points to whoever can answer this question i asked earlier

**************
one more quick question:

what if in my file I added another column such as:

******
#Id|array|extra
1234|1|key,value,key,value
3241|2|key,value,key,value
******

How do have the code go to the third column now?
*******************
0
 
TintinCommented:
#!/usr/bin/perl
use strict;

open F,"test.dat" or die "Can not open test.dat $!\n";

my @groups;

while (<F>) {
  my ($data) = m/\d+\|\d+\|(.*)/;
  push(@groups,split(",",$data));
}

close F;

my %groups=@groups;

while (my ($key,$value) = each(%groups)) {
    print "KEY: $key VAL $value.\n";
}
0
 
derek4716Author Commented:
Thanks TinTin, I created a topic for your points.  How do I continue to add additional columns?  what if I were to add 2 or 3 more cloumns, how would I let your code know?
0
 
TintinCommented:
Thinking about it, the regex can be simplified and will handle any number of columns by doing

my ($data) = m/.*\|(.*)/;

ie:  match everything after the last |
0
 
ahoffmannCommented:
lost the focus, sorry ...
but is there something to be done for the initial question?
0
 
samriCommented:
ahoffmann (<-- got ur name right!)

- grab some coffee :)
0
All Courses

From novice to tech pro — start learning today.