Author Topic: Pulling a random line from a text file  (Read 5005 times)

Offline DH

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Pulling a random line from a text file
« on: December 19, 2011, 06:20:37 PM »
Hi all

I'm a relative newbie at Scala and scripting - so bear with me.
I would like to design a script to pull a random line out of a text file for use on a slide/script.  For example, I have a fixed background and want to display 1 of 50 quotes that I have in a textfile on my network.  I know how to set up my text file and script to pull out inidividual lines (using !line3 etc in my script) - but I'd like to pull out of the file randomly (!line(n)) 

I've tired to set it up by matching it to a variable set by the random function (ie n=random(1,50), !line(n), or !linen or even in the script "!line"+"n") but nothing works - the best I get is seeing !line6 (or !line12) - it sets and displays the value of n 9the random number) but doesn't see it as input to the !line command.

Any ideas?

Thanks

DH

Offline nicu

  • Jr. Member
  • **
  • Posts: 48
  • Karma: 1
    • View Profile
Re: Pulling a random line from a text file
« Reply #1 on: December 20, 2011, 08:46:06 AM »
Use !line[n] not !line(n).

Offline DH

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: Pulling a random line from a text file
« Reply #2 on: December 20, 2011, 11:38:50 AM »
Hi Nicu

I tried your suggestion - it doesn't work.

Use !line[n] not !line(n).

This still returns just the number and not the line.  I've included the relevant code snippet:
{
  Group:
    External Integer(rd);    BackgroundSettings(Size(617, 1080));
  Sequence:
    rd = random(1,18);    {
      Group:
        TextFile("Content:\Scala Daily Events\whats_on.txt", LinesDisplayed(18));
..........
 Text(137, 292, "!line[!rd]", Face(On, Pen(2)), Backdrop(Off, Pen(3)), Focus(Off, Pen(11)), Outline(Off, Pen(1)), Shadow(Off, Pen(1)), Bevel(Off, Base(Pen(3)), Left(Pen(18)), Right(Pen(17)), Top(Pen(19)), Bottom(Pen(4))), AntiAlias(On), OnReplay(Replace), Under(Off, Thickness(3), Pen(2)), Font("Trebuchet MS (Western [])", 38), Leading(-6), Wrap(On, Width(180)));


This returns "!line[5]" and if I sub in !line!rd I get "line8"  .  For some reason the !line command is not seeing the variable as a number.  If I use !line[rd] I get "!line[rd]" as the output in player (and similarly for all other variations w/ brakcets and ! commands - I know I am doing the !line command properly and have linked the text properly because I use !line to reference particular lines in other places in the script and they output fine.

So I am still stumped (and feel like I must be missing somethng obvious)

D

Offline nicu

  • Jr. Member
  • **
  • Posts: 48
  • Karma: 1
    • View Profile
Re: Pulling a random line from a text file
« Reply #3 on: December 20, 2011, 03:11:09 PM »
Try !(line[rd]).

Offline DH

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: Pulling a random line from a text file
« Reply #4 on: December 20, 2011, 05:15:57 PM »
Nope - neither !(line[rd]) nor !(line[!rd]) works..... still returning !(line[rd]) or !(line[14]) in the player.
D

Offline nicu

  • Jr. Member
  • **
  • Posts: 48
  • Karma: 1
    • View Profile
Re: Pulling a random line from a text file
« Reply #5 on: December 21, 2011, 08:44:50 AM »
Try the last thing. That works for sure:
!(EvalString(^"line^" + Format(^"#^",rd)))
« Last Edit: December 21, 2011, 08:50:46 AM by nicu »

Offline DH

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: Pulling a random line from a text file
« Reply #6 on: December 21, 2011, 06:07:02 PM »
Works!  Thanks Nicu.  Not the most intutitive solution, that's for sure........

Offline alesh_eprom

  • Jr. Member
  • **
  • Posts: 15
  • Karma: 0
    • View Profile
Re: Pulling a random line from a text file
« Reply #7 on: March 13, 2012, 03:12:24 AM »
Wow,  could someone explain to a non scripting guy once more, how to achieve this random line if I have a txt file with x lines? Many thanks!

Ales

Offline DH

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: Pulling a random line from a text file
« Reply #8 on: March 13, 2012, 02:45:04 PM »
Nicu's last suggestion worked - at least for the context in how we were using it.

Offline alesh_eprom

  • Jr. Member
  • **
  • Posts: 15
  • Karma: 0
    • View Profile
Re: Pulling a random line from a text file
« Reply #9 on: March 15, 2012, 04:25:20 AM »
Where exactly do you put that string to make it work (as I said unfortunatelly I'm not into scripting)?

Offline DH

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: Pulling a random line from a text file
« Reply #10 on: March 15, 2012, 05:29:26 PM »
Here's the section of code I use:
 {
          Group:
            Text(115, 202, "!(EvalString(^"line^" + Format(^"#^",rd+2)))", Wipe("Dissolve", Duration(3000), Direction(90), Wait(Off)), Face(On, Pen(1)), Backdrop(Off, Pen(3)), Focus(Off, Pen(11)), Outline(Off, Pen(1)), Shadow(Off, Pen(1)), Bevel(Off, Base(Pen(3)), Left(Pen(18)), Right(Pen(17)), Top(Pen(19)), Bottom(Pen(4))), AntiAlias(On), OnReplay(Replace), Under(Off, Thickness(3), Pen(1)), Font("Gotham-Book (Western [<Gotham Book> 500])", 41), Wrap(On, Width(988)));
            HardDuration(2000);

this pulls from a file identified using:
TextFile("Content:\Scala Daily Events\sample.txt", LinesDisplayed(22))

Hope this helps

Offline alesh_eprom

  • Jr. Member
  • **
  • Posts: 15
  • Karma: 0
    • View Profile
Re: Pulling a random line from a text file
« Reply #11 on: March 16, 2012, 02:10:03 AM »
I know it's a dumm question, but where do I put this code?

Thanks!!

Offline DH

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
    • View Profile
Re: Pulling a random line from a text file
« Reply #12 on: March 19, 2012, 10:02:27 AM »
Ok- open your  original script in an editor (wordpad for example).  You will have a range of text that is your script.  It should look something like:

!ScalaScript500
// Saved by Scala Designer 5 Release 5.1 at 2011-12-22 12:42:53
:"aaaaaaa.sca"
{
  Group:
    External Integer(rd);
    BackgroundSettings(Size(1080, 1920));
    Config.RecentPublishLocations(PublishLocation("C Documents and Settings zzzzzzzzz", "aaaaaaaaa"));
  Sequence:
    rd = random(1,22);
    {
      Group:
        TextFile("Content:\Scala Daily Events\xxxxx.txt", LinesDisplayed(22));        Picture("C:\Documents and Settings\aaaa\bbbb\Content\E&I\someimage.jpg", Backdrop(Pen(1)), Margin(10, 10, 0, 0), Tabs(Explicit(80, 160, 240, 320, 400, 480, 560)), UserPalette(RGBPen(1, $0, $ffffff, $999999, $555555, $712068, $df449c, $dc110e, $662200, $ff5a00, $ff9c00, $ffee00, $8800, $dd00, $cccc, $66ff, $aa, $777777, $bbbbbb, $dddddd)), AutoScale(FillAndTrim), Operation(On));
      Sequence:
        :"_TempName1"
        {
          Group:
            Text(115, 202, "!(EvalString(^"line^" + Format(^"#^",rd+2)))", Wipe("Dissolve", Duration(3000), Direction(90), Wait(Off)), Face(On, Pen(1)), Backdrop(Off, Pen(3)), Focus(Off, Pen(11)), Outline(Off, Pen(1)), Shadow(Off, Pen(1)), Bevel(Off, Base(Pen(3)), Left(Pen(18)), Right(Pen(17)), Top(Pen(19)), Bottom(Pen(4))), AntiAlias(On), OnReplay(Replace), Under(Off, Thickness(3), Pen(1)), Font("Gotham-Book (Western [<Gotham Book> 500])", 41), Wrap(On, Width(988)));            HardDuration(2000);
        }
        {
          Group:
            WipeOut("_TempName1", Wipe("Dissolve", Duration(3000), Direction(90), Wait(Off)));
            HardDuration(2000);
        }
        :"_TempName2"
        {
          Group:
            Text(317, 552, "!(EvalString(^"line^" + Format(^"#^",rd)))", Wipe("Dissolve", Duration(3000), Direction(90), Wait(Off)), Face(On, Pen(1)), Backdrop(Off, Pen(3)), Focus(Off, Pen(11)), Outline(Off, Pen(1)), Shadow(Off, Pen(1)), Bevel(Off, Base(Pen(3)), Left(Pen(18)), Right(Pen(17)), Top(Pen(19)), Bottom(Pen(4))), AntiAlias(On), OnReplay(Replace), Under(Off, Thickness(3), Pen(1)), Font("Gotham-Book (Western [<Gotham Book> 500])", 40), Wrap(On, Width(836)));            HardDuration(2000);
        }
        {
          Group:
            WipeOut("_TempName2", Wipe("Dissolve", Duration(3000), Direction(90), Wait(Off)));
            HardDuration(2000);
        }
        :"_TempName3"
        {
          Group:
            Text(44, 1098, "!(EvalString(^"line^" + Format(^"#^",rd+1)))", Wipe("Dissolve", Duration(3000), Direction(90), Wait(Off)), Face(On, Pen(1)), Backdrop(Off, Pen(3)), Focus(Off, Pen(11)), Outline(Off, Pen(1)), Shadow(Off, Pen(1)), Bevel(Off, Base(Pen(3)), Left(Pen(18)), Right(Pen(17)), Top(Pen(19)), Bottom(Pen(4))), AntiAlias(On), OnReplay(Replace), Under(Off, Thickness(3), Pen(1)), Font("Gotham-Book (Western [<Gotham Book> 500])", 40), Wrap(On, Width(952)));            HardDuration(2000);
        }
        {
          Group:
            WipeOut("_TempName3", Wipe("Dissolve", Duration(3000), Direction(90), Wait(Off)));
            HardDuration(2000);
        }
    }
    {
      Group:
        Picture("C:\Documents and Settings\aaaaa\sssssss\Content\E&I\someimage.jpg", Backdrop(Pen(1)), Margin(10, 10, 0, 0), Tabs(Explicit(80, 160, 240, 320, 400, 480, 560)), UserPalette(RGBPen(1, $0, $ffffff, $999999, $555555, $712068, $df449c, $dc110e, $662200, $ff5a00, $ff9c00, $ffee00, $8800, $dd00, $cccc, $66ff, $aa, $777777, $bbbbbb, $dddddd)), AutoScale(FillAndTrim), Operation(On));
        HardDuration(2000);
        Quit(1);
    }
}
I've changed someof the paths to text files to generic placeholders and bolded the key lines for pulling the random codes.  To figure this out it is a good idea to take existing scripts and sample scripts from yoru Scala install and look at them in wordpad or someother text editor to explore the strucutre and syntax.

Once you are done you can then open the modified script in Designer and tweak the visuals and then publish.

D

Offline alesh_eprom

  • Jr. Member
  • **
  • Posts: 15
  • Karma: 0
    • View Profile
Re: Pulling a random line from a text file
« Reply #13 on: March 21, 2012, 04:56:25 AM »
Excellent! Understood and tweaked -> worked.

Thanks a bunch!

Offline alesh_eprom

  • Jr. Member
  • **
  • Posts: 15
  • Karma: 0
    • View Profile
Re: Pulling a random line from a text file
« Reply #14 on: March 21, 2012, 09:50:02 AM »
Now, to make it even more interesting, I'm thinking of adding the second text file and then make the script play in the following order:

- first slide, "x" seconds: textfile nr1 - random line
- next slide, "y" seconds: textfile nr2 - random line
- and so on

Would this be possible trough some similar coding?