?
Solved

simple array question

Posted on 2003-03-02
22
Medium Priority
?
131 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:derek4716
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 5
  • +1
22 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 8054040
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
 
LVL 15

Expert Comment

by:samri
ID: 8055987
How about;

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

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

Author Comment

by:derek4716
ID: 8057086
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
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
LVL 51

Expert Comment

by:ahoffmann
ID: 8057441
open F,"<file";
foreach $l (<F>){
  ($d,$p)=split(/\|/,$l);
  push(@groups,split(",",$p));
}
%groups=@groups;
close F;
0
 

Author Comment

by:derek4716
ID: 8057594
ahoffmann,
   I can't get your script to do anything.?
0
 
LVL 15

Expert Comment

by:samri
ID: 8058574
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
 

Author Comment

by:derek4716
ID: 8059707
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
 
LVL 48

Expert Comment

by:Tintin
ID: 8060153
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 8060162
my script works perfect formewith your example
what exactly is the problem?
just through in some print commands ...
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 8060232
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
 

Author Comment

by:derek4716
ID: 8060454
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
 

Author Comment

by:derek4716
ID: 8060485
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
 
LVL 51

Accepted Solution

by:
ahoffmann earned 1600 total points
ID: 8060660
while(($key,$value) = each(%groups)) {print "KEY: $key VAL $value.\n";}
0
 

Author Comment

by:derek4716
ID: 8061122
thanks, figures I did something stupid :)
0
 

Author Comment

by:derek4716
ID: 8061197
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
 
LVL 48

Expert Comment

by:Tintin
ID: 8067364
And the typo of 'groups' is a classic example of where

use strict;

and

use warnings;

would have come to the rescue.
0
 

Author Comment

by:derek4716
ID: 8067741
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
 
LVL 48

Expert Comment

by:Tintin
ID: 8070225
#!/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
 

Author Comment

by:derek4716
ID: 8073949
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
 
LVL 48

Expert Comment

by:Tintin
ID: 8074913
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 8078195
lost the focus, sorry ...
but is there something to be done for the initial question?
0
 
LVL 15

Expert Comment

by:samri
ID: 8078469
ahoffmann (<-- got ur name right!)

- grab some coffee :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question