Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split array in array

Status
Not open for further replies.

PerlElvir

Technical User
Aug 23, 2005
68
Hi Idont know how to eplain this, so than throught example and I hope that someone can help me;) so I have

my @ace_param6 = ("LYON LONDON PARIS<");
my @ace_param7 = ("le 23 avr. 2006 : 398,00 @0@20060423@X@0@32823@@le 30 avr. 2006 : 354,00 @1@20060430@X@0@32823@@le 07 mai 2006 : 328,00 @2@20060507@X@0@32823@@--HERE--L_1= 16 avr. 2006 : 418,00 @27@20060416@X@0@32823@@le 23 avr. 2006 : 388,00 @28@20060423@X@0@32823@@le 30 avr. 2006 : 344,00 @29@20060430@X@0@32823@@--HERE--L_2= 08 avr. 2006 : 299,00 au lieu de 398,00 @55@20060408@F@1@32824@@le 09 avr. 2006 : 299,00 au lieu de 398,00 @56@20060409@F@1@32824@@le 16 avr. 2006 : 428,00 @57@20060416@F@1@32824@@");

So I want to slpit @ace_param6 with &nbsp; to get

@ace_param6=("LYON","LONDON","PARIS")

and split @ace_param7 with --HERE-- to get

@ace_param6=("le 23 avr. 2006 : 398,00 @0@20060423@X@0@32823@@le 30 avr. 2006 : 354,00 @1@20060430@X@0@32823@@le 07 mai 2006 : 328,00 @2@20060507@X@0@32823@@","L_1= 16 avr. 2006 : 418,00 @27@20060416@X@0@32823@@le 23 avr. 2006 : 388,00 @28@20060423@X@0@32823@@le 30 avr. 2006 : 344,00 @29@20060430@X@0@32823@@","L_2= 08 avr. 2006 : 299,00 au lieu de 398,00 @55@20060408@F@1@32824@@")


SO now I have 3 elements each array and I want to get

for (my $i = 0; $i <= $#ville_arr; $i++)

{
$first=@ace_param6[$i]-@ace_param7[$i];
}

but in @ace_param7 I want to extract and convert to new dates also for price, but let do now just dates, so 23 avr. 2006 I want to be like 2006-4-23 and all other dates, at the end I have to get

LYON-2006-4-23
LYON-2006-4-30
LYON-2006-5-07
LONDON-2006-4-16
LONDON-2006-4-23
LONDON-2006-4-30
PARIS-2006-4-08

I hope that some can help. This forum help me a lot of time and I hope also now will be same answer ;)


 
if you only have 3 dates in the big array then try this
Code:
@ace_param6 = ('LYON LONDON PARIS');
@ace_param7 = ('le 23 avr. 2006 : 398,00 @0@20060423@X@0@32823@@le 30 avr. 2006 : 354,00 @1@20060430@X@0@32823@@le 07 mai 2006 : 328,00 @2@20060507@X@0@32823@@--HERE--L_1= 16 avr. 2006 : 418,00 @27@20060416@X@0@32823@@le 23 avr. 2006 : 388,00 @28@20060423@X@0@32823@@le 30 avr. 2006 : 344,00 @29@20060430@X@0@32823@@--HERE--L_2= 08 avr. 2006 : 299,00  au lieu de 398,00 @55@20060408@F@1@32824@@le 09 avr. 2006 : 299,00  au lieu de 398,00 @56@20060409@F@1@32824@@le 16 avr. 2006 : 428,00 @57@20060416@F@1@32824@@');

$ace_param6 = $ace_param6[0];
$ace_param7 = $ace_param7[0];

@ace_param6 = split(/ /,$ace_param6);
@ace_param7 = split(/--HERE--/,$ace_param7);

for($i=0;$i<@ace_param6;$i++) {
	$match = '\d{2} \w{3}\.? \d{4}';
	$ace_param7[$i] =~ /($match).*($match).*($match)/;
	my @foo = ("$1","$2","$3");
	$hash{$ace_param6[$i]} = \@foo;	
}

foreach $key (keys %hash) {
	for $i ( 0 .. $#{ $hash{$key} } ) {
		print "$key: $hash{$key}[$i]\n";
	}
}

Output
Code:
LYON: 23 avr. 2006
LYON: 30 avr. 2006
LYON: 07 mai 2006
PARIS: 08 avr. 2006
PARIS: 09 avr. 2006
PARIS: 16 avr. 2006
LONDON: 16 avr. 2006
LONDON: 23 avr. 2006
LONDON: 30 avr. 2006
Now try yourself, to map the month to its number and turn around the year and the day.
If you can then post your tries and we might help some more.


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Well yestrday Im find some one, but thx on rensponse anyway ;o) here is my way:


use DBI;



my @ace_param6 = ("LYON LONDON PARIS<");
my @ace_param7 = ("le 23 avr. 2006 : 398,00 @0@20060423@X@0@32823@@le 30 avr. 2006 : 354,00 @1@20060430@X@0@32823@@le 07 mai 2006 : 328,00 @2@20060507@X@0@32823@@--HERE--L_1= 16 avr. 2006 : 418,00 @27@20060416@X@0@32823@@le 23 avr. 2006 : 388,00 @28@20060423@X@0@32823@@le 30 avr. 2006 : 344,00 @29@20060430@X@0@32823@@--HERE--L_2= 08 avr. 2006 : 299,00 au lieu de 398,00 @55@20060408@F@1@32824@@le 09 avr. 2006 : 299,00 au lieu de 398,00 @56@20060409@F@1@32824@@le 16 avr. 2006 : 428,00 @57@20060416@F@1@32824@@");

