Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi

  • Here's my data
  • jsdlkjalsd same_text = 1, jhhj, jhjk = 256,iuqoi,uereoiu,sjksdh, same_text = 2, lkkj, lksj = 6287
  • uwie same_text = 2, sdaj, jsdgd, sksd
  • what i am trying is to get make a reference of "same_text" with the value assigned to it as the key and values found in the second line as values

    while(<LF>){ if ($_ =~ /(same_text\s=\s\d),((\s\S+){1,2})/gi){ ($junk,$ky)=split(/=/,$1); push(@{$hash{$ky}},$2); } }

    The above only gets me the first match "same_text = 1, jhhj" and dumps "jhhj" in the hash-array-value. the rest of the matching does not happen

    what i need exactly is : A hash of same_text with {1,2,3..} as keys and i get values "sdaj, jsdgd, sksd" for the respective keys

  • Thanks in advance
  • Replies are listed 'Best First'.
    Re: Repeat match
    by siva kumar (Pilgrim) on Jan 30, 2007 at 12:51 UTC
      Your post is not clear to understand.
      According to my understanding, your data seems to be like this
      jsdlkjalsd same_text = 1, jhhj, jhjk = 256, iuqoi, uereoiu, sjksdh, s +ame_text = 2, lkkj, lksj = 6287 uwie same_text = 2, sdaj, jsdgd, sksd
      you want a hash output as
      For the key "1" the value should be "jhhj, jhjk" For the key "2" the value should be "lkkj, lksj , sdaj, jsdgd"
      If I am correct then you can use the below perl code.
      use Data::Dumper; open(FH,"x.txt") or die("Can't open the file: $!"); $/=EOF; my $var = <FH>; my %hash=(); while ($var =~ /(same_text\s=\s\d),((\s\S+){1,2})/gi) { my ($junk,$ky)=split(/=/,$1); push(@{$hash{$ky}},$2); } map { print "$_ -- @{$hash{$_}} \n "; } keys %hash;
        Thanks..Siva Kumar... works now!!!
    Re: Repeat match
    by RMGir (Prior) on Jan 30, 2007 at 13:13 UTC
      Somewhere, a perl teacher is waiting for this assignment to be handed in, ready to search on google and perlmonks for iuqoi uereoiu sjksdh :)

      Mike
    Re: Repeat match
    by swampyankee (Parson) on Jan 30, 2007 at 13:25 UTC

      Neglecting the trivial nits -- such as the superfluous $_ -- it may be easier to use split twice: first with qq(same_text\s=\s\d) on the records from <LF>, then on the resulting array elements. Using split, vs a regex, on your first line:

      my $string = q(jsdlkjalsd same_text = 1, jhhj, jhjk = 256,iuqoi,uereoi +u,sjksdh, same_text = 2, lkkj, lksj = 6287); my @inter = split(/(same_text\s=\s)/i, $string);
      will yield an array:
      jsdlkjalsd same_text = 1, jhhj, jhjk = 256,iuqoi,uereoiu,sjksdh, same_text = 2, lkkj, lksj = 6287

      emc

      Insisting on perfect safety is for people who don't have the balls to live in the real world.

      —Mary Shafer, NASA Dryden Flight Research Center
    Re: Repeat match
    by Anonymous Monk on Jan 30, 2007 at 11:44 UTC
    • Sorry, forgot to include
    • Example output:
    • $hash{1}=[jhhj]; $hash{2}=[lkkj,sdaj,jsdgd];