sub manage_ex()
{


$dbh = DBI->connect("DBI:mysql:veille_sejours_test:localhost", "root", "");



@ville_arr1 = split(/ /, $ace_param6[0]);


for (my $i = 0; $i <= $#ville_arr1; $i++)

{

@dates = split(/HERE/, $ace_param7[0]);

#print $dates[$i]."\n\n\n\n";
@dates_all = split(/@/, $dates[$i]);
for (my $j = 0; $j <= $#dates_all; $j++)

{


my ($prix) = $dates_all[$j] =~ /:\s*(.*)\s*/;
$prix_n=$prix;

if ($prix_n =~ /d/)
{
my ($prix1) = $prix_n =~ /\s*(.*)\s*a/;

push(@price,$prix1);
}
else
{
$prix1 = $prix_n;
push(@price,$prix1);
}
my ($dates_all1) = $dates_all[$j] =~ /\s*(.*)\s*:/;
$dates_all1=~ s/--L_1= //g;
$dates_all1=~ s/--L_2= //g;
$dates_fin=trans_date($dates_all1);
$dates_fin =~ s/\D//g;
$day_fin=substr($dates_fin,0,2);
$mo_fin=substr($dates_fin,2,2);
$year_fin=substr($dates_fin,4,4);

$dates_final=$year_fin."-".$mo_fin."-".$day_fin;



print $ville_arr1[$i]."-".$dates_final."\n";


}

}



$dbh->disconnect();
}


sub trans_date()
{
my $type = $_[0];
$type=~s/jan./01/;
$type=~s/feb./02/;
$type=~s/mars/03/;
$type=~s/avr./04/;
$type=~s/mai/05/;
$type=~s/juin/06/;
$type=~s/juil./07/;
$type=~s/août/08/;
$type=~s/sept./09/;
$type=~s/oct./10/;
$type=~s/nov./11/;
$type=~s/dec./12/;


return $type;
}


manage_ex();



 
It looks like your code is doing way more work than necessary to me. You already have your dates in the strings:

@20060423@

and all that splitting just looks unecessary.
 
yes but I didnt know how will I extraxt that dates? Do you know? If so that will be nice to make some script :eek:)
 
perluserpengo posted some good code you could work with. As far as the dates go just use a regexp to pull them out of the strings:

my @dates = $string =~ /@(\d{8})@/g;

where $string is the string you are working with to get the date out of.

 
hmmm....I haven't notice the '@20060423@' in the middle of the line.

I think I'm getting toooooooooo old....


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
I came up with this, not the most readable code but very compact:

Code:
use Data::Dump qw/dump/;

my $ace_param6 = 'LYON LONDON PARIS';
my $ace_param7 = 'le 23 avr. 2006 : 398,00 @0@20060423@X@0@32823@@le 30 avr. 2006 : 354,00 @1@20060430@X@0@32823@@le 07 mai 2006 : 328,00 @2@20060507@X@0@32823@@--HERE--L_1= 16 avr. 2006 : 418,00 @27@20060416@X@0@32823@@le 23 avr. 2006 : 388,00 @28@20060423@X@0@32823@@le 30 avr. 2006 : 344,00 @29@20060430@X@0@32823@@--HERE--L_2= 08 avr. 2006 : 299,00  au lieu de 398,00 @55@20060408@F@1@32824@@le 09 avr. 2006 : 299,00  au lieu de 398,00 @56@20060409@F@1@32824@@le 16 avr. 2006 : 428,00 @57@20060416@F@1@32824@@';

manage_ex();
sub  manage_ex {

#$dbh = DBI->connect("DBI:mysql:veille_sejours_test:localhost", "root", "");
   my %ville_hash = map {$_,undef} split(/ /, $ace_param6);
   my @ville_arr1 = split(/ /, $ace_param6);# to retain order of list
   my @dates = split(/--HERE--/, $ace_param7);
   foreach my $i (0 .. $#ville_arr1) {
         push (@{$ville_hash{$ville_arr1[$i]}{PRICES}}, ($dates[$i] =~ /:\s+([^ ]+)\s/g));
         push (@{$ville_hash{$ville_arr1[$i]}{DATES}}, map {m/(\d\d\d\d)(\d\d)(\d\d)/;"$1-$2-$3"} ($dates[$i] =~ /@(\d{8})@/g));
      }
#   # $dbh->disconnect();
   print dump(%ville_hash);
}

results:

Code:
(
  "LYON",
  {
    DATES  => ["2006-04-23", "2006-04-30", "2006-05-07"],
    PRICES => ["398,00", "354,00", "328,00"],
  },
  "PARIS",
  {
    DATES  => ["2006-04-08", "2006-04-09", "2006-04-16"],
    PRICES => ["299,00", "299,00", "428,00"],
  },
  "LONDON",
  {
    DATES  => ["2006-04-16", "2006-04-23", "2006-04-30"],
    PRICES => ["418,00", "388,00", "344,00"],
  },
)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